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


x86 assembly (clang, -O0):
<talloc_append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cltq
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <talloc_append_string+0x37>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f0 <talloc_append_string+0xb0>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rcx
add    -0x30(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1,%edx
lea    0xe5b(%rip),%r8
call   1050 <_talloc_realloc_array@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c6 <talloc_append_string+0x86>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <talloc_append_string+0xb0>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <_talloc_set_name_const@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<talloc_append_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %r15,%r15
je     11b5 <talloc_append_string+0x75>
movslq %eax,%r14
mov    %r15,%rdi
call   1030 <strlen@plt>
movslq %eax,%r12
lea    (%r12,%r14,1),%rcx
add    $0x1,%rcx
lea    0xe8a(%rip),%r8
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <_talloc_realloc_array@plt>
mov    $0x0,%ebx
test   %rax,%rax
je     11b5 <talloc_append_string+0x75>
mov    %rax,%r13
add    $0x1,%r12
add    %rax,%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %r13,%rdi
mov    %r13,%rsi
call   1060 <_talloc_set_name_const@plt>
mov    %r13,%rbx
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) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
store i64 %11, i64* %7
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
store i8* %15, i8** %3
br label %41
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
store i64 %20, i64* %8
%21 = load i8*, i8** %4
%22 = load i64, i64* %7
%23 = load i64, i64* %8
%24 = add i64 %22, %23
%25 = call i64 @_talloc_realloc_array(i32* null, i8* %21, i32 1, i64 %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %16
store i8* null, i8** %3
br label %41
30: ; preds = %16
%31 = load i8*, i8** %6
%32 = load i64, i64* %7
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i8*, i8** %5
%35 = load i64, i64* %8
%36 = call i32 @memcpy(i8* %33, i8* %34, i64 %35)
%37 = load i8*, i8** %6
%38 = load i8*, i8** %6
%39 = call i32 @_talloc_set_name_const(i8* %37, i8* %38)
%40 = load i8*, i8** %6
store i8* %40, i8** %3
br label %41
41: ; preds = %30, %29, %14
%42 = load i8*, i8** %3
ret i8* %42
}

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