#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 */
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
char *StringConcat(char *s1, char *s2) {
  int length;
  if (s2 == NULL)
    return s1;
  if (s1 == NULL) {
    s1 = strdup(s2);
  } else {
    length = strlen(s1) + strlen(s2) + 1;
    s1 = (char *)realloc(s1, length);
    strcat(s1, s2);
  }
  return s1;
}


x86 assembly (clang, -O0):
<StringConcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1168 <StringConcat+0x28>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cc <StringConcat+0x8c>
cmpq   $0x0,-0x10(%rbp)
jne    1185 <StringConcat+0x45>
mov    -0x18(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
jmp    11c4 <StringConcat+0x84>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StringConcat>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rsi,%rsi
je     1185 <StringConcat+0x45>
mov    %rsi,%r14
test   %rbx,%rbx
je     118e <StringConcat+0x4e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x1,%esi
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* @StringConcat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i8*, i8** %4
store i8* %10, i8** %3
br label %33
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %4
br label %31
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = load i8*, i8** %5
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %19, %21
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = call i64 @realloc(i8* %24, i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = call i32 @strcat(i8* %28, i8* %29)
br label %31
31: ; preds = %17, %14
%32 = load i8*, i8** %4
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %9
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
define i8* @StringConcat(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %16, 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 %16
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 @strcat(i8* %14, i8* %1)
br label %16
16: ; preds = %6, %8, %2
%17 = phi i8* [ %0, %2 ], [ %7, %6 ], [ %14, %8 ]
ret i8* %17
}