#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 */
char *realloc(char *, size_t);
int /*<<< orphan*/ strcpy(char *const, char const *);
size_t strlen(char const *);
int strappend(char **str, size_t *len, const char suffix[]) {
  const size_t suffix_len = strlen(suffix);
  char *const new = realloc(*str, *len + suffix_len + 1);
  if (new == NULL) {
    return 1;
  }
  strcpy(new + *len, suffix);
  *str = new;
  *len += suffix_len;
  return 0;
}


x86 assembly (clang, -O0):
<strappend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1187 <strappend+0x57>
movl   $0x1,-0x4(%rbp)
jmp    11bb <strappend+0x8b>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strappend>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r14
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    (%r14),%rdi
mov    0x0(%r13),%rax
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     1183 <strappend+0x53>
mov    %rax,%rbx
mov    0x0(%r13),%rdi
add    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,(%r14)
add    %r12,0x0(%r13)
xor    %eax,%eax
jmp    1188 <strappend+0x58>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @strappend(i8** %0, i64* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i8** %0, i8*** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %8
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
%14 = load i64*, i64** %6
%15 = load i64, i64* %14
%16 = load i64, i64* %8
%17 = add i64 %15, %16
%18 = add i64 %17, 1
%19 = call i8* @realloc(i8* %13, i64 %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %3
store i32 1, i32* %4
br label %36
23: ; preds = %3
%24 = load i8*, i8** %9
%25 = load i64*, i64** %6
%26 = load i64, i64* %25
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8*, i8** %7
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %9
%31 = load i8**, i8*** %5
store i8* %30, i8** %31
%32 = load i64, i64* %8
%33 = load i64*, i64** %6
%34 = load i64, i64* %33
%35 = add i64 %34, %32
store i64 %35, i64* %33
store i32 0, i32* %4
br label %36
36: ; preds = %23, %22
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @strappend(i8** nocapture %0, i64* nocapture %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%5 = load i8*, i8** %0
%6 = load i64, i64* %1
%7 = add i64 %4, 1
%8 = add i64 %7, %6
%9 = tail call i8* @realloc(i8* %5, i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = load i64, i64* %1
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = tail call i32 @strcpy(i8* %13, i8* %2)
store i8* %9, i8** %0
%15 = load i64, i64* %1
%16 = add i64 %15, %4
store i64 %16, i64* %1
br label %17
17: ; preds = %3, %11
%18 = phi i32 [ 0, %11 ], [ 1, %3 ]
ret i32 %18
}