#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*/ free(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ realloc(char *, int);
int main(int argc, char *argv[]) {
  static int count = 1;
  char *p = NULL;
  while (1) {
    p = (char *)realloc(p, (count * 1024 * 1024));
    if (p == NULL) {
      break;
    }
    count++;
  }
  printf("the malloc_length is %d \n", count - 1);
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2ee0(%rip),%esi
shl    $0xa,%esi
shl    $0xa,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <main+0x47>
jmp    118b <main+0x5b>
mov    0x2ebb(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2eb2(%rip)
jmp    114e <main+0x1e>
mov    0x2ea7(%rip),%esi
sub    $0x1,%esi
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2f01(%rip),%esi
shl    $0x14,%esi
xor    %edi,%edi
nopl   0x0(%rax)
call   1050 <realloc@plt>
test   %rax,%rax
je     1161 <main+0x31>
mov    0x2ee8(%rip),%esi
add    $0x1,%esi
mov    %esi,0x2edf(%rip)
shl    $0x14,%esi
mov    %rax,%rdi
jmp    1140 <main+0x10>
mov    0x2ed1(%rip),%esi
add    $0xffffffff,%esi
lea    0xe8f(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@main.count = internal global i32 1
@.str = [26 x i8] c"the malloc_length is %d \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
br label %7
7: ; preds = %2, %17
%8 = load i8*, i8** %6
%9 = load i32, i32* @main.count
%10 = mul nsw i32 %9, 1024
%11 = mul nsw i32 %10, 1024
%12 = call i64 @realloc(i8* %8, i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %7
br label %20
17: ; preds = %7
%18 = load i32, i32* @main.count
%19 = add nsw i32 %18, 1
store i32 %19, i32* @main.count
br label %7
20: ; preds = %16
%21 = load i32, i32* @main.count
%22 = sub nsw i32 %21, 1
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %6
%25 = call i32 @free(i8* %24)
ret i32 0
}

LLVM IR (-O2):
@main.count = internal unnamed_addr global i32 1
@.str = [26 x i8] c"the malloc_length is %d \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @main.count
%4 = shl nsw i32 %3, 20
%5 = tail call i64 @realloc(i8* null, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %2, %7
%8 = phi i64 [ %13, %7 ], [ %5, %2 ]
%9 = inttoptr i64 %8 to i8*
%10 = load i32, i32* @main.count
%11 = add nsw i32 %10, 1
store i32 %11, i32* @main.count
%12 = shl nsw i32 %11, 20
%13 = tail call i64 @realloc(i8* %9, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %7
15: ; preds = %7, %2
%16 = load i32, i32* @main.count
%17 = add nsw i32 %16, -1
%18 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = tail call i32 @free(i8* null)
ret i32 0
}