#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*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
void *realloc(void *, int);
int /*<<< orphan*/ stderr;
int misc_realloc(void **blocks, int len) {
  void *nblocks;
  int nlen;
  nlen = (((len - 1) / (1024)) + 1) * (1024);
  if ((nblocks = realloc(*blocks, nlen)) == NULL) {
    fprintf(stderr, "fatal: out of memory\n");
    abort();
  }
  *blocks = nblocks;
  return nlen;
}


x86 assembly (clang, -O0):
<misc_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
shl    $0xa,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    118f <misc_realloc+0x5f>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_realloc>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
lea    -0x1(%rbx),%eax
add    $0x3fe,%ebx
test   %eax,%eax
cmovns %eax,%ebx
mov    %rdi,%r14
and    $0xfffffc00,%ebx
add    $0x400,%ebx
mov    (%rdi),%rdi
mov    %ebx,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     116f <misc_realloc+0x3f>
mov    %rax,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"fatal: out of memory\0A\00"
define i32 @misc_realloc(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sub nsw i32 %7, 1
%9 = sdiv i32 %8, 1024
%10 = add nsw i32 %9, 1
%11 = mul nsw i32 %10, 1024
store i32 %11, i32* %6
%12 = load i8**, i8*** %3
%13 = load i8*, i8** %12
%14 = load i32, i32* %6
%15 = call i8* @realloc(i8* %13, i32 %14)
store i8* %15, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%20 = call i32 (...) @abort()
unreachable
21: ; preds = %2
%22 = load i8*, i8** %5
%23 = load i8**, i8*** %3
store i8* %22, i8** %23
%24 = load i32, i32* %6
ret i32 %24
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"fatal: out of memory\0A\00"
define i32 @misc_realloc(i8** nocapture %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = sdiv i32 %3, 1024
%5 = shl nsw i32 %4, 10
%6 = add i32 %5, 1024
%7 = load i8*, i8** %0
%8 = tail call i8* @realloc(i8* %7, i32 %6)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 (...) @abort()
unreachable
14: ; preds = %2
store i8* %8, i8** %0
ret i32 %6
}