#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*/ assert(char *);
int /*<<< orphan*/ memcpy(char *, char *, unsigned int);
char *strings_alloc(unsigned int);
unsigned int strings_length(char *);
char *strings_concat(char *string1, char *string2) {
  assert(string1);
  assert(string2);
  unsigned int string1_length = strings_length(string1),
               string2_length = strings_length(string2);
  char *ret = strings_alloc(string1_length + string2_length + 1);
  if (string1_length != 0) {
    memcpy(ret, string1, string1_length * sizeof(char));
  }
  if (string2_length != 0) {
    memcpy(&ret[string1_length], string2, string2_length * sizeof(char));
  }
  ret[string1_length + string2_length] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<strings_concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strings_length@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strings_length@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
add    -0x18(%rbp),%edi
add    $0x1,%edi
call   1040 <strings_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ac <strings_concat+0x6c>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
shl    $0x0,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
cmpl   $0x0,-0x18(%rbp)
je     11d2 <strings_concat+0x92>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%eax
shl    $0x0,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    -0x18(%rbp),%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strings_concat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <assert@plt>
mov    %r14,%rdi
call   1050 <assert@plt>
mov    %r12,%rdi
call   1030 <strings_length@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <strings_length@plt>
mov    %eax,%r13d
lea    (%rbx,%r13,1),%edi
add    $0x1,%edi
call   1040 <strings_alloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
je     1193 <strings_concat+0x53>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
lea    (%rbx,%r13,1),%ebp
test   %r13d,%r13d
je     11af <strings_concat+0x6f>
mov    %ebx,%eax
mov    %r15,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %ebp,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strings_concat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i32 @assert(i8* %8)
%10 = load i8*, i8** %4
%11 = call i32 @assert(i8* %10)
%12 = load i8*, i8** %3
%13 = call i32 @strings_length(i8* %12)
store i32 %13, i32* %5
%14 = load i8*, i8** %4
%15 = call i32 @strings_length(i8* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %5
%17 = load i32, i32* %6
%18 = add i32 %16, %17
%19 = add i32 %18, 1
%20 = call i8* @strings_alloc(i32 %19)
store i8* %20, i8** %7
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %2
%24 = load i8*, i8** %7
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = zext i32 %26 to i64
%28 = mul i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i32 @memcpy(i8* %24, i8* %25, i32 %29)
br label %31
31: ; preds = %23, %2
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %45
34: ; preds = %31
%35 = load i8*, i8** %7
%36 = load i32, i32* %5
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8*, i8** %4
%40 = load i32, i32* %6
%41 = zext i32 %40 to i64
%42 = mul i64 %41, 1
%43 = trunc i64 %42 to i32
%44 = call i32 @memcpy(i8* %38, i8* %39, i32 %43)
br label %45
45: ; preds = %34, %31
%46 = load i8*, i8** %7
%47 = load i32, i32* %5
%48 = load i32, i32* %6
%49 = add i32 %47, %48
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %46, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %7
ret i8* %52
}

LLVM IR (-O2):
define i8* @strings_concat(i8* %0, i8* %1) {
%3 = tail call i32 @assert(i8* %0)
%4 = tail call i32 @assert(i8* %1)
%5 = tail call i32 @strings_length(i8* %0)
%6 = tail call i32 @strings_length(i8* %1)
%7 = add i32 %6, %5
%8 = add i32 %7, 1
%9 = tail call i8* @strings_alloc(i32 %8)
%10 = icmp eq i32 %5, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = tail call i32 @memcpy(i8* %9, i8* %0, i32 %5)
br label %13
13: ; preds = %11, %2
%14 = icmp eq i32 %6, 0
br i1 %14, label %19, label %15
15: ; preds = %13
%16 = zext i32 %5 to i64
%17 = getelementptr inbounds i8, i8* %9, i64 %16
%18 = tail call i32 @memcpy(i8* %17, i8* %1, i32 %6)
br label %19
19: ; preds = %15, %13
%20 = zext i32 %7 to i64
%21 = getelementptr inbounds i8, i8* %9, i64 %20
store i8 0, i8* %21
ret i8* %9
}