#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 const *, size_t);
char *realloc(char *, size_t);
size_t strlen(char *);
__attribute__((malloc)) char *string_append(char *dest, const char *src,
                                            size_t len) {
  size_t destlen = dest != NULL ? strlen(dest) : 0;
  dest = realloc(dest, destlen + len + 1);
  memcpy(dest + destlen, src, len);
  dest[destlen + len] = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<string_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1161 <string_append+0x31>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
jmp    116c <string_append+0x3c>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    116c <string_append+0x3c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_append>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
test   %rdi,%rdi
je     1154 <string_append+0x24>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
jmp    1156 <string_append+0x26>
xor    %ebx,%ebx
lea    (%rbx,%r15,1),%r13
lea    (%rbx,%r15,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,%r12
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%r13,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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