#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 */
scalar_t__ _talloc_realloc_array(void const *, char *, int, size_t, char *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int strlen(char const *);
char *talloc_append_string(const void *t, char *orig, const char *append) {
  char *ret;
  size_t olen = strlen(orig);
  size_t alenz;
  if (!append)
    return orig;
  alenz = strlen(append) + 1;
  ret = (char *)_talloc_realloc_array(t, orig, sizeof(char), olen + alenz,
                                      "char");
  if (!ret)
    return NULL;
  memcpy(&ret[olen], append, alenz);
  return ret;
}


x86 assembly (clang, -O0):
<talloc_append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116b <talloc_append_string+0x3b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <talloc_append_string+0xa7>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rcx
add    -0x38(%rbp),%rcx
mov    $0x1,%edx
lea    0xe67(%rip),%r8
call   1050 <_talloc_realloc_array@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ba <talloc_append_string+0x8a>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <talloc_append_string+0xa7>
mov    -0x28(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<talloc_append_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r13
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %r14,%r14
je     119d <talloc_append_string+0x6d>
movslq %eax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
movslq %eax,%r12
lea    (%r12,%r15,1),%rcx
add    $0x1,%rcx
lea    0xe94(%rip),%r8
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <_talloc_realloc_array@plt>
test   %rax,%rax
je     119b <talloc_append_string+0x6b>
mov    %rax,%rbx
add    $0x1,%r12
add    %rax,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
jmp    119d <talloc_append_string+0x6d>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"char\00"
define i8* @talloc_append_string(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = sext i32 %4 to i64
%6 = icmp eq i8* %2, null
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = tail call i32 @strlen(i8* %2)
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = add nsw i64 %10, %5
%12 = tail call i64 @_talloc_realloc_array(i8* %0, i8* %1, i32 1, i64 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %18, label %15
15: ; preds = %7
%16 = getelementptr inbounds i8, i8* %13, i64 %5
%17 = tail call i32 @memcpy(i8* %16, i8* %2, i64 %10)
br label %18
18: ; preds = %7, %3, %15
%19 = phi i8* [ %13, %15 ], [ %1, %3 ], [ null, %7 ]
ret i8* %19
}