#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 *strcat(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ xrealloc(char *, size_t);
char *append(char *dest, size_t *size_dest, const char *src) {
  size_t space_left = *size_dest - strlen(dest) - 1;
  if (strlen(src) > space_left) {
    *size_dest = strlen(dest) + strlen(src) + 1;
    xrealloc(dest, *size_dest);
  }
  return strcat(dest, src);
}


x86 assembly (clang, -O0):
<append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
cltq
cmp    -0x20(%rbp),%rax
jbe    11ba <append+0x8a>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
movslq %eax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <xrealloc@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    (%rsi),%r12
call   1030 <strlen@plt>
not    %eax
movslq %eax,%rbx
add    %r12,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jae    1188 <append+0x58>
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %r12d,%eax
add    $0x1,%eax
movslq %eax,%rsi
mov    %rsi,(%r15)
mov    %r13,%rdi
call   1040 <xrealloc@plt>
mov    %r13,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <strcat@plt>

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

LLVM IR (-O2):
define i8* @append(i8* returned %0, i64* nocapture %1, i8* %2) {
%4 = load i64, i64* %1
%5 = tail call i32 @strlen(i8* %0)
%6 = xor i32 %5, -1
%7 = sext i32 %6 to i64
%8 = add i64 %4, %7
%9 = tail call i32 @strlen(i8* %2)
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %8, %10
br i1 %11, label %12, label %19
12: ; preds = %3
%13 = tail call i32 @strlen(i8* %0)
%14 = tail call i32 @strlen(i8* %2)
%15 = add i32 %13, 1
%16 = add i32 %15, %14
%17 = sext i32 %16 to i64
store i64 %17, i64* %1
%18 = tail call i32 @xrealloc(i8* %0, i64 %17)
br label %19
19: ; preds = %12, %3
%20 = tail call i8* @strcat(i8* dereferenceable(1) %0, i8* dereferenceable(1) %2)
ret i8* %0
}