#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*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
void AddChar(char **str, int ch) {
  int len;
  if (!(*str)) {
    *str = (char *)(malloc(16));
    if (!(*str)) {
      fputs("out of memory\n", stderr);
      exit(1);
    }
    len = 0;
  } else if ((strlen(*str) % 16) == 0) {
    *str = (char *)(realloc(*str, strlen(*str) + 16));
    if (!(*str)) {
      fputs("out of memory\n", stderr);
      exit(1);
    }
    len = strlen(*str);
  } else {
    len = strlen(*str);
  }
  (*str)[len] = (char)ch;
  (*str)[len + 1] = '\0';
}


x86 assembly (clang, -O0):
<AddChar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11ba <AddChar+0x6a>
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11ae <AddChar+0x5e>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x10(%rbp)
jmp    125b <AddChar+0x10b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1247 <AddChar+0xf7>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    -0x18(%rbp),%rdi
mov    %eax,%esi
add    $0x10,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1233 <AddChar+0xe3>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xddc(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
jmp    1256 <AddChar+0x106>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
jmp    125b <AddChar+0x10b>
mov    -0xc(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AddChar>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     119c <AddChar+0x4c>
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    (%rbx),%r15
mov    %r15,%rdi
call   1030 <strlen@plt>
test   $0xf,%bpl
jne    11b3 <AddChar+0x63>
add    $0x10,%eax
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11cf <AddChar+0x7f>
mov    %rax,%rdi
call   1030 <strlen@plt>
jmp    11b3 <AddChar+0x63>
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    %rax,(%rbx)
xor    %eax,%eax
test   %rcx,%rcx
je     11cf <AddChar+0x7f>
mov    (%rbx),%rcx
cltq
mov    %r14b,(%rcx,%rax,1)
mov    (%rbx),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0xe21(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"out of memory\0A\00"
@stderr = global i32 0
define void @AddChar(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%6 = load i8**, i8*** %3
%7 = load i8*, i8** %6
%8 = icmp ne i8* %7, null
br i1 %8, label %21, label %9
9: ; preds = %2
%10 = call i64 @malloc(i32 16)
%11 = inttoptr i64 %10 to i8*
%12 = load i8**, i8*** %3
store i8* %11, i8** %12
%13 = load i8**, i8*** %3
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %9
store i32 0, i32* %5
br label %53
21: ; preds = %2
%22 = load i8**, i8*** %3
%23 = load i8*, i8** %22
%24 = call i32 @strlen(i8* %23)
%25 = srem i32 %24, 16
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %48
27: ; preds = %21
%28 = load i8**, i8*** %3
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %3
%31 = load i8*, i8** %30
%32 = call i32 @strlen(i8* %31)
%33 = add nsw i32 %32, 16
%34 = call i64 @realloc(i8* %29, i32 %33)
%35 = inttoptr i64 %34 to i8*
%36 = load i8**, i8*** %3
store i8* %35, i8** %36
%37 = load i8**, i8*** %3
%38 = load i8*, i8** %37
%39 = icmp ne i8* %38, null
br i1 %39, label %44, label %40
40: ; preds = %27
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %27
%45 = load i8**, i8*** %3
%46 = load i8*, i8** %45
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %5
br label %52
48: ; preds = %21
%49 = load i8**, i8*** %3
%50 = load i8*, i8** %49
%51 = call i32 @strlen(i8* %50)
store i32 %51, i32* %5
br label %52
52: ; preds = %48, %44
br label %53
53: ; preds = %52, %20
%54 = load i32, i32* %4
%55 = trunc i32 %54 to i8
%56 = load i8**, i8*** %3
%57 = load i8*, i8** %56
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 %55, i8* %60
%61 = load i8**, i8*** %3
%62 = load i8*, i8** %61
%63 = load i32, i32* %5
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %62, i64 %65
store i8 0, i8* %66
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"out of memory\0A\00"
@stderr = global i32 0
define void @AddChar(i8** nocapture %0, i32 %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i64 @malloc(i32 16)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %0
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %30
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32 @strlen(i8* %3)
%15 = and i32 %14, 15
%16 = icmp eq i32 %15, 0
%17 = load i8*, i8** %0
%18 = tail call i32 @strlen(i8* %17)
br i1 %16, label %19, label %30
19: ; preds = %13
%20 = add nsw i32 %18, 16
%21 = tail call i64 @realloc(i8* %17, i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %0
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = tail call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = tail call i32 @strlen(i8* %22)
br label %30
30: ; preds = %13, %5, %28
%31 = phi i32 [ %29, %28 ], [ 0, %5 ], [ %18, %13 ]
%32 = trunc i32 %1 to i8
%33 = load i8*, i8** %0
%34 = sext i32 %31 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 %32, i8* %35
%36 = load i8*, i8** %0
%37 = add nsw i32 %31, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
ret void
}