#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 CHAR_BIT;
int /*<<< orphan*/ EXIT_FAILURE;
unsigned long ULONG_MAX;
int /*<<< orphan*/ assert(unsigned long);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ realloc(void *, unsigned long);
int /*<<< orphan*/ rebuild();
unsigned long grow(void **pool, unsigned long size, unsigned long element_size,
                   unsigned long next, const unsigned int should_rebuild) {
  assert(element_size);
  if (size == ULONG_MAX) {
    printf("Out of memory: reached pool limit - exit.\n");
    exit(EXIT_FAILURE);
  }
  assert(next <= size);
  if (next < size)
    return size;
  unsigned long bits = sizeof(unsigned long) * CHAR_BIT;
  unsigned long inc = 1 << 10;
  unsigned long new_size;
  if (size == 0)
    new_size = 4;
  else if (ULONG_MAX - size < inc)
    new_size = ULONG_MAX;
  else if (size >> (bits / 2))
    new_size = size + inc;
  else
    new_size = size << 1;
  assert(new_size);
  void *new_pool = (void *)realloc(*pool, new_size * element_size);
  if (!new_pool) {
    printf("Out of memory: realloc failed - exit.\n");
    exit(EXIT_FAILURE);
  }
  void *old_pool = *pool;
  *pool = new_pool;
  if (old_pool != new_pool && should_rebuild)
    rebuild();
  return new_size;
}


x86 assembly (clang, -O0):
<grow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
jne    11a3 <grow+0x53>
lea    0xe70(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%eax
movslq %eax,%rdi
call   1050 <assert@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d6 <grow+0x86>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f7 <grow+0x1a7>
mov    0x2e03(%rip),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %rax,-0x38(%rbp)
movq   $0x400,-0x40(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1208 <grow+0xb8>
movq   $0x4,-0x48(%rbp)
jmp    1273 <grow+0x123>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
sub    -0x18(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    1233 <grow+0xe3>
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    126e <grow+0x11e>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
shr    $0x1,%rcx
shr    %cl,%rax
cmp    $0x0,%rax
je     125d <grow+0x10d>
mov    -0x18(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1269 <grow+0x119>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    126e <grow+0x11e>
jmp    1273 <grow+0x123>
mov    -0x48(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x48(%rbp),%rsi
imul   -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    12ba <grow+0x16a>
lea    0xd84(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x58(%rbp),%rax
cmp    -0x50(%rbp),%rax
je     12ef <grow+0x19f>
cmpl   $0x0,-0x2c(%rbp)
je     12ef <grow+0x19f>
mov    $0x0,%al
call   1040 <rebuild@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grow>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rdx,%rdi
call   1050 <assert@plt>
mov    0x2e4f(%rip),%r13
cmp    %rbx,0x0(%r13)
je     1225 <grow+0xd5>
xor    %edi,%edi
cmp    %rbx,%rbp
setbe  %dil
call   1050 <assert@plt>
cmp    %rbx,%rbp
jae    119b <grow+0x4b>
mov    %rbx,%rbp
jmp    1213 <grow+0xc3>
test   %rbx,%rbx
je     11d3 <grow+0x83>
mov    0x0(%r13),%rbp
mov    %rbp,%rax
sub    %rbx,%rax
cmp    $0x400,%rax
jb     11e0 <grow+0x90>
mov    0x2e27(%rip),%rax
movslq (%rax),%rcx
shl    $0x2,%ecx
mov    %rbx,%rax
shr    %cl,%rax
test   %rax,%rax
je     11da <grow+0x8a>
add    $0x400,%rbx
jmp    11dd <grow+0x8d>
mov    $0x4,%ebp
jmp    11e0 <grow+0x90>
add    %rbx,%rbx
mov    %rbx,%rbp
mov    %rbp,%rdi
call   1050 <assert@plt>
mov    (%r15),%rdi
imul   %rbp,%r12
mov    %r12,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     122e <grow+0xde>
mov    (%r15),%rcx
mov    %rax,(%r15)
test   %r14d,%r14d
je     1213 <grow+0xc3>
cmp    %rax,%rcx
je     1213 <grow+0xc3>
xor    %eax,%eax
call   1040 <rebuild@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rdi
jmp    1235 <grow+0xe5>
lea    0xdcb(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@ULONG_MAX = global i64 0
@.str = [43 x i8] c"Out of memory: reached pool limit - exit.\0A\00"
@EXIT_FAILURE = global i32 0
@CHAR_BIT = global i32 0
@.str.1 = [39 x i8] c"Out of memory: realloc failed - exit.\0A\00"
define i64 @grow(i8** %0, i64 %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i8*
store i8** %0, i8*** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i32 %4, i32* %11
%17 = load i64, i64* %9
%18 = call i32 @assert(i64 %17)
%19 = load i64, i64* %8
%20 = load i64, i64* @ULONG_MAX
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %5
%23 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %5
%27 = load i64, i64* %10
%28 = load i64, i64* %8
%29 = icmp ule i64 %27, %28
%30 = zext i1 %29 to i32
%31 = sext i32 %30 to i64
%32 = call i32 @assert(i64 %31)
%33 = load i64, i64* %10
%34 = load i64, i64* %8
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = load i64, i64* %8
store i64 %37, i64* %6
br label %99
38: ; preds = %26
%39 = load i32, i32* @CHAR_BIT
%40 = sext i32 %39 to i64
%41 = mul i64 8, %40
store i64 %41, i64* %12
store i64 1024, i64* %13
%42 = load i64, i64* %8
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38
store i64 4, i64* %14
br label %68
45: ; preds = %38
%46 = load i64, i64* @ULONG_MAX
%47 = load i64, i64* %8
%48 = sub i64 %46, %47
%49 = load i64, i64* %13
%50 = icmp ult i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = load i64, i64* @ULONG_MAX
store i64 %52, i64* %14
br label %67
53: ; preds = %45
%54 = load i64, i64* %8
%55 = load i64, i64* %12
%56 = udiv i64 %55, 2
%57 = lshr i64 %54, %56
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = load i64, i64* %8
%61 = load i64, i64* %13
%62 = add i64 %60, %61
store i64 %62, i64* %14
br label %66
63: ; preds = %53
%64 = load i64, i64* %8
%65 = shl i64 %64, 1
store i64 %65, i64* %14
br label %66
66: ; preds = %63, %59
br label %67
67: ; preds = %66, %51
br label %68
68: ; preds = %67, %44
%69 = load i64, i64* %14
%70 = call i32 @assert(i64 %69)
%71 = load i8**, i8*** %7
%72 = load i8*, i8** %71
%73 = load i64, i64* %14
%74 = load i64, i64* %9
%75 = mul i64 %73, %74
%76 = call i64 @realloc(i8* %72, i64 %75)
%77 = inttoptr i64 %76 to i8*
store i8* %77, i8** %15
%78 = load i8*, i8** %15
%79 = icmp ne i8* %78, null
br i1 %79, label %84, label %80
80: ; preds = %68
%81 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%82 = load i32, i32* @EXIT_FAILURE
%83 = call i32 @exit(i32 %82)
unreachable
84: ; preds = %68
%85 = load i8**, i8*** %7
%86 = load i8*, i8** %85
store i8* %86, i8** %16
%87 = load i8*, i8** %15
%88 = load i8**, i8*** %7
store i8* %87, i8** %88
%89 = load i8*, i8** %16
%90 = load i8*, i8** %15
%91 = icmp ne i8* %89, %90
br i1 %91, label %92, label %97
92: ; preds = %84
%93 = load i32, i32* %11
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %92
%96 = call i32 (...) @rebuild()
br label %97
97: ; preds = %95, %92, %84
%98 = load i64, i64* %14
store i64 %98, i64* %6
br label %99
99: ; preds = %97, %36
%100 = load i64, i64* %6
ret i64 %100
}

LLVM IR (-O2):
@ULONG_MAX = global i64 0
@EXIT_FAILURE = global i32 0
@CHAR_BIT = global i32 0
@str = [38 x i8] c"Out of memory: realloc failed - exit.\00"
@str.2 = [42 x i8] c"Out of memory: reached pool limit - exit.\00"
define i64 @grow(i8** nocapture %0, i64 %1, i64 %2, i64 %3, i32 %4) {
%6 = tail call i32 @assert(i64 %2)
%7 = load i64, i64* @ULONG_MAX
%8 = icmp eq i64 %7, %1
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.2, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %5
%14 = icmp ule i64 %3, %1
%15 = zext i1 %14 to i64
%16 = tail call i32 @assert(i64 %15)
%17 = icmp ult i64 %3, %1
br i1 %17, label %54, label %18
18: ; preds = %13
%19 = load i32, i32* @CHAR_BIT
%20 = sext i32 %19 to i64
%21 = icmp eq i64 %1, 0
br i1 %21, label %35, label %22
22: ; preds = %18
%23 = load i64, i64* @ULONG_MAX
%24 = sub i64 %23, %1
%25 = icmp ult i64 %24, 1024
br i1 %25, label %35, label %26
26: ; preds = %22
%27 = shl nsw i64 %20, 2
%28 = and i64 %27, 9223372036854775804
%29 = lshr i64 %1, %28
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = add i64 %1, 1024
br label %35
33: ; preds = %26
%34 = shl i64 %1, 1
br label %35
35: ; preds = %22, %18, %33, %31
%36 = phi i64 [ %32, %31 ], [ %34, %33 ], [ 4, %18 ], [ %23, %22 ]
%37 = tail call i32 @assert(i64 %36)
%38 = load i8*, i8** %0
%39 = mul i64 %36, %2
%40 = tail call i64 @realloc(i8* %38, i64 %39)
%41 = inttoptr i64 %40 to i8*
%42 = icmp eq i64 %40, 0
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = tail call i32 @exit(i32 %45)
unreachable
47: ; preds = %35
%48 = load i8*, i8** %0
store i8* %41, i8** %0
%49 = icmp ne i8* %48, %41
%50 = icmp ne i32 %4, 0
%51 = and i1 %50, %49
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = tail call i32 (...) @rebuild()
br label %54
54: ; preds = %47, %52, %13
%55 = phi i64 [ %1, %13 ], [ %36, %52 ], [ %36, %47 ]
ret i64 %55
}