#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*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
void *realloc(void *, size_t);
int expand_by_doubling(void **ptr, size_t size, size_t *current_nmemb,
                       size_t min_nmemb) {
  void *new_ptr = NULL;
  size_t new_nmemb = 1;
  if (!ptr || !current_nmemb) {
    errno = EINVAL;
    return -1;
  }
  if (*current_nmemb >= min_nmemb)
    return 0;
  new_nmemb = (new_nmemb * size > min_nmemb * size) ? new_nmemb : min_nmemb;
  new_nmemb = (new_nmemb * size > 2 * (*current_nmemb) * size)
                  ? new_nmemb
                  : 2 * (*current_nmemb);
  if (*current_nmemb >= new_nmemb) {
    errno = ENOMEM;
    return -1;
  }
  new_ptr = realloc(*ptr, new_nmemb * size);
  if (!new_ptr) {
    errno = ENOMEM;
    return -1;
  }
  *ptr = new_ptr;
  *current_nmemb = new_nmemb;
  return 0;
}


x86 assembly (clang, -O0):
<expand_by_doubling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x1,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114e <expand_by_doubling+0x3e>
cmpq   $0x0,-0x20(%rbp)
jne    116c <expand_by_doubling+0x5c>
mov    0x2e7b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <expand_by_doubling+0x185>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jb     1189 <expand_by_doubling+0x79>
movl   $0x0,-0x4(%rbp)
jmp    1295 <expand_by_doubling+0x185>
mov    -0x38(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
imul   -0x18(%rbp),%rcx
cmp    %rcx,%rax
jbe    11b1 <expand_by_doubling+0xa1>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11b9 <expand_by_doubling+0xa9>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
shl    $0x1,%rcx
imul   -0x18(%rbp),%rcx
cmp    %rcx,%rax
jbe    11f0 <expand_by_doubling+0xe0>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11ff <expand_by_doubling+0xef>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
shl    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x38(%rbp),%rax
jb     1236 <expand_by_doubling+0x126>
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    0x2db0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <expand_by_doubling+0x185>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1278 <expand_by_doubling+0x168>
mov    0x2d7f(%rip),%rax
mov    (%rax),%ecx
mov    0x2d6e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <expand_by_doubling+0x185>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_by_doubling>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     117e <expand_by_doubling+0x6e>
mov    %rdx,%rbx
test   %rdx,%rdx
je     117e <expand_by_doubling+0x6e>
mov    (%rbx),%rax
xor    %r14d,%r14d
cmp    %rcx,%rax
jae    119f <expand_by_doubling+0x8f>
mov    %rdi,%rbp
mov    %rcx,%rdx
imul   %rsi,%rdx
cmp    %rsi,%rdx
mov    $0x1,%edx
cmovae %rcx,%rdx
mov    %rdx,%rcx
imul   %rsi,%rcx
lea    (%rax,%rax,1),%r15
mov    %r15,%rdi
imul   %rsi,%rdi
cmp    %rdi,%rcx
cmova  %rdx,%r15
cmp    %r15,%rax
jae    1187 <expand_by_doubling+0x77>
mov    0x0(%rbp),%rdi
imul   %r15,%rsi
call   1030 <realloc@plt>
test   %rax,%rax
je     1187 <expand_by_doubling+0x77>
mov    %rax,0x0(%rbp)
mov    %r15,(%rbx)
jmp    119f <expand_by_doubling+0x8f>
mov    0x2e4b(%rip),%rax
jmp    118e <expand_by_doubling+0x7e>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    0x2e41(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @expand_by_doubling(i8** %0, i64 %1, i64* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
store i8** %0, i8*** %6
store i64 %1, i64* %7
store i64* %2, i64** %8
store i64 %3, i64* %9
store i8* null, i8** %10
store i64 1, i64* %11
%12 = load i8**, i8*** %6
%13 = icmp ne i8** %12, null
br i1 %13, label %14, label %17
14: ; preds = %4
%15 = load i64*, i64** %8
%16 = icmp ne i64* %15, null
br i1 %16, label %19, label %17
17: ; preds = %14, %4
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
store i32 -1, i32* %5
br label %78
19: ; preds = %14
%20 = load i64*, i64** %8
%21 = load i64, i64* %20
%22 = load i64, i64* %9
%23 = icmp uge i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 0, i32* %5
br label %78
25: ; preds = %19
%26 = load i64, i64* %11
%27 = load i64, i64* %7
%28 = mul i64 %26, %27
%29 = load i64, i64* %9
%30 = load i64, i64* %7
%31 = mul i64 %29, %30
%32 = icmp ugt i64 %28, %31
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = load i64, i64* %11
br label %37
35: ; preds = %25
%36 = load i64, i64* %9
br label %37
37: ; preds = %35, %33
%38 = phi i64 [ %34, %33 ], [ %36, %35 ]
store i64 %38, i64* %11
%39 = load i64, i64* %11
%40 = load i64, i64* %7
%41 = mul i64 %39, %40
%42 = load i64*, i64** %8
%43 = load i64, i64* %42
%44 = mul i64 2, %43
%45 = load i64, i64* %7
%46 = mul i64 %44, %45
%47 = icmp ugt i64 %41, %46
br i1 %47, label %48, label %50
48: ; preds = %37
%49 = load i64, i64* %11
br label %54
50: ; preds = %37
%51 = load i64*, i64** %8
%52 = load i64, i64* %51
%53 = mul i64 2, %52
br label %54
54: ; preds = %50, %48
%55 = phi i64 [ %49, %48 ], [ %53, %50 ]
store i64 %55, i64* %11
%56 = load i64*, i64** %8
%57 = load i64, i64* %56
%58 = load i64, i64* %11
%59 = icmp uge i64 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = load i32, i32* @ENOMEM
store i32 %61, i32* @errno
store i32 -1, i32* %5
br label %78
62: ; preds = %54
%63 = load i8**, i8*** %6
%64 = load i8*, i8** %63
%65 = load i64, i64* %11
%66 = load i64, i64* %7
%67 = mul i64 %65, %66
%68 = call i8* @realloc(i8* %64, i64 %67)
store i8* %68, i8** %10
%69 = load i8*, i8** %10
%70 = icmp ne i8* %69, null
br i1 %70, label %73, label %71
71: ; preds = %62
%72 = load i32, i32* @ENOMEM
store i32 %72, i32* @errno
store i32 -1, i32* %5
br label %78
73: ; preds = %62
%74 = load i8*, i8** %10
%75 = load i8**, i8*** %6
store i8* %74, i8** %75
%76 = load i64, i64* %11
%77 = load i64*, i64** %8
store i64 %76, i64* %77
store i32 0, i32* %5
br label %78
78: ; preds = %73, %71, %60, %24, %17
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @expand_by_doubling(i8** %0, i64 %1, i64* %2, i64 %3) {
%5 = icmp ne i8** %0, null
%6 = icmp ne i64* %2, null
%7 = and i1 %5, %6
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
br label %33
10: ; preds = %4
%11 = load i64, i64* %2
%12 = icmp ult i64 %11, %3
br i1 %12, label %13, label %33
13: ; preds = %10
%14 = mul i64 %3, %1
%15 = icmp ult i64 %14, %1
%16 = select i1 %15, i64 1, i64 %3
%17 = mul i64 %16, %1
%18 = shl i64 %11, 1
%19 = mul i64 %18, %1
%20 = icmp ugt i64 %17, %19
%21 = select i1 %20, i64 %16, i64 %18
%22 = icmp ult i64 %11, %21
br i1 %22, label %25, label %23
23: ; preds = %13
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* @errno
br label %33
25: ; preds = %13
%26 = load i8*, i8** %0
%27 = mul i64 %21, %1
%28 = tail call i8* @realloc(i8* %26, i64 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* @ENOMEM
store i32 %31, i32* @errno
br label %33
32: ; preds = %25
store i8* %28, i8** %0
store i64 %21, i64* %2
br label %33
33: ; preds = %10, %32, %30, %23, %8
%34 = phi i32 [ -1, %23 ], [ 0, %32 ], [ -1, %30 ], [ -1, %8 ], [ 0, %10 ]
ret i32 %34
}