#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__ malloc(unsigned long);
int /*<<< orphan*/ printf(char *, unsigned long, unsigned long);
int /*<<< orphan*/ sleep(int);
void loadmem() {
  unsigned long int nbytes = 1;
  unsigned long int tbytes = 0;
  void *ptr = NULL;
  do {
    nbytes = nbytes + nbytes;
    ptr = (void *)malloc(nbytes);
    printf("malloc %lu bytes at %p\n", nbytes, ptr);
    if (ptr)
      tbytes += nbytes;
    sleep(1);
  } while (ptr);
  printf("malloc fail for %lu bytes. total %lu allocated\n", nbytes, tbytes);
}


x86 assembly (clang, -O0):
<loadmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x1,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe88(%rip),%rdi
call   1030 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     1194 <loadmem+0x64>
mov    -0x8(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x1,%edi
call   1050 <sleep@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1150 <loadmem+0x20>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadmem>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x1,%ebx
xor    %eax,%eax
lea    0xeb9(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rax,%r15
mov    %rbx,%r13
add    %rbx,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
lea    (%r15,%r13,2),%rax
test   %r12,%r12
jne    1150 <loadmem+0x20>
lea    0xe8c(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"malloc %lu bytes at %p\0A\00"
@.str.1 = [48 x i8] c"malloc fail for %lu bytes. total %lu allocated\0A\00"
define void @loadmem() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i8*
store i64 1, i64* %1
store i64 0, i64* %2
store i8* null, i8** %3
br label %4
4: ; preds = %23, %0
%5 = load i64, i64* %1
%6 = load i64, i64* %1
%7 = add i64 %5, %6
store i64 %7, i64* %1
%8 = load i64, i64* %1
%9 = call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = load i64, i64* %1
%12 = load i8*, i8** %3
%13 = ptrtoint i8* %12 to i64
%14 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %11, i64 %13)
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i64, i64* %1
%19 = load i64, i64* %2
%20 = add i64 %19, %18
store i64 %20, i64* %2
br label %21
21: ; preds = %17, %4
%22 = call i32 @sleep(i32 1)
br label %23
23: ; preds = %21
%24 = load i8*, i8** %3
%25 = icmp ne i8* %24, null
br i1 %25, label %4, label %26
26: ; preds = %23
%27 = load i64, i64* %1
%28 = load i64, i64* %2
%29 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i64 %27, i64 %28)
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"malloc %lu bytes at %p\0A\00"
@.str.1 = [48 x i8] c"malloc fail for %lu bytes. total %lu allocated\0A\00"
define void @loadmem() {
br label %1
1: ; preds = %1, %0
%2 = phi i64 [ 1, %0 ], [ %4, %1 ]
%3 = phi i64 [ 0, %0 ], [ %8, %1 ]
%4 = shl i64 %2, 1
%5 = tail call i64 @malloc(i64 %4)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %4, i64 %5)
%7 = icmp eq i64 %5, 0
%8 = add i64 %3, %4
%9 = tail call i32 @sleep(i32 1)
br i1 %7, label %10, label %1
10: ; preds = %1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i64 %4, i64 %3)
ret void
}