#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 *malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *string_concat(const char *string1, const char *string2) {
  if (string1 == NULL || string2 == NULL) {
    return NULL;
  }
  char *result = malloc(strlen(string1) + strlen(string2) + 1);
  if (result == NULL) {
    return NULL;
  }
  strcpy(result, string1);
  strcat(result, string2);
  return result;
}


x86 assembly (clang, -O0):
<string_concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1166 <string_concat+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1173 <string_concat+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <string_concat+0x97>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <string_concat+0x75>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <string_concat+0x97>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_concat>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     1196 <string_concat+0x56>
mov    %rsi,%r14
test   %rsi,%rsi
je     1196 <string_concat+0x56>
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1194 <string_concat+0x54>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
jmp    1196 <string_concat+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @string_concat(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
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i8* null, i8** %3
br label %32
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %15, %17
%19 = add nsw i64 %18, 1
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %13
store i8* null, i8** %3
br label %32
24: ; preds = %13
%25 = load i8*, i8** %6
%26 = load i8*, i8** %4
%27 = call i32 @strcpy(i8* %25, i8* %26)
%28 = load i8*, i8** %6
%29 = load i8*, i8** %5
%30 = call i32 @strcat(i8* %28, i8* %29)
%31 = load i8*, i8** %6
store i8* %31, i8** %3
br label %32
32: ; preds = %24, %23, %12
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
define i8* @string_concat(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %16, label %6
6: ; preds = %2
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = add i64 %7, 1
%10 = add i64 %9, %8
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %6
%14 = tail call i32 @strcpy(i8* %11, i8* %0)
%15 = tail call i32 @strcat(i8* %11, i8* %1)
br label %16
16: ; preds = %13, %6, %2
%17 = phi i8* [ null, %2 ], [ %11, %13 ], [ null, %6 ]
ret i8* %17
}