#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*/ memcpy(char *, char const *, int);
scalar_t__ safe_realloc(char *, int);
char *safe_strdup(char const *);
int strlen(char const *);
char *safe_append_realloc(char *buf, const char *str) {
  char *newbuf;
  (void)(str != NULL);
  if (buf == NULL)
    return (safe_strdup(str));
  newbuf = (char *)safe_realloc(buf, strlen(buf) + strlen(str) + 1);
  memcpy(&newbuf[strlen(newbuf)], str, strlen(str) + 1);
  return (newbuf);
}


x86 assembly (clang, -O0):
<safe_append_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <safe_append_realloc+0x2d>
mov    -0x18(%rbp),%rdi
call   1060 <safe_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11eb <safe_append_realloc+0xab>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
call   1050 <safe_realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_append_realloc>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   %rdi,%rdi
je     119f <safe_append_realloc+0x5f>
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1050 <safe_realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
add    %r15,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <safe_strdup@plt>

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

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