#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ *realloc(char *, int);
int strlen(char *);
int _APPEND_work(char **__new, int *_buf_len, char *filename, char *append) {
  int buf_len = *_buf_len;
  int append_len = strlen(append);
  int new_len = strlen(*__new);
  if ((new_len + append_len) > buf_len) {
    if (realloc(*__new, buf_len + append_len) == NULL) {
      return -1111111111;
    }
    *_buf_len = *_buf_len + append_len;
  }
  memcpy((char *)*__new + new_len, append, append_len);
  memcpy(((char *)*__new + new_len + append_len), "\0", 1);
  return 0;
}


x86 assembly (clang, -O0):
<_APPEND_work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jle    11b2 <_APPEND_work+0x82>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%esi
add    -0x30(%rbp),%esi
call   1050 <realloc@plt>
cmp    $0x0,%rax
jne    11a3 <_APPEND_work+0x73>
movl   $0xbdc5ca39,-0x4(%rbp)
jmp    11f9 <_APPEND_work+0xc9>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
lea    0xe18(%rip),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_APPEND_work>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    (%rsi),%ebp
mov    %rcx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    (%r15),%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
lea    (%rbx,%r13,1),%eax
cmp    %ebp,%eax
jle    1178 <_APPEND_work+0x48>
mov    (%r15),%rdi
add    %ebx,%ebp
mov    %ebp,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11a9 <_APPEND_work+0x79>
add    %ebx,(%r12)
movslq %r13d,%rbp
mov    (%r15),%rdi
add    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
add    (%r15),%rbp
movslq %ebx,%rdi
add    %rbp,%rdi
lea    0xe65(%rip),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
xor    %eax,%eax
jmp    11ae <_APPEND_work+0x7e>
mov    $0xbdc5ca39,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] zeroinitializer
define i32 @_APPEND_work(i8** %0, i32* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8** %0, i8*** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i32*, i32** %7
%14 = load i32, i32* %13
store i32 %14, i32* %10
%15 = load i8*, i8** %9
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %11
%17 = load i8**, i8*** %6
%18 = load i8*, i8** %17
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %12
%20 = load i32, i32* %12
%21 = load i32, i32* %11
%22 = add nsw i32 %20, %21
%23 = load i32, i32* %10
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %40
25: ; preds = %4
%26 = load i8**, i8*** %6
%27 = load i8*, i8** %26
%28 = load i32, i32* %10
%29 = load i32, i32* %11
%30 = add nsw i32 %28, %29
%31 = call i32* @realloc(i8* %27, i32 %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 -1111111111, i32* %5
br label %58
34: ; preds = %25
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = load i32, i32* %11
%38 = add nsw i32 %36, %37
%39 = load i32*, i32** %7
store i32 %38, i32* %39
br label %40
40: ; preds = %34, %4
%41 = load i8**, i8*** %6
%42 = load i8*, i8** %41
%43 = load i32, i32* %12
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8*, i8** %9
%47 = load i32, i32* %11
%48 = call i32 @memcpy(i8* %45, i8* %46, i32 %47)
%49 = load i8**, i8*** %6
%50 = load i8*, i8** %49
%51 = load i32, i32* %12
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i32, i32* %11
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = call i32 @memcpy(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 0, i32* %5
br label %58
58: ; preds = %40, %33
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
@.str = [2 x i8] zeroinitializer
define i32 @_APPEND_work(i8** nocapture %0, i32* nocapture %1, i8* nocapture readnone %2, i8* %3) {
%5 = load i32, i32* %1
%6 = tail call i32 @strlen(i8* %3)
%7 = load i8*, i8** %0
%8 = tail call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, %6
%10 = icmp sgt i32 %9, %5
br i1 %10, label %11, label %19
11: ; preds = %4
%12 = load i8*, i8** %0
%13 = add nsw i32 %6, %5
%14 = tail call i32* @realloc(i8* %12, i32 %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %29, label %16
16: ; preds = %11
%17 = load i32, i32* %1
%18 = add nsw i32 %17, %6
store i32 %18, i32* %1
br label %19
19: ; preds = %16, %4
%20 = load i8*, i8** %0
%21 = sext i32 %8 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = tail call i32 @memcpy(i8* %22, i8* %3, i32 %6)
%24 = load i8*, i8** %0
%25 = getelementptr inbounds i8, i8* %24, i64 %21
%26 = sext i32 %6 to i64
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = tail call i32 @memcpy(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
br label %29
29: ; preds = %11, %19
%30 = phi i32 [ 0, %19 ], [ -1111111111, %11 ]
ret i32 %30
}