#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int extend_token(char **tok, char *buf, int size) {
  char *newtok = realloc(*tok, size);
  if (!newtok) {
    free(*tok);
    *tok = NULL;
    return -1;
  }
  if (!*tok)
    strcpy(newtok, buf);
  else
    strcat(newtok, buf);
  *tok = newtok;
  return 0;
}


x86 assembly (clang, -O0):
<extend_token>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1194 <extend_token+0x54>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <extend_token+0x93>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11b4 <extend_token+0x74>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11c1 <extend_token+0x81>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extend_token>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rdi),%rdi
mov    %edx,%esi
call   1050 <realloc@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
test   %rax,%rax
je     1172 <extend_token+0x32>
test   %rdi,%rdi
je     117e <extend_token+0x3e>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
jmp    1189 <extend_token+0x49>
call   1030 <free@plt>
mov    $0xffffffff,%eax
jmp    118b <extend_token+0x4b>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
xor    %eax,%eax
mov    %rbx,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @extend_token(i8** %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8**, i8*** %5
%10 = load i8*, i8** %9
%11 = load i32, i32* %7
%12 = call i8* @realloc(i8* %10, i32 %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %3
%16 = load i8**, i8*** %5
%17 = load i8*, i8** %16
%18 = call i32 @free(i8* %17)
%19 = load i8**, i8*** %5
store i8* null, i8** %19
store i32 -1, i32* %4
br label %35
20: ; preds = %3
%21 = load i8**, i8*** %5
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i8*, i8** %8
%26 = load i8*, i8** %6
%27 = call i32 @strcpy(i8* %25, i8* %26)
br label %32
28: ; preds = %20
%29 = load i8*, i8** %8
%30 = load i8*, i8** %6
%31 = call i32 @strcat(i8* %29, i8* %30)
br label %32
32: ; preds = %28, %24
%33 = load i8*, i8** %8
%34 = load i8**, i8*** %5
store i8* %33, i8** %34
store i32 0, i32* %4
br label %35
35: ; preds = %32, %15
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
define i32 @extend_token(i8** nocapture %0, i8* %1, i32 %2) {
%4 = load i8*, i8** %0
%5 = tail call i8* @realloc(i8* %4, i32 %2)
%6 = icmp eq i8* %5, null
%7 = load i8*, i8** %0
br i1 %6, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @free(i8* %7)
br label %16
10: ; preds = %3
%11 = icmp eq i8* %7, null
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @strcpy(i8* %5, i8* %1)
br label %16
14: ; preds = %10
%15 = tail call i32 @strcat(i8* %5, i8* %1)
br label %16
16: ; preds = %12, %14, %8
%17 = phi i32 [ -1, %8 ], [ 0, %14 ], [ 0, %12 ]
store i8* %5, i8** %0
ret i32 %17
}