#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*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ stderr;
char *ensure_capacity(char *buffer, size_t len, size_t *alloc) {
  if (buffer == NULL) {
    buffer = (char *)malloc(sizeof(char) * 256);
  } else if (*alloc - len < 256) {
    buffer = (char *)realloc(buffer, *alloc + 256);
    *alloc += 256;
  }
  if (buffer == NULL) {
    fprintf(stderr, "Not enough memory");
    return NULL;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<ensure_capacity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1162 <ensure_capacity+0x32>
mov    $0x100,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
jmp    11aa <ensure_capacity+0x7a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
sub    -0x18(%rbp),%rax
cmp    $0x100,%rax
jae    11a5 <ensure_capacity+0x75>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
add    $0x100,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0x100,%rcx
mov    %rcx,(%rax)
jmp    11aa <ensure_capacity+0x7a>
cmpq   $0x0,-0x10(%rbp)
jne    11d7 <ensure_capacity+0xa7>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11df <ensure_capacity+0xaf>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_capacity>:
push   %rbx
test   %rdi,%rdi
je     116e <ensure_capacity+0x3e>
mov    %rdx,%rbx
mov    %rsi,%rcx
mov    %rdi,%rax
mov    (%rdx),%rsi
mov    %rsi,%rdx
sub    %rcx,%rdx
cmp    $0xff,%rdx
ja     117d <ensure_capacity+0x4d>
add    $0x100,%rsi
mov    %rax,%rdi
call   1050 <realloc@plt>
addq   $0x100,(%rbx)
test   %rax,%rax
jne    117d <ensure_capacity+0x4d>
jmp    117f <ensure_capacity+0x4f>
mov    $0x100,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     117f <ensure_capacity+0x4f>
pop    %rbx
ret
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Not enough memory\00"
define i8* @ensure_capacity(i8* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = call i64 @malloc(i32 256)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %5
br label %30
13: ; preds = %3
%14 = load i64*, i64** %7
%15 = load i64, i64* %14
%16 = load i64, i64* %6
%17 = sub i64 %15, %16
%18 = icmp ult i64 %17, 256
br i1 %18, label %19, label %29
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = load i64*, i64** %7
%22 = load i64, i64* %21
%23 = add i64 %22, 256
%24 = call i64 @realloc(i8* %20, i64 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
%26 = load i64*, i64** %7
%27 = load i64, i64* %26
%28 = add i64 %27, 256
store i64 %28, i64* %26
br label %29
29: ; preds = %19, %13
br label %30
30: ; preds = %29, %10
%31 = load i8*, i8** %5
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %38
36: ; preds = %30
%37 = load i8*, i8** %5
store i8* %37, i8** %4
br label %38
38: ; preds = %36, %33
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Not enough memory\00"
define i8* @ensure_capacity(i8* %0, i64 %1, i64* nocapture %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i64 @malloc(i32 256)
br label %16
7: ; preds = %3
%8 = load i64, i64* %2
%9 = sub i64 %8, %1
%10 = icmp ult i64 %9, 256
br i1 %10, label %11, label %23
11: ; preds = %7
%12 = add i64 %8, 256
%13 = tail call i64 @realloc(i8* %0, i64 %12)
%14 = load i64, i64* %2
%15 = add i64 %14, 256
store i64 %15, i64* %2
br label %16
16: ; preds = %11, %5
%17 = phi i64 [ %6, %5 ], [ %13, %11 ]
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %7, %16, %20
%24 = phi i8* [ null, %20 ], [ %18, %16 ], [ %0, %7 ]
ret i8* %24
}