#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*/ assert(char *);
scalar_t__ realloc(char *, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
char *append_str(char *str_buf, char *str) {
  if (str == NULL)
    return str_buf;
  if (str_buf == NULL) {
    str_buf = strdup(str);
    assert(str_buf != NULL);
    return str_buf;
  }
  str_buf = (char *)realloc(str_buf, strlen(str_buf) + strlen(str) + 1);
  assert(str_buf);
  strcat(str_buf, str);
  return str_buf;
}


x86 assembly (clang, -O0):
<append_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1178 <append_str+0x28>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1206 <append_str+0xb6>
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <append_str+0x62>
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
movslq %eax,%rdi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1206 <append_str+0xb6>
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    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@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):
<append_str>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rsi,%rsi
je     11b9 <append_str+0x69>
mov    %rsi,%r14
test   %rbx,%rbx
je     11a0 <append_str+0x50>
mov    %rbx,%rdi
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>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
jmp    11b9 <append_str+0x69>
mov    %r14,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @append_str(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %21, label %4
4: ; preds = %2
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %12
6: ; preds = %4
%7 = tail call i8* @strdup(i8* %1)
%8 = icmp ne i8* %7, null
%9 = zext i1 %8 to i64
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @assert(i8* %10)
br label %21
12: ; preds = %4
%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 i64 @realloc(i8* %0, i64 %16)
%18 = inttoptr i64 %17 to i8*
%19 = tail call i32 @assert(i8* %18)
%20 = tail call i32 @strcat(i8* %18, i8* %1)
br label %21
21: ; preds = %2, %12, %6
%22 = phi i8* [ %7, %6 ], [ %18, %12 ], [ %0, %2 ]
ret i8* %22
}