#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*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ realloc(char *, int);
char *strdup(char const *const);
int strlen(char const *const);
int /*<<< orphan*/ strncpy(char *, char const *const, int);
char *strappend(char *original, const char *const appendage) {
  char *endResult = 0;
  if (!appendage)
    return (original ? original : 0);
  if (!original)
    return strdup(appendage);
  endResult =
      (char *)realloc(original, strlen(original) + strlen(appendage) + 1);
  memset(endResult + strlen(endResult), 0, strlen(appendage) + 1);
  strncpy(endResult + strlen(endResult), appendage, strlen(appendage));
  return endResult;
}


x86 assembly (clang, -O0):
<strappend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a3 <strappend+0x53>
cmpq   $0x0,-0x10(%rbp)
je     118b <strappend+0x3b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1196 <strappend+0x46>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    1196 <strappend+0x46>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1275 <strappend+0x125>
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <strappend+0x70>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1275 <strappend+0x125>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%esi
mov    -0x58(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strappend>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rsi,%rsi
je     11d0 <strappend+0x80>
mov    %rsi,%r14
test   %rbx,%rbx
je     11d9 <strappend+0x89>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>

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

LLVM IR (-O2):
define i8* @strappend(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %26, label %4
4: ; preds = %2
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i8* @strdup(i8* %1)
br label %26
8: ; preds = %4
%9 = tail call i32 @strlen(i8* %0)
%10 = tail call i32 @strlen(i8* %1)
%11 = add i32 %9, 1
%12 = add i32 %11, %10
%13 = tail call i64 @realloc(i8* %0, i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = tail call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = tail call i32 @strlen(i8* %1)
%19 = add nsw i32 %18, 1
%20 = tail call i32 @memset(i8* %17, i32 0, i32 %19)
%21 = tail call i32 @strlen(i8* %14)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %14, i64 %22
%24 = tail call i32 @strlen(i8* %1)
%25 = tail call i32 @strncpy(i8* %23, i8* %1, i32 %24)
br label %26
26: ; preds = %2, %8, %6
%27 = phi i8* [ %14, %8 ], [ %7, %6 ], [ %0, %2 ]
ret i8* %27
}