#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*/ exit(int);
int /*<<< orphan*/ memmove(char *, char *, size_t);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
void buffer_append(char **buffer, size_t *buffer_length, char *appended,
                   size_t appended_length) {
  *buffer = realloc(*buffer, *buffer_length + appended_length);
  if (*buffer == NULL && *buffer_length + appended_length != 0) {
    perror("realloc");
    exit(1);
  }
  memmove(*buffer + *buffer_length, appended, appended_length);
  *buffer_length = *buffer_length + appended_length;
}


x86 assembly (clang, -O0):
<buffer_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
add    -0x20(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11b2 <buffer_append+0x72>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    -0x20(%rbp),%rax
cmp    $0x0,%rax
je     11b2 <buffer_append+0x72>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
add    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_append>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    (%rsi),%rsi
add    %rcx,%rsi
call   1030 <realloc@plt>
mov    %rax,(%rbx)
mov    (%r14),%rcx
test   %rax,%rax
jne    1178 <buffer_append+0x38>
mov    %r12,%rdx
neg    %rdx
cmp    %rdx,%rcx
jne    1198 <buffer_append+0x58>
add    %rcx,%rax
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memmove@plt>
add    %r12,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"realloc\00"
define void @buffer_append(i8** %0, i64* %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64
store i8** %0, i8*** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%9 = load i8**, i8*** %5
%10 = load i8*, i8** %9
%11 = load i64*, i64** %6
%12 = load i64, i64* %11
%13 = load i64, i64* %8
%14 = add i64 %12, %13
%15 = call i8* @realloc(i8* %10, i64 %14)
%16 = load i8**, i8*** %5
store i8* %15, i8** %16
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %29
20: ; preds = %4
%21 = load i64*, i64** %6
%22 = load i64, i64* %21
%23 = load i64, i64* %8
%24 = add i64 %22, %23
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20, %4
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = load i64*, i64** %6
%33 = load i64, i64* %32
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8*, i8** %7
%36 = load i64, i64* %8
%37 = call i32 @memmove(i8* %34, i8* %35, i64 %36)
%38 = load i64*, i64** %6
%39 = load i64, i64* %38
%40 = load i64, i64* %8
%41 = add i64 %39, %40
%42 = load i64*, i64** %6
store i64 %41, i64* %42
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"realloc\00"
define void @buffer_append(i8** nocapture %0, i64* nocapture %1, i8* %2, i64 %3) {
%5 = load i8*, i8** %0
%6 = load i64, i64* %1
%7 = add i64 %6, %3
%8 = tail call i8* @realloc(i8* %5, i64 %7)
store i8* %8, i8** %0
%9 = icmp ne i8* %8, null
%10 = load i64, i64* %1
%11 = sub i64 0, %3
%12 = icmp eq i64 %10, %11
%13 = select i1 %9, i1 true, i1 %12
br i1 %13, label %17, label %14
14: ; preds = %4
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %4
%18 = getelementptr inbounds i8, i8* %8, i64 %10
%19 = tail call i32 @memmove(i8* %18, i8* %2, i64 %3)
%20 = load i64, i64* %1
%21 = add i64 %20, %3
store i64 %21, i64* %1
ret void
}