#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 const *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
scalar_t__ strlen(char const *);
char *strjoin(const char *str1, const char *str2) {
  if (!str1 && !str2)
    return NULL;
  if (!str2)
    return strdup(str1);
  if (!str1)
    return strdup(str2);
  char *s = malloc(strlen(str1) + strlen(str2) + 1);
  strcpy(s, str1);
  strcat(s, str2);
  return s;
}


x86 assembly (clang, -O0):
<strjoin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <strjoin+0x33>
cmpq   $0x0,-0x18(%rbp)
jne    1183 <strjoin+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1209 <strjoin+0xb9>
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <strjoin+0x50>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1209 <strjoin+0xb9>
cmpq   $0x0,-0x10(%rbp)
jne    11bd <strjoin+0x6d>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1209 <strjoin+0xb9>
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):
<strjoin>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rax
or     %rsi,%rax
je     11a8 <strjoin+0x58>
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rsi,%rsi
je     11b4 <strjoin+0x64>
test   %rbx,%rbx
je     11b9 <strjoin+0x69>
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>
jmp    11ab <strjoin+0x5b>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
jmp    11bc <strjoin+0x6c>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strdup@plt>

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

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