#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 */
void *checked_realloc(void *, size_t);
int /*<<< orphan*/ memory_exhausted(int /*<<< orphan*/);
void *checked_grow_alloc(void *ptr, size_t *size) {
  size_t max = -1;
  if (*size == max)
    memory_exhausted(0);
  *size = *size < max / 2 ? 2 * *size : max;
  return checked_realloc(ptr, *size);
}


x86 assembly (clang, -O0):
<checked_grow_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0xffffffffffffffff,-0x18(%rbp)
ff
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x18(%rbp),%rax
jne    1150 <checked_grow_alloc+0x30>
xor    %edi,%edi
call   1030 <memory_exhausted@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jae    117c <checked_grow_alloc+0x5c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1184 <checked_grow_alloc+0x64>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <checked_realloc@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_grow_alloc>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%rax
cmp    $0xffffffffffffffff,%rax
jne    113d <checked_grow_alloc+0x1d>
xor    %edi,%edi
call   1030 <memory_exhausted@plt>
mov    (%rbx),%rax
lea    (%rax,%rax,1),%rcx
movabs $0x7fffffffffffffff,%rdx
ff ff 7f
cmp    %rdx,%rax
mov    $0xffffffffffffffff,%rsi
cmovb  %rcx,%rsi
mov    %rsi,(%rbx)
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <checked_realloc@plt>

LLVM IR (-O0):
define i8* @checked_grow_alloc(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i64
store i8* %0, i8** %3
store i64* %1, i64** %4
store i64 -1, i64* %5
%6 = load i64*, i64** %4
%7 = load i64, i64* %6
%8 = load i64, i64* %5
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @memory_exhausted(i32 0)
br label %12
12: ; preds = %10, %2
%13 = load i64*, i64** %4
%14 = load i64, i64* %13
%15 = load i64, i64* %5
%16 = udiv i64 %15, 2
%17 = icmp ult i64 %14, %16
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i64*, i64** %4
%20 = load i64, i64* %19
%21 = mul i64 2, %20
br label %24
22: ; preds = %12
%23 = load i64, i64* %5
br label %24
24: ; preds = %22, %18
%25 = phi i64 [ %21, %18 ], [ %23, %22 ]
%26 = load i64*, i64** %4
store i64 %25, i64* %26
%27 = load i8*, i8** %3
%28 = load i64*, i64** %4
%29 = load i64, i64* %28
%30 = call i8* @checked_realloc(i8* %27, i64 %29)
ret i8* %30
}

LLVM IR (-O2):
define i8* @checked_grow_alloc(i8* %0, i64* nocapture %1) {
%3 = load i64, i64* %1
%4 = icmp eq i64 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @memory_exhausted(i32 0)
%7 = load i64, i64* %1
br label %8
8: ; preds = %5, %2
%9 = phi i64 [ %7, %5 ], [ %3, %2 ]
%10 = icmp ult i64 %9, 9223372036854775807
%11 = shl i64 %9, 1
%12 = select i1 %10, i64 %11, i64 -1
store i64 %12, i64* %1
%13 = tail call i8* @checked_realloc(i8* %0, i64 %12)
ret i8* %13
}