#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 */
char *realloc(char *, size_t);
char *strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
char *concat(char *buffer, char *string) {
  if (!buffer) {
    return strdup(string);
  }
  if (string) {
    size_t length = strlen(buffer) + strlen(string) + 1;
    char *new = realloc(buffer, length);
    return strcat(new, string);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <concat+0x2d>
mov    -0x18(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11cc <concat+0x8c>
cmpq   $0x0,-0x18(%rbp)
je     11c4 <concat+0x84>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    %rax,-0x8(%rbp)
jmp    11cc <concat+0x8c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   %rdi,%rdi
je     1190 <concat+0x50>
mov    %rdi,%rbx
test   %r14,%r14
je     1187 <concat+0x47>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %r15d,%eax
add    $0x1,%eax
movslq %eax,%rsi
mov    %rbx,%rdi
call   1040 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <strdup@plt>

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

LLVM IR (-O2):
define i8* @concat(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @strdup(i8* %1)
br label %16
6: ; preds = %2
%7 = icmp eq i8* %1, null
br i1 %7, label %16, label %8
8: ; preds = %6
%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 = sext i32 %12 to i64
%14 = tail call i8* @realloc(i8* %0, i64 %13)
%15 = tail call i8* @strcat(i8* dereferenceable(1) %14, i8* %1)
br label %16
16: ; preds = %6, %8, %4
%17 = phi i8* [ %14, %8 ], [ %5, %4 ], [ %0, %6 ]
ret i8* %17
}