#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 */
int /*<<< orphan*/ cctools_fatal(char *);
char *realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char const *);
size_t strlen(char const *);
char *string_combine(char *a, const char *b) {
  char *r = NULL;
  size_t a_len = strlen(a);
  if (a && b) {
    r = realloc(a, (a_len + strlen(b) + 1) * sizeof(char));
  }
  if (r)
    strcat(r, b);
  else
    cctools_fatal("Cannot allocate memory for string concatenation.\n");
  return r;
}


x86 assembly (clang, -O0):
<string_combine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11b0 <string_combine+0x70>
cmpq   $0x0,-0x10(%rbp)
je     11b0 <string_combine+0x70>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
shl    $0x0,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cd <string_combine+0x8d>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
jmp    11d9 <string_combine+0x99>
lea    0xe2c(%rip),%rdi
call   1040 <cctools_fatal@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_combine>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     118a <string_combine+0x4a>
mov    %rsi,%r14
test   %rsi,%rsi
je     118a <string_combine+0x4a>
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
je     118a <string_combine+0x4a>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
jmp    1198 <string_combine+0x58>
lea    0xe6f(%rip),%rdi
call   1040 <cctools_fatal@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Cannot allocate memory for string concatenation.\0A\00"
define i8* @string_combine(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %5
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %6
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %23
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i8*, i8** %3
%16 = load i64, i64* %6
%17 = load i8*, i8** %4
%18 = call i64 @strlen(i8* %17)
%19 = add i64 %16, %18
%20 = add i64 %19, 1
%21 = mul i64 %20, 1
%22 = call i8* @realloc(i8* %15, i64 %21)
store i8* %22, i8** %5
br label %23
23: ; preds = %14, %11, %2
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i8*, i8** %5
%28 = load i8*, i8** %4
%29 = call i32 @strcat(i8* %27, i8* %28)
br label %32
30: ; preds = %23
%31 = call i32 @cctools_fatal(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
@.str = [50 x i8] c"Cannot allocate memory for string concatenation.\0A\00"
define i8* @string_combine(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %15
6: ; preds = %2
%7 = tail call i64 @strlen(i8* %0)
%8 = tail call i64 @strlen(i8* %1)
%9 = add i64 %7, 1
%10 = add i64 %9, %8
%11 = tail call i8* @realloc(i8* %0, i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %6
%14 = tail call i32 @strcat(i8* %11, i8* %1)
br label %17
15: ; preds = %2, %6
%16 = tail call i32 @cctools_fatal(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
%18 = phi i8* [ null, %15 ], [ %11, %13 ]
ret i8* %18
}