#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*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ messalloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
void add(char c, char **buf, int *buflen, int n) {
  if (n >= *buflen) {
    int blen = *buflen;
    if (blen < 0) {
      blen = -blen;
      *buf = (char *)messalloc(blen);
    } else {
      blen *= 2;
      if ((*buf = realloc(*buf, blen)) == NULL) {
        fprintf(stderr, "REALLOC failure reqesting %d bytes - aborting\n",
                blen);
        exit(-1);
      }
    }
    *buflen = blen;
  }
  (*buf)[n] = c;
}


x86 assembly (clang, -O0):
<add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jl     11f3 <add+0xb3>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    119a <add+0x5a>
xor    %eax,%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <messalloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11ea <add+0xaa>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%esi
call   1050 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11e5 <add+0xa5>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe2a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    11ea <add+0xaa>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %rsi,%rbx
mov    %edi,%r14d
mov    (%rdx),%ebp
cmp    %ecx,%ebp
jle    115c <add+0x1c>
mov    (%rbx),%rax
jmp    11a7 <add+0x67>
mov    %rdx,%r15
test   %ebp,%ebp
js     1198 <add+0x58>
add    %ebp,%ebp
mov    (%rbx),%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    11a4 <add+0x64>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
neg    %ebp
mov    %ebp,%edi
call   1030 <messalloc@plt>
mov    %rax,(%rbx)
mov    %ebp,(%r15)
movslq %r12d,%rcx
mov    %r14b,(%rax,%rcx,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [47 x i8] c"REALLOC failure reqesting %d bytes - aborting\0A\00"
define void @add(i8 signext %0, i8** %1, i32* %2, i32 %3) {
%5 = alloca i8
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8 %0, i8* %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%10 = load i32, i32* %8
%11 = load i32*, i32** %7
%12 = load i32, i32* %11
%13 = icmp sge i32 %10, %12
br i1 %13, label %14, label %44
14: ; preds = %4
%15 = load i32*, i32** %7
%16 = load i32, i32* %15
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load i32, i32* %9
%21 = sub nsw i32 0, %20
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = call i64 @messalloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
%25 = load i8**, i8*** %6
store i8* %24, i8** %25
br label %41
26: ; preds = %14
%27 = load i32, i32* %9
%28 = mul nsw i32 %27, 2
store i32 %28, i32* %9
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = load i32, i32* %9
%32 = call i8* @realloc(i8* %30, i32 %31)
%33 = load i8**, i8*** %6
store i8* %32, i8** %33
%34 = icmp eq i8* %32, null
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = load i32, i32* @stderr
%37 = load i32, i32* %9
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %26
br label %41
41: ; preds = %40, %19
%42 = load i32, i32* %9
%43 = load i32*, i32** %7
store i32 %42, i32* %43
br label %44
44: ; preds = %41, %4
%45 = load i8, i8* %5
%46 = load i8**, i8*** %6
%47 = load i8*, i8** %46
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 %45, i8* %50
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [47 x i8] c"REALLOC failure reqesting %d bytes - aborting\0A\00"
define void @add(i8 signext %0, i8** nocapture %1, i32* nocapture %2, i32 %3) {
%5 = load i32, i32* %2
%6 = icmp sgt i32 %5, %3
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = load i8*, i8** %1
br label %27
9: ; preds = %4
%10 = icmp slt i32 %5, 0
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = sub nsw i32 0, %5
%13 = tail call i64 @messalloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %1
br label %24
15: ; preds = %9
%16 = shl nuw nsw i32 %5, 1
%17 = load i8*, i8** %1
%18 = tail call i8* @realloc(i8* %17, i32 %16)
store i8* %18, i8** %1
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %16)
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %15, %11
%25 = phi i8* [ %14, %11 ], [ %18, %15 ]
%26 = phi i32 [ %12, %11 ], [ %16, %15 ]
store i32 %26, i32* %2
br label %27
27: ; preds = %7, %24
%28 = phi i8* [ %8, %7 ], [ %25, %24 ]
%29 = sext i32 %3 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 %0, i8* %30
ret void
}