#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 *, size_t);
scalar_t__ realloc(char *, size_t);
int append_buffer_data(char **pdata, size_t *pdatalen, const char *bufferdata,
                       size_t bufferdatalen) {
  if ((*pdata = (char *)realloc(*pdata, *pdatalen + bufferdatalen + 1)) == NULL)
    return 1;
  memcpy(*pdata + *pdatalen, bufferdata, bufferdatalen);
  *pdatalen += bufferdatalen;
  return 0;
}


x86 assembly (clang, -O0):
<append_buffer_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1040 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1170 <append_buffer_data+0x50>
movl   $0x1,-0x4(%rbp)
jmp    11a0 <append_buffer_data+0x80>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_buffer_data>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    (%rsi),%rax
lea    (%rcx,%rax,1),%rsi
add    $0x1,%rsi
call   1040 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1169 <append_buffer_data+0x49>
add    (%r12),%rax
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <memcpy@plt>
add    %r14,(%r12)
xor    %eax,%eax
jmp    116e <append_buffer_data+0x4e>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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