#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(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ printf(char *);
int strlen(char const *);
char *concat(const char *a, const char *b) {
  int la = strlen(a);
  int lb = strlen(b);
  char *tmp = malloc(la + lb + 1);
  if (!tmp) {
    printf("out of memory.\n");
    return NULL;
  }
  memcpy(tmp, a, la);
  memcpy(&tmp[la], b, lb);
  tmp[la + lb] = '\0';
  return tmp;
}


x86 assembly (clang, -O0):
<concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
add    -0x20(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119e <concat+0x5e>
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11de <concat+0x9e>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
lea    (%r12,%r13,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a9 <concat+0x69>
mov    %r12d,%ebp
add    %r13d,%ebp
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>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11b5 <concat+0x75>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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