#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*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strAppend(char *dest, const char *src) {
  if (dest == NULL) {
    dest = (char *)malloc(sizeof(char) * (strlen(src) + 1));
    if (dest == NULL) {
      fputs("Memory allocation error\n", stderr);
      exit(1);
    }
    strcpy(dest, src);
  } else {
    dest =
        (char *)realloc(dest, sizeof(char) * (strlen(src) + strlen(dest) + 1));
    if (dest == NULL) {
      fputs("Memory allocation error\n", stderr);
      exit(1);
    }
    strcat(dest, src);
  }
  return dest;
}


x86 assembly (clang, -O0):
<strAppend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e4 <strAppend+0x74>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d2 <strAppend+0x62>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1257 <strAppend+0xe7>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    124a <strAppend+0xda>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
lea    0xdc5(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strAppend>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
test   %rbx,%rbx
je     11b7 <strAppend+0x47>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11df <strAppend+0x6f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
jmp    11d6 <strAppend+0x66>
add    $0x1,%r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11df <strAppend+0x6f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    0xe11(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Memory allocation error\0A\00"
@stderr = global i32 0
define i8* @strAppend(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %26
7: ; preds = %2
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = mul i64 1, %11
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %7
%19 = load i32, i32* @stderr
%20 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %7
%23 = load i8*, i8** %3
%24 = load i8*, i8** %4
%25 = call i32 @strcpy(i8* %23, i8* %24)
br label %49
26: ; preds = %2
%27 = load i8*, i8** %3
%28 = load i8*, i8** %4
%29 = call i32 @strlen(i8* %28)
%30 = load i8*, i8** %3
%31 = call i32 @strlen(i8* %30)
%32 = add nsw i32 %29, %31
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = mul i64 1, %34
%36 = trunc i64 %35 to i32
%37 = call i64 @realloc(i8* %27, i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %3
%39 = load i8*, i8** %3
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %45
41: ; preds = %26
%42 = load i32, i32* @stderr
%43 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %26
%46 = load i8*, i8** %3
%47 = load i8*, i8** %4
%48 = call i32 @strcat(i8* %46, i8* %47)
br label %49
49: ; preds = %45, %22
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [25 x i8] c"Memory allocation error\0A\00"
@stderr = global i32 0
define i8* @strAppend(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = tail call i32 @strlen(i8* %1)
br i1 %3, label %5, label %16
5: ; preds = %2
%6 = add nsw i32 %4, 1
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %5
%15 = tail call i32 @strcpy(i8* %8, i8* %1)
br label %29
16: ; preds = %2
%17 = tail call i32 @strlen(i8* %0)
%18 = add i32 %4, 1
%19 = add i32 %18, %17
%20 = tail call i64 @realloc(i8* %0, i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = tail call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = tail call i32 @strcat(i8* %21, i8* %1)
br label %29
29: ; preds = %27, %14
%30 = phi i8* [ %8, %14 ], [ %21, %27 ]
ret i8* %30
}