#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(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
char *putil_strconcat(char *a, char *b) {
  if (!b) {
    return strdup(a);
  } else if (!a) {
    return strdup(b);
  } else {
    char *res = malloc(strlen(a) + strlen(b) + 1);
    strcpy(res, a);
    strcat(res, b);
    return res;
  }
}


x86 assembly (clang, -O0):
<putil_strconcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117d <putil_strconcat+0x2d>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11e6 <putil_strconcat+0x96>
cmpq   $0x0,-0x10(%rbp)
jne    119a <putil_strconcat+0x4a>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11e6 <putil_strconcat+0x96>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putil_strconcat>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rsi,%rsi
je     11a7 <putil_strconcat+0x57>
mov    %rsi,%r14
test   %rbx,%rbx
je     11ac <putil_strconcat+0x5c>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
jmp    11af <putil_strconcat+0x5f>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>

LLVM IR (-O0):
define i8* @putil_strconcat(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** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %3
br label %33
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %3
br label %33
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = call i64 @strlen(i8* %19)
%21 = load i8*, i8** %5
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 %20, %22
%24 = add nsw i64 %23, 1
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = load i8*, i8** %4
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %6
%30 = load i8*, i8** %5
%31 = call i32 @strcat(i8* %29, i8* %30)
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %18, %15, %9
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
define i8* @putil_strconcat(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i8* @strdup(i8* %0)
br label %18
6: ; preds = %2
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i8* @strdup(i8* %1)
br label %18
10: ; preds = %6
%11 = tail call i64 @strlen(i8* %0)
%12 = tail call i64 @strlen(i8* %1)
%13 = add i64 %11, 1
%14 = add i64 %13, %12
%15 = tail call i8* @malloc(i64 %14)
%16 = tail call i32 @strcpy(i8* %15, i8* %0)
%17 = tail call i32 @strcat(i8* %15, i8* %1)
br label %18
18: ; preds = %10, %8, %4
%19 = phi i8* [ %15, %10 ], [ %9, %8 ], [ %5, %4 ]
ret i8* %19
}