#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 *, int);
char *realloc(char *, int);
int strlen(char const *);
char *strappend(char *dst, const char *src) {
  int src_len = strlen(src);
  int dst_len = dst ? strlen(dst) : 0;
  dst = realloc(dst, dst_len + src_len + 1);
  memcpy(dst + dst_len, src, src_len + 1);
  return dst;
}


x86 assembly (clang, -O0):
<strappend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1168 <strappend+0x38>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
jmp    1172 <strappend+0x42>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    1172 <strappend+0x42>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    -0x14(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strappend>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
test   %r12,%r12
je     115e <strappend+0x2e>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
jmp    1161 <strappend+0x31>
xor    %r13d,%r13d
mov    %r15d,%ebp
add    $0x1,%ebp
lea    (%r15,%r13,1),%esi
add    $0x1,%esi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
movslq %r13d,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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