#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);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *concat(char *a, char *b) {
  if (a == NULL || b == NULL) {
    return a;
  }
  char *concat = malloc((strlen(a) + strlen(b) + 1) * sizeof(char));
  strncpy(concat, a, strlen(a));
  strncpy(concat + strlen(a), b, strlen(b));
  concat[strlen(a) + strlen(b)] = '\0';
  return concat;
}


x86 assembly (clang, -O0):
<concat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <concat+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <concat+0x33>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1237 <concat+0x107>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rcx
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
add    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concat>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     11b8 <concat+0x88>
mov    %rsi,%r14
test   %rsi,%rsi
je     11b8 <concat+0x88>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r12,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r15,1),%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %rbx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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