#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*/ fprintf(int /*<<< orphan*/, char *);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
int append_char(char **token, char appc, int pos, int size) {
  if (pos >= size - 1) {
    size = size * 2;
    char *newplace = realloc(*token, sizeof(char) * size);
    if (newplace != NULL) {
      *token = newplace;
    } else {
      fprintf(stderr,
              "append_char failed to reallocate memory when necessary.\n");
    }
  }
  (*token)[pos] = appc;
  return size;
}


x86 assembly (clang, -O0):
<append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     11a1 <append_char+0x81>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1187 <append_char+0x67>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    119c <append_char+0x7c>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11a1 <append_char+0x81>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
lea    -0x1(%rbx),%eax
cmp    %edx,%eax
jg     1163 <append_char+0x43>
add    %ebx,%ebx
mov    (%r15),%rdi
mov    %ebx,%esi
call   1040 <realloc@plt>
test   %rax,%rax
je     114e <append_char+0x2e>
mov    %rax,(%r15)
jmp    1163 <append_char+0x43>
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
lea    0xea2(%rip),%rsi
call   1030 <fprintf@plt>
mov    (%r15),%rax
movslq %ebp,%rcx
mov    %r14b,(%rax,%rcx,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [57 x i8] c"append_char failed to reallocate memory when necessary.\0A\00"
define i32 @append_char(i8** %0, i8 signext %1, i32 %2, i32 %3) {
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %5
store i8 %1, i8* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* %7
%11 = load i32, i32* %8
%12 = sub nsw i32 %11, 1
%13 = icmp sge i32 %10, %12
br i1 %13, label %14, label %33
14: ; preds = %4
%15 = load i32, i32* %8
%16 = mul nsw i32 %15, 2
store i32 %16, i32* %8
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = mul i64 1, %20
%22 = trunc i64 %21 to i32
%23 = call i8* @realloc(i8* %18, i32 %22)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = load i8*, i8** %9
%28 = load i8**, i8*** %5
store i8* %27, i8** %28
br label %32
29: ; preds = %14
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %29, %26
br label %33
33: ; preds = %32, %4
%34 = load i8, i8* %6
%35 = load i8**, i8*** %5
%36 = load i8*, i8** %35
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %34, i8* %39
%40 = load i32, i32* %8
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [57 x i8] c"append_char failed to reallocate memory when necessary.\0A\00"
define i32 @append_char(i8** nocapture %0, i8 signext %1, i32 %2, i32 %3) {
%5 = add nsw i32 %3, -1
%6 = icmp sgt i32 %5, %2
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = shl nsw i32 %3, 1
%9 = load i8*, i8** %0
%10 = tail call i8* @realloc(i8* %9, i32 %8)
%11 = icmp eq i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %7
store i8* %10, i8** %0
br label %16
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %12, %13, %4
%17 = phi i32 [ %3, %4 ], [ %8, %13 ], [ %8, %12 ]
%18 = load i8*, i8** %0
%19 = sext i32 %2 to i64
%20 = getelementptr inbounds i8, i8* %18, i64 %19
store i8 %1, i8* %20
ret i32 %17
}