#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__ malloc(int);
int /*<<< orphan*/ memError();
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ strlen(char *);
char *strconcat(char *str1, char *str2) {
  int str1Len = (int)strlen(str1);
  int str2Len = (int)strlen(str2);
  int resLen = str1Len + str2Len + 1;
  char *result = (char *)malloc(sizeof(char) * resLen);
  if (result == NULL)
    memError();
  memcpy(result, str1, str1Len);
  memcpy(result + str1Len, str2, str2Len);
  *(result + resLen - 1) = '\0';
  return result;
}


x86 assembly (clang, -O0):
<strconcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1199 <strconcat+0x59>
mov    $0x0,%al
call   1040 <memError@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strconcat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
lea    (%rax,%r13,1),%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1181 <strconcat+0x41>
xor    %eax,%eax
call   1040 <memError@plt>
movslq %ebp,%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
movslq %r13d,%rax
mov    %rbx,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
movb   $0x0,-0x1(%rbx,%rbp,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* @strconcat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %5
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
%14 = trunc i64 %13 to i32
store i32 %14, i32* %6
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = add nsw i32 %15, %16
%18 = add nsw i32 %17, 1
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = mul i64 1, %20
%22 = trunc i64 %21 to i32
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = call i32 (...) @memError()
br label %29
29: ; preds = %27, %2
%30 = load i8*, i8** %8
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = call i32 @memcpy(i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %8
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8*, i8** %4
%39 = load i32, i32* %6
%40 = call i32 @memcpy(i8* %37, i8* %38, i32 %39)
%41 = load i8*, i8** %8
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = getelementptr inbounds i8, i8* %44, i64 -1
store i8 0, i8* %45
%46 = load i8*, i8** %8
ret i8* %46
}

LLVM IR (-O2):
define i8* @strconcat(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = trunc i64 %3 to i32
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = trunc i64 %5 to i32
%7 = add i32 %4, 1
%8 = add i32 %7, %6
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 (...) @memError()
br label %14
14: ; preds = %12, %2
%15 = sext i32 %8 to i64
%16 = tail call i32 @memcpy(i8* %10, i8* %0, i32 %4)
%17 = shl i64 %3, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %10, i64 %18
%20 = tail call i32 @memcpy(i8* %19, i8* %1, i32 %6)
%21 = getelementptr inbounds i8, i8* %10, i64 %15
%22 = getelementptr inbounds i8, i8* %21, i64 -1
store i8 0, i8* %22
ret i8* %10
}