#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 */
char *realloc(char *, size_t);
void check_capacity(char **buf, size_t *cap, size_t offs, char **curp,
                    size_t curi) {
  if (*cap < offs) {
    *cap = offs < 128 ? 256 : (*cap) * 2;
    *buf = realloc(*buf, *cap);
    if (curp)
      *curp = *buf + curi;
  }
}


x86 assembly (clang, -O0):
<check_capacity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x18(%rbp),%rax
jae    11b2 <check_capacity+0xa2>
cmpq   $0x80,-0x18(%rbp)
jae    1159 <check_capacity+0x49>
mov    $0x100,%eax
mov    %rax,-0x30(%rbp)
jmp    1168 <check_capacity+0x58>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1030 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     11ad <check_capacity+0x9d>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    11b2 <check_capacity+0xa2>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_capacity>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%r15
mov    (%rsi),%rcx
cmp    %rdx,%rcx
jae    1155 <check_capacity+0x45>
mov    %r8,%r14
mov    %rdi,%rbx
add    %rcx,%rcx
cmp    $0x80,%rdx
mov    $0x100,%eax
cmovae %rcx,%rax
mov    %rax,(%rsi)
mov    (%rdi),%rdi
mov    %rax,%rsi
call   1030 <realloc@plt>
mov    %rax,(%rbx)
test   %r15,%r15
je     1155 <check_capacity+0x45>
add    %r14,%rax
mov    %rax,(%r15)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @check_capacity(i8** %0, i64* %1, i64 %2, i8** %3, i64 %4) {
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i8**
%10 = alloca i64
store i8** %0, i8*** %6
store i64* %1, i64** %7
store i64 %2, i64* %8
store i8** %3, i8*** %9
store i64 %4, i64* %10
%11 = load i64*, i64** %7
%12 = load i64, i64* %11
%13 = load i64, i64* %8
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %41
15: ; preds = %5
%16 = load i64, i64* %8
%17 = icmp ult i64 %16, 128
br i1 %17, label %18, label %19
18: ; preds = %15
br label %23
19: ; preds = %15
%20 = load i64*, i64** %7
%21 = load i64, i64* %20
%22 = mul i64 %21, 2
br label %23
23: ; preds = %19, %18
%24 = phi i64 [ 256, %18 ], [ %22, %19 ]
%25 = load i64*, i64** %7
store i64 %24, i64* %25
%26 = load i8**, i8*** %6
%27 = load i8*, i8** %26
%28 = load i64*, i64** %7
%29 = load i64, i64* %28
%30 = call i8* @realloc(i8* %27, i64 %29)
%31 = load i8**, i8*** %6
store i8* %30, i8** %31
%32 = load i8**, i8*** %9
%33 = icmp ne i8** %32, null
br i1 %33, label %34, label %40
34: ; preds = %23
%35 = load i8**, i8*** %6
%36 = load i8*, i8** %35
%37 = load i64, i64* %10
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = load i8**, i8*** %9
store i8* %38, i8** %39
br label %40
40: ; preds = %34, %23
br label %41
41: ; preds = %40, %5
ret void
}

LLVM IR (-O2):
define void @check_capacity(i8** nocapture %0, i64* nocapture %1, i64 %2, i8** writeonly %3, i64 %4) {
%6 = load i64, i64* %1
%7 = icmp ult i64 %6, %2
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = icmp ult i64 %2, 128
%10 = shl i64 %6, 1
%11 = select i1 %9, i64 256, i64 %10
store i64 %11, i64* %1
%12 = load i8*, i8** %0
%13 = tail call i8* @realloc(i8* %12, i64 %11)
store i8* %13, i8** %0
%14 = icmp eq i8** %3, null
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = getelementptr inbounds i8, i8* %13, i64 %4
store i8* %16, i8** %3
br label %17
17: ; preds = %8, %15, %5
ret void
}