#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 */
scalar_t__ calloc(size_t, int);
void *large_alloc(size_t s0) {
  static char *alloc_pos = NULL;
  static size_t alloc_size = 0;
  void *res;
  size_t s = ((s0 + 3) / 4) * 4;
  if (alloc_size < s) {
    alloc_size = s + 1024 * 1024;
    alloc_pos = (char *)calloc(alloc_size, 1);
  }
  res = alloc_pos;
  alloc_size -= s;
  alloc_pos += s;
  return res;
}


x86 assembly (clang, -O0):
<large_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x3,%rax
shr    $0x2,%rax
shl    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    0x2f01(%rip),%rax
cmp    -0x18(%rbp),%rax
jae    116a <large_alloc+0x5a>
mov    -0x18(%rbp),%rax
add    $0x100000,%rax
mov    %rax,0x2ee6(%rip)
mov    0x2edf(%rip),%rdi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,0x2ec6(%rip)
mov    0x2ebf(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rcx
mov    0x2eb8(%rip),%rax
sub    %rcx,%rax
mov    %rax,0x2eae(%rip)
mov    -0x18(%rbp),%rax
add    0x2e9b(%rip),%rax
mov    %rax,0x2e94(%rip)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<large_alloc>:
push   %rbx
mov    %rdi,%rbx
add    $0x3,%rbx
and    $0xfffffffffffffffc,%rbx
mov    0x2f15(%rip),%rcx
cmp    %rbx,%rcx
jae    1149 <large_alloc+0x39>
lea    0x100000(%rbx),%rdi
mov    %rdi,0x2f02(%rip)
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    0x2ef1(%rip),%rcx
jmp    1150 <large_alloc+0x40>
mov    0x2ee0(%rip),%rax
sub    %rbx,%rcx
mov    %rcx,0x2ede(%rip)
add    %rax,%rbx
mov    %rbx,0x2ecc(%rip)
pop    %rbx
ret

LLVM IR (-O0):
@large_alloc.alloc_pos = internal global i8* null
@large_alloc.alloc_size = internal global i64 0
define i8* @large_alloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = add i64 %5, 3
%7 = udiv i64 %6, 4
%8 = mul i64 %7, 4
store i64 %8, i64* %4
%9 = load i64, i64* @large_alloc.alloc_size
%10 = load i64, i64* %4
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i64, i64* %4
%14 = add i64 %13, 1048576
store i64 %14, i64* @large_alloc.alloc_size
%15 = load i64, i64* @large_alloc.alloc_size
%16 = call i64 @calloc(i64 %15, i32 1)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** @large_alloc.alloc_pos
br label %18
18: ; preds = %12, %1
%19 = load i8*, i8** @large_alloc.alloc_pos
store i8* %19, i8** %3
%20 = load i64, i64* %4
%21 = load i64, i64* @large_alloc.alloc_size
%22 = sub i64 %21, %20
store i64 %22, i64* @large_alloc.alloc_size
%23 = load i64, i64* %4
%24 = load i8*, i8** @large_alloc.alloc_pos
%25 = getelementptr inbounds i8, i8* %24, i64 %23
store i8* %25, i8** @large_alloc.alloc_pos
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@large_alloc.alloc_pos = internal unnamed_addr global i8* null
@large_alloc.alloc_size = internal unnamed_addr global i64 0
define i8* @large_alloc(i64 %0) {
%2 = add i64 %0, 3
%3 = and i64 %2, -4
%4 = load i64, i64* @large_alloc.alloc_size
%5 = icmp ult i64 %4, %3
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = load i8*, i8** @large_alloc.alloc_pos
br label %13
8: ; preds = %1
%9 = add i64 %3, 1048576
store i64 %9, i64* @large_alloc.alloc_size
%10 = tail call i64 @calloc(i64 %9, i32 1)
%11 = inttoptr i64 %10 to i8*
%12 = load i64, i64* @large_alloc.alloc_size
br label %13
13: ; preds = %6, %8
%14 = phi i64 [ %4, %6 ], [ %12, %8 ]
%15 = phi i8* [ %7, %6 ], [ %11, %8 ]
%16 = sub i64 %14, %3
store i64 %16, i64* @large_alloc.alloc_size
%17 = getelementptr inbounds i8, i8* %15, i64 %3
store i8* %17, i8** @large_alloc.alloc_pos
ret i8* %15
}