#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*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, size_t const);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, size_t const);
void gimme(unsigned long megs) {
  static char *ptr = NULL;
  static size_t oldsize = 0;
  const size_t bytes = megs * 1024 * 1024;
  printf("Allocating %lu MB: ", megs);
  fflush(NULL);
  ptr = realloc(ptr, bytes);
  if (!ptr) {
    printf("Failed!\n");
    exit(EXIT_FAILURE);
  }
  if (bytes > oldsize)
    memset(ptr + oldsize, '#', bytes - oldsize);
  oldsize = bytes;
  printf("0x%lx bytes\n", (unsigned long)bytes);
}


x86 assembly (clang, -O0):
<gimme>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shl    $0xa,%rax
shl    $0xa,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    0x2ec2(%rip),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,0x2eb2(%rip)
cmpq   $0x0,0x2eaa(%rip)
jne    11c8 <gimme+0x78>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
cmp    0x2e85(%rip),%rax
jbe    11fc <gimme+0xac>
mov    0x2e70(%rip),%rdi
add    0x2e71(%rip),%rdi
mov    -0x10(%rbp),%rdx
sub    0x2e66(%rip),%rdx
mov    $0x23,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,0x2e51(%rip)
mov    -0x10(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gimme>:
push   %rbx
mov    %rdi,%rsi
mov    %rdi,%rbx
shl    $0x14,%rbx
lea    0xe8e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    0x2ed1(%rip),%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
mov    %rax,0x2ec2(%rip)
test   %rax,%rax
je     11d3 <gimme+0x73>
mov    0x2ebe(%rip),%rcx
mov    %rbx,%rdx
sub    %rcx,%rdx
jbe    11ba <gimme+0x5a>
add    %rcx,%rax
mov    %rax,%rdi
mov    $0x23,%esi
call   1050 <memset@plt>
mov    %rbx,0x2e9f(%rip)
lea    0xe4c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
pop    %rbx
jmp    1040 <printf@plt>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@gimme.ptr = internal global i8* null
@gimme.oldsize = internal global i64 0
@.str = [20 x i8] c"Allocating %lu MB: \00"
@.str.1 = [9 x i8] c"Failed!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"0x%lx bytes\0A\00"
define void @gimme(i64 %0) {
%2 = alloca i64
%3 = alloca i64
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = mul i64 %4, 1024
%6 = mul i64 %5, 1024
store i64 %6, i64* %3
%7 = load i64, i64* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %7)
%9 = call i32 @fflush(i32* null)
%10 = load i8*, i8** @gimme.ptr
%11 = load i64, i64* %3
%12 = call i8* @realloc(i8* %10, i64 %11)
store i8* %12, i8** @gimme.ptr
%13 = load i8*, i8** @gimme.ptr
%14 = icmp ne i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %1
%20 = load i64, i64* %3
%21 = load i64, i64* @gimme.oldsize
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = load i8*, i8** @gimme.ptr
%25 = load i64, i64* @gimme.oldsize
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i64, i64* %3
%28 = load i64, i64* @gimme.oldsize
%29 = sub i64 %27, %28
%30 = call i32 @memset(i8* %26, i8 signext 35, i64 %29)
br label %31
31: ; preds = %23, %19
%32 = load i64, i64* %3
store i64 %32, i64* @gimme.oldsize
%33 = load i64, i64* %3
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %33)
ret void
}

LLVM IR (-O2):
@gimme.ptr = internal unnamed_addr global i8* null
@gimme.oldsize = internal unnamed_addr global i64 0
@.str = [20 x i8] c"Allocating %lu MB: \00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"0x%lx bytes\0A\00"
@str = [8 x i8] c"Failed!\00"
define void @gimme(i64 %0) {
%2 = shl i64 %0, 20
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %0)
%4 = tail call i32 @fflush(i32* null)
%5 = load i8*, i8** @gimme.ptr
%6 = tail call i8* @realloc(i8* %5, i64 %2)
store i8* %6, i8** @gimme.ptr
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = load i64, i64* @gimme.oldsize
%14 = icmp ugt i64 %2, %13
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %6, i64 %13
%17 = sub i64 %2, %13
%18 = tail call i32 @memset(i8* %16, i8 signext 35, i64 %17)
br label %19
19: ; preds = %15, %12
store i64 %2, i64* @gimme.oldsize
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %2)
ret void
}