#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void realloc_array(void **array, size_t elem_size, size_t *curr_array_size,
                   size_t requested_size) {
  const size_t array_inc_size = (1024 + elem_size - 1) / elem_size;
  *curr_array_size =
      array_inc_size * ((requested_size + array_inc_size) / array_inc_size);
  *array = realloc(*array, *curr_array_size * elem_size);
  if (*array == NULL && (*curr_array_size * elem_size)) {
    fprintf(stderr, "error in realloc\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<realloc_array>:
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    -0x10(%rbp),%rax
add    $0x400,%rax
sub    $0x1,%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
xor    %edx,%edx
divq   -0x28(%rbp)
imul   %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
imul   -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e8 <realloc_array+0xb8>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
imul   -0x10(%rbp),%rax
cmp    $0x0,%rax
je     11e8 <realloc_array+0xb8>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc_array>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x3ff(%rsi),%rax
mov    %rax,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     115b <realloc_array+0x2b>
xor    %edx,%edx
div    %rbx
mov    %rax,%rsi
jmp    1161 <realloc_array+0x31>
xor    %edx,%edx
div    %ebx
mov    %eax,%esi
add    %rsi,%rcx
mov    %rcx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     117a <realloc_array+0x4a>
mov    %rcx,%rax
xor    %edx,%edx
div    %rsi
jmp    1180 <realloc_array+0x50>
mov    %ecx,%eax
xor    %edx,%edx
div    %esi
sub    %rdx,%rcx
mov    %rcx,(%r14)
mov    (%r15),%rdi
imul   %rbx,%rcx
mov    %rcx,%rsi
call   1040 <realloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    11a6 <realloc_array+0x76>
imul   (%r14),%rbx
test   %rbx,%rbx
jne    11ac <realloc_array+0x7c>
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"error in realloc\0A\00"
@EXIT_FAILURE = global i32 0
define void @realloc_array(i8** %0, i64 %1, i64* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i64 %3, i64* %8
%10 = load i64, i64* %6
%11 = add i64 1024, %10
%12 = sub i64 %11, 1
%13 = load i64, i64* %6
%14 = udiv i64 %12, %13
store i64 %14, i64* %9
%15 = load i64, i64* %9
%16 = load i64, i64* %8
%17 = load i64, i64* %9
%18 = add i64 %16, %17
%19 = load i64, i64* %9
%20 = udiv i64 %18, %19
%21 = mul i64 %15, %20
%22 = load i64*, i64** %7
store i64 %21, i64* %22
%23 = load i8**, i8*** %5
%24 = load i8*, i8** %23
%25 = load i64*, i64** %7
%26 = load i64, i64* %25
%27 = load i64, i64* %6
%28 = mul i64 %26, %27
%29 = call i8* @realloc(i8* %24, i64 %28)
%30 = load i8**, i8*** %5
store i8* %29, i8** %30
%31 = load i8**, i8*** %5
%32 = load i8*, i8** %31
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %45
34: ; preds = %4
%35 = load i64*, i64** %7
%36 = load i64, i64* %35
%37 = load i64, i64* %6
%38 = mul i64 %36, %37
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %34, %4
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"error in realloc\0A\00"
@EXIT_FAILURE = global i32 0
define void @realloc_array(i8** nocapture %0, i64 %1, i64* nocapture %2, i64 %3) {
%5 = add i64 %1, 1023
%6 = udiv i64 %5, %1
%7 = add i64 %6, %3
%8 = urem i64 %7, %6
%9 = sub i64 %7, %8
store i64 %9, i64* %2
%10 = load i8*, i8** %0
%11 = mul i64 %9, %1
%12 = tail call i8* @realloc(i8* %10, i64 %11)
store i8* %12, i8** %0
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %23
14: ; preds = %4
%15 = load i64, i64* %2
%16 = mul i64 %15, %1
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14, %4
ret void
}