#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int append_str(char **dest, int buffer_size, char *source) {
  int dest_size = strlen(*dest);
  int source_size = strlen(source);
  int new_buffer_size = buffer_size;
  while (new_buffer_size < dest_size + source_size) {
    new_buffer_size *= 2;
  }
  if (new_buffer_size != buffer_size) {
    char *new_dest = (char *)realloc(*dest, new_buffer_size);
    if (!new_dest) {
      printf("Fatal error! Could not reallocate memory\n");
      exit(EXIT_FAILURE);
    }
    *dest = new_dest;
  }
  strcat(*dest, source);
  return new_buffer_size;
}


x86 assembly (clang, -O0):
<append_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    -0x20(%rbp),%ecx
cmp    %ecx,%eax
jge    11a3 <append_str+0x53>
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1184 <append_str+0x34>
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11f2 <append_str+0xa2>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e7 <append_str+0x97>
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
mov    (%rdi),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
mov    %ebp,%ecx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ecx,%ebx
lea    (%rbx,%rbx,1),%ecx
cmp    %eax,%ebx
jl     1180 <append_str+0x30>
mov    (%r15),%rdi
cmp    %ebp,%ebx
je     11a2 <append_str+0x52>
mov    %ebx,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11b7 <append_str+0x67>
mov    %rax,%rdi
mov    %rax,(%r15)
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"Fatal error! Could not reallocate memory\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @append_str(i8** %0, i32 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i8**, i8*** %4
%12 = load i8*, i8** %11
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %8
%16 = load i32, i32* %5
store i32 %16, i32* %9
br label %17
17: ; preds = %23, %3
%18 = load i32, i32* %9
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = add nsw i32 %19, %20
%22 = icmp slt i32 %18, %21
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* %9
%25 = mul nsw i32 %24, 2
store i32 %25, i32* %9
br label %17
26: ; preds = %17
%27 = load i32, i32* %9
%28 = load i32, i32* %5
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i8**, i8*** %4
%32 = load i8*, i8** %31
%33 = load i32, i32* %9
%34 = call i64 @realloc(i8* %32, i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %10
%36 = load i8*, i8** %10
%37 = icmp ne i8* %36, null
br i1 %37, label %42, label %38
38: ; preds = %30
%39 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %30
%43 = load i8*, i8** %10
%44 = load i8**, i8*** %4
store i8* %43, i8** %44
br label %45
45: ; preds = %42, %26
%46 = load i8**, i8*** %4
%47 = load i8*, i8** %46
%48 = load i8*, i8** %6
%49 = call i32 @strcat(i8* %47, i8* %48)
%50 = load i32, i32* %9
ret i32 %50
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@str = [41 x i8] c"Fatal error! Could not reallocate memory\00"
define i32 @append_str(i8** nocapture %0, i32 %1, i8* %2) {
%4 = load i8*, i8** %0
%5 = tail call i32 @strlen(i8* %4)
%6 = tail call i32 @strlen(i8* %2)
%7 = add nsw i32 %6, %5
br label %8
8: ; preds = %8, %3
%9 = phi i32 [ %1, %3 ], [ %11, %8 ]
%10 = icmp slt i32 %9, %7
%11 = shl nsw i32 %9, 1
br i1 %10, label %8, label %12
12: ; preds = %8
%13 = icmp eq i32 %9, %1
%14 = load i8*, i8** %0
br i1 %13, label %24, label %15
15: ; preds = %12
%16 = tail call i64 @realloc(i8* %14, i32 %9)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %15
%23 = inttoptr i64 %16 to i8*
store i8* %23, i8** %0
br label %24
24: ; preds = %12, %22
%25 = phi i8* [ %23, %22 ], [ %14, %12 ]
%26 = tail call i32 @strcat(i8* %25, i8* %2)
ret i32 %9
}