#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__);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
char *e_cat_string(char *p, char *str) {
  if (str == NULL)
    return (p = NULL);
  if (p == NULL) {
    if ((p = malloc(strlen(str) + 2)) == NULL)
      return (NULL);
    p[0] = '\0';
  } else if ((p = realloc((p), (strlen(p) + strlen(str) + 2))) == NULL)
    return (NULL);
  strcat(p, " ");
  strcat(p, str);
  return (p);
}


x86 assembly (clang, -O0):
<e_cat_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1170 <e_cat_string+0x30>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x8(%rbp)
jmp    122e <e_cat_string+0xee>
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <e_cat_string+0x77>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ab <e_cat_string+0x6b>
movq   $0x0,-0x8(%rbp)
jmp    122e <e_cat_string+0xee>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1209 <e_cat_string+0xc9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdi
add    %rax,%rsi
add    $0x2,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1204 <e_cat_string+0xc4>
movq   $0x0,-0x8(%rbp)
jmp    122e <e_cat_string+0xee>
jmp    1209 <e_cat_string+0xc9>
mov    -0x10(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<e_cat_string>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     11ba <e_cat_string+0x7a>
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
je     1182 <e_cat_string+0x42>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x2,%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119e <e_cat_string+0x5e>
jmp    11ba <e_cat_string+0x7a>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x2(%rax),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     11ba <e_cat_string+0x7a>
mov    %rax,%rbx
movb   $0x0,(%rax)
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
jmp    11bc <e_cat_string+0x7c>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
define i8* @e_cat_string(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %2
store i8* null, i8** %4
store i8* null, i8** %3
br label %41
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
%15 = add nsw i64 %14, 2
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %4
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %12
store i8* null, i8** %3
br label %41
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 0, i8* %21
br label %34
22: ; preds = %9
%23 = load i8*, i8** %4
%24 = load i8*, i8** %4
%25 = call i64 @strlen(i8* %24)
%26 = load i8*, i8** %5
%27 = call i64 @strlen(i8* %26)
%28 = add nsw i64 %25, %27
%29 = add nsw i64 %28, 2
%30 = call i8* @realloc(i8* %23, i64 %29)
store i8* %30, i8** %4
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %22
store i8* null, i8** %3
br label %41
33: ; preds = %22
br label %34
34: ; preds = %33, %19
%35 = load i8*, i8** %4
%36 = call i32 @strcat(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = load i8*, i8** %4
%38 = load i8*, i8** %5
%39 = call i32 @strcat(i8* %37, i8* %38)
%40 = load i8*, i8** %4
store i8* %40, i8** %3
br label %41
41: ; preds = %34, %32, %18, %8
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i8* @e_cat_string(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %23, label %4
4: ; preds = %2
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %12
6: ; preds = %4
%7 = tail call i64 @strlen(i8* %1)
%8 = add nsw i64 %7, 2
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %23, label %11
11: ; preds = %6
store i8 0, i8* %9
br label %19
12: ; preds = %4
%13 = tail call i64 @strlen(i8* %0)
%14 = tail call i64 @strlen(i8* %1)
%15 = add i64 %13, 2
%16 = add i64 %15, %14
%17 = tail call i8* @realloc(i8* %0, i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %23, label %19
19: ; preds = %12, %11
%20 = phi i8* [ %9, %11 ], [ %17, %12 ]
%21 = tail call i32 @strcat(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @strcat(i8* %20, i8* %1)
br label %23
23: ; preds = %12, %6, %2, %19
%24 = phi i8* [ %20, %19 ], [ null, %2 ], [ null, %6 ], [ null, %12 ]
ret i8* %24
}