#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*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *y_string_append(char *string, char *append) {
  int size = strlen(string) + strlen(append) + 1;
  char *new_string = (char *)realloc(string, size);
  if (new_string == NULL) {
    new_string = (char *)malloc(sizeof(char) * (size));
    strcpy(new_string, string);
    if (string) {
      free(string);
      string = NULL;
    };
  }
  strcat(new_string, append);
  return new_string;
}


x86 assembly (clang, -O0):
<y_string_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ee <y_string_append+0x8e>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
cmpq   $0x0,-0x8(%rbp)
je     11e9 <y_string_append+0x89>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ee <y_string_append+0x8e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<y_string_append>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x1,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b6 <y_string_append+0x56>
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
test   %r15,%r15
je     11b6 <y_string_append+0x56>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @y_string_append(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %8, %10
%12 = add nsw i32 %11, 1
store i32 %12, i32* %5
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = call i64 @realloc(i8* %13, i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %35
19: ; preds = %2
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = load i8*, i8** %3
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %3
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %19
%32 = load i8*, i8** %3
%33 = call i32 @free(i8* %32)
store i8* null, i8** %3
br label %34
34: ; preds = %31, %19
br label %35
35: ; preds = %34, %2
%36 = load i8*, i8** %6
%37 = load i8*, i8** %4
%38 = call i32 @strcat(i8* %36, i8* %37)
%39 = load i8*, i8** %6
ret i8* %39
}

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