#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(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *concat_str(const char *str1, const char *str2) {
  size_t str1_size = strlen(str1);
  size_t str2_size = strlen(str2);
  char *str3 = (char *)malloc(str1_size + str2_size + 1);
  if (str3 == NULL) {
    return NULL;
  }
  memcpy(str3, str1, str1_size);
  memcpy(str3 + str1_size, str2, str2_size);
  str3[str1_size + str2_size] = 0;
  return str3;
}


x86 assembly (clang, -O0):
<concat_str>:
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    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1187 <concat_str+0x57>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <concat_str+0x95>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat_str>:
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,%r12
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r12,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1194 <concat_str+0x64>
mov    %rax,%rbx
mov    %r13,%rbp
add    %r12,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
add    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%rbp,1)
jmp    1196 <concat_str+0x66>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @concat_str(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %6
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %7
%13 = load i64, i64* %6
%14 = load i64, i64* %7
%15 = add i64 %13, %14
%16 = add i64 %15, 1
%17 = call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %2
store i8* null, i8** %3
br label %39
22: ; preds = %2
%23 = load i8*, i8** %8
%24 = load i8*, i8** %4
%25 = load i64, i64* %6
%26 = call i32 @memcpy(i8* %23, i8* %24, i64 %25)
%27 = load i8*, i8** %8
%28 = load i64, i64* %6
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i8*, i8** %5
%31 = load i64, i64* %7
%32 = call i32 @memcpy(i8* %29, i8* %30, i64 %31)
%33 = load i8*, i8** %8
%34 = load i64, i64* %6
%35 = load i64, i64* %7
%36 = add i64 %34, %35
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %8
store i8* %38, i8** %3
br label %39
39: ; preds = %22, %21
%40 = load i8*, i8** %3
ret i8* %40
}

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