#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t const);
char *strappend(char *c1, char *c2) {
  char *result;
  if (c1 == NULL) {
    const size_t len = strlen(c2);
    result = (char *)malloc(sizeof(char) * (len + 1));
    strcpy(result, c2);
    result[len] = '\0';
  } else {
    const size_t len1 = strlen(c1);
    const size_t len2 = strlen(c2);
    const size_t len = 1 + len1 + len2;
    result = (char *)malloc(sizeof(char) * (len + 1));
    strcpy(result, c1);
    result[len1] = ' ';
    strncpy(result + len1 + 1, c2, len2 + 1);
    result[len] = '\0';
    free(c1);
  }
  return result;
}


x86 assembly (clang, -O0):
<strappend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11ad <strappend+0x5d>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1239 <strappend+0xe9>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x20,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%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
test   %rdi,%rdi
je     11be <strappend+0x6e>
mov    %rdi,%r15
call   1060 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rax,%r12,1),%rbp
add    $0x1,%rbp
lea    0x1(%rax),%r13
lea    0x1(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
movb   $0x20,(%rbx,%r12,1)
lea    (%rbx,%r12,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <strncpy@plt>
movb   $0x0,(%rbx,%rbp,1)
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11e4 <strappend+0x94>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
movb   $0x0,(%rbx,%r15,1)
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 i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %27
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = add i64 %15, 1
%17 = mul i64 1, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = load i8*, i8** %4
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 0, i8* %26
br label %61
27: ; preds = %2
%28 = load i8*, i8** %3
%29 = call i64 @strlen(i8* %28)
store i64 %29, i64* %7
%30 = load i8*, i8** %4
%31 = call i64 @strlen(i8* %30)
store i64 %31, i64* %8
%32 = load i64, i64* %7
%33 = add i64 1, %32
%34 = load i64, i64* %8
%35 = add i64 %33, %34
store i64 %35, i64* %9
%36 = load i64, i64* %9
%37 = add i64 %36, 1
%38 = mul i64 1, %37
%39 = trunc i64 %38 to i32
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %5
%42 = load i8*, i8** %5
%43 = load i8*, i8** %3
%44 = call i32 @strcpy(i8* %42, i8* %43)
%45 = load i8*, i8** %5
%46 = load i64, i64* %7
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 32, i8* %47
%48 = load i8*, i8** %5
%49 = load i64, i64* %7
%50 = getelementptr inbounds i8, i8* %48, i64 %49
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = load i8*, i8** %4
%53 = load i64, i64* %8
%54 = add i64 %53, 1
%55 = call i32 @strncpy(i8* %51, i8* %52, i64 %54)
%56 = load i8*, i8** %5
%57 = load i64, i64* %9
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %3
%60 = call i32 @free(i8* %59)
br label %61
61: ; preds = %27, %12
%62 = load i8*, i8** %5
ret i8* %62
}

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