#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);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *new_string_buffer(const char *old_buffer, const char *append_buffer,
                        int append_len) {
  char *ret_buffer = 0;
  int ret_len = 0;
  int old_len = 0;
  if (!append_len) {
    return (char *)old_buffer;
  }
  if (old_buffer) {
    old_len = strlen(old_buffer);
  }
  ret_len = old_len + append_len + 1;
  ret_buffer = (char *)malloc(ret_len);
  if (old_len > 0) {
    memcpy(ret_buffer, old_buffer, old_len);
    free((char *)old_buffer);
  }
  memcpy(ret_buffer + old_len, append_buffer, append_len);
  ret_buffer[ret_len - 1] = '\0';
  return ret_buffer;
}


x86 assembly (clang, -O0):
<new_string_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1180 <new_string_buffer+0x40>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1202 <new_string_buffer+0xc2>
cmpq   $0x0,-0x10(%rbp)
je     1197 <new_string_buffer+0x57>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    11d2 <new_string_buffer+0x92>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_string_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %edx,%edx
je     11bb <new_string_buffer+0x7b>
mov    %edx,%r15d
mov    %rsi,%r14
test   %rbx,%rbx
je     116a <new_string_buffer+0x2a>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
jmp    116d <new_string_buffer+0x2d>
xor    %r12d,%r12d
lea    (%r12,%r15,1),%ebp
lea    (%r12,%r15,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
test   %r12d,%r12d
jle    119b <new_string_buffer+0x5b>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
movslq %r12d,%rax
mov    %r13,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @new_string_buffer(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* null, i8** %8
store i32 0, i32* %9
store i32 0, i32* %10
%11 = load i32, i32* %7
%12 = icmp ne i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %3
%14 = load i8*, i8** %5
store i8* %14, i8** %4
br label %52
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %10
br label %21
21: ; preds = %18, %15
%22 = load i32, i32* %10
%23 = load i32, i32* %7
%24 = add nsw i32 %22, %23
%25 = add nsw i32 %24, 1
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %8
%29 = load i32, i32* %10
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %21
%32 = load i8*, i8** %8
%33 = load i8*, i8** %5
%34 = load i32, i32* %10
%35 = call i32 @memcpy(i8* %32, i8* %33, i32 %34)
%36 = load i8*, i8** %5
%37 = call i32 @free(i8* %36)
br label %38
38: ; preds = %31, %21
%39 = load i8*, i8** %8
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8*, i8** %6
%44 = load i32, i32* %7
%45 = call i32 @memcpy(i8* %42, i8* %43, i32 %44)
%46 = load i8*, i8** %8
%47 = load i32, i32* %9
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %8
store i8* %51, i8** %4
br label %52
52: ; preds = %38, %13
%53 = load i8*, i8** %4
ret i8* %53
}

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