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


x86 assembly (clang, -O0):
<new_cstring_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1170 <new_cstring_buffer+0x40>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    11a2 <new_cstring_buffer+0x72>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <memcpy@plt>
cmpl   $0x0,-0x14(%rbp)
jle    11c3 <new_cstring_buffer+0x93>
mov    -0x20(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_cstring_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r13
test   %rdi,%rdi
je     1155 <new_cstring_buffer+0x25>
mov    %r13,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
jmp    1157 <new_cstring_buffer+0x27>
xor    %ebx,%ebx
lea    (%rbx,%r15,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %ebx,%ebx
jle    1177 <new_cstring_buffer+0x47>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
lea    (%rbx,%r15,1),%ebp
test   %r15d,%r15d
jle    1194 <new_cstring_buffer+0x64>
movslq %ebx,%rax
mov    %r12,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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