#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*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *add_str(char *str1, const char *str2) {
  int l1, l2;
  char *ctmp;
  if (str1)
    l1 = strlen(str1);
  else
    l1 = 0;
  l2 = strlen(str2);
  ctmp = (char *)malloc(sizeof(char) * (l1 + l2 + 1));
  if (!ctmp) {
    printf("out of memory\n");
    exit(-1);
  }
  if (str1)
    strcpy(ctmp, str1);
  strcpy(&ctmp[l1], str2);
  return ctmp;
}


x86 assembly (clang, -O0):
<add_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     117c <add_str+0x2c>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
jmp    1183 <add_str+0x33>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ca <add_str+0x7a>
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     11e2 <add_str+0x92>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
test   %rdi,%rdi
je     1170 <add_str+0x20>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
jmp    1173 <add_str+0x23>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11bf <add_str+0x6f>
mov    %rax,%rbx
test   %r12,%r12
je     119f <add_str+0x4f>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
movslq %r15d,%rax
mov    %rbx,%rdi
add    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"out of memory\0A\00"
define i8* @add_str(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 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %5
br label %14
13: ; preds = %2
store i32 0, i32* %5
br label %14
14: ; preds = %13, %10
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = add nsw i32 %17, %18
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = icmp ne i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %14
%29 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %14
%32 = load i8*, i8** %3
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i8*, i8** %7
%36 = load i8*, i8** %3
%37 = call i32 @strcpy(i8* %35, i8* %36)
br label %38
38: ; preds = %34, %31
%39 = load i8*, i8** %7
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8*, i8** %4
%44 = call i32 @strcpy(i8* %42, i8* %43)
%45 = load i8*, i8** %7
ret i8* %45
}

LLVM IR (-O2):
@str = [14 x i8] c"out of memory\00"
define i8* @add_str(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %0)
br label %6
6: ; preds = %2, %4
%7 = phi i32 [ %5, %4 ], [ 0, %2 ]
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %7, 1
%10 = add i32 %9, %8
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %17
14: ; preds = %6
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %6
br i1 %3, label %20, label %18
18: ; preds = %17
%19 = tail call i32 @strcpy(i8* %12, i8* %0)
br label %20
20: ; preds = %18, %17
%21 = sext i32 %7 to i64
%22 = getelementptr inbounds i8, i8* %12, i64 %21
%23 = tail call i32 @strcpy(i8* %22, i8* %1)
ret i8* %12
}