#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 */
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void addtobuffer(char **buf, int *bufsz, char *newtext) {
  if (*buf == NULL) {
    *bufsz = strlen(newtext) + 4096;
    *buf = (char *)malloc(*bufsz);
    **buf = '\0';
  } else if ((strlen(*buf) + strlen(newtext) + 1) > *bufsz) {
    *bufsz += strlen(newtext) + 4096;
    *buf = (char *)realloc(*buf, *bufsz);
  }
  strcat(*buf, newtext);
}


x86 assembly (clang, -O0):
<addtobuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119d <addtobuffer+0x5d>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0x1000,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
jmp    1205 <addtobuffer+0xc5>
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,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jle    1200 <addtobuffer+0xc0>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0x1000,%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1205 <addtobuffer+0xc5>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addtobuffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1192 <addtobuffer+0x52>
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %ebp,%eax
cmp    (%r15),%eax
jl     11ae <addtobuffer+0x6e>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    (%r15),%ecx
lea    (%rax,%rcx,1),%esi
add    $0x1000,%esi
mov    %esi,(%r15)
mov    (%rbx),%rdi
call   1050 <realloc@plt>
mov    %rax,(%rbx)
jmp    11ae <addtobuffer+0x6e>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1000(%rax),%edi
mov    %edi,(%r15)
call   1040 <malloc@plt>
mov    %rax,(%rbx)
movb   $0x0,(%rax)
mov    (%rbx),%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <strcat@plt>

LLVM IR (-O0):
define void @addtobuffer(i8** %0, i32* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i8*
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%7 = load i8**, i8*** %4
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %22
10: ; preds = %3
%11 = load i8*, i8** %6
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %12, 4096
%14 = load i32*, i32** %5
store i32 %13, i32* %14
%15 = load i32*, i32** %5
%16 = load i32, i32* %15
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = load i8**, i8*** %4
store i8* %18, i8** %19
%20 = load i8**, i8*** %4
%21 = load i8*, i8** %20
store i8 0, i8* %21
br label %48
22: ; preds = %3
%23 = load i8**, i8*** %4
%24 = load i8*, i8** %23
%25 = call i32 @strlen(i8* %24)
%26 = load i8*, i8** %6
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %25, %27
%29 = add nsw i32 %28, 1
%30 = load i32*, i32** %5
%31 = load i32, i32* %30
%32 = icmp sgt i32 %29, %31
br i1 %32, label %33, label %47
33: ; preds = %22
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
%36 = add nsw i32 %35, 4096
%37 = load i32*, i32** %5
%38 = load i32, i32* %37
%39 = add nsw i32 %38, %36
store i32 %39, i32* %37
%40 = load i8**, i8*** %4
%41 = load i8*, i8** %40
%42 = load i32*, i32** %5
%43 = load i32, i32* %42
%44 = call i64 @realloc(i8* %41, i32 %43)
%45 = inttoptr i64 %44 to i8*
%46 = load i8**, i8*** %4
store i8* %45, i8** %46
br label %47
47: ; preds = %33, %22
br label %48
48: ; preds = %47, %10
%49 = load i8**, i8*** %4
%50 = load i8*, i8** %49
%51 = load i8*, i8** %6
%52 = call i32 @strcat(i8* %50, i8* %51)
ret void
}

LLVM IR (-O2):
define void @addtobuffer(i8** nocapture %0, i32* nocapture %1, i8* %2) {
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %2)
%8 = add nsw i32 %7, 4096
store i32 %8, i32* %1
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %0
store i8 0, i8* %10
br label %25
11: ; preds = %3
%12 = tail call i32 @strlen(i8* %4)
%13 = tail call i32 @strlen(i8* %2)
%14 = add nsw i32 %13, %12
%15 = load i32, i32* %1
%16 = icmp slt i32 %14, %15
br i1 %16, label %25, label %17
17: ; preds = %11
%18 = tail call i32 @strlen(i8* %2)
%19 = add nsw i32 %18, 4096
%20 = load i32, i32* %1
%21 = add nsw i32 %19, %20
store i32 %21, i32* %1
%22 = load i8*, i8** %0
%23 = tail call i64 @realloc(i8* %22, i32 %21)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %0
br label %25
25: ; preds = %11, %17, %6
%26 = load i8*, i8** %0
%27 = tail call i32 @strcat(i8* %26, i8* %2)
ret void
}