#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(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *concat(char const *s1, size_t len1, char const *s2) {
  size_t len2 = strlen(s2);
  char *mem = malloc(len1 + len2 + 1);
  if (mem != NULL) {
    memcpy(mem, s1, len1);
    memcpy(mem + len1, s2, len2 + 1);
  }
  return mem;
}


x86 assembly (clang, -O0):
<concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     119b <concat+0x6b>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
lea    (%rax,%r15,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1185 <concat+0x55>
add    $0x1,%r12
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
add    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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