#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 */
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  unsigned long i;
  char *x;
  for (i = 0; i < 5000; i += 1) {
    if ((x = calloc(16, 1 << 16)) == 0) {
      write(1, "X\n", 2);
      return 0;
    }
    free(x);
  }
  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)
cmpq   $0x1388,-0x18(%rbp)
jae    11b5 <main+0x85>
mov    $0x10,%edi
mov    $0x10000,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119b <main+0x6b>
mov    $0x1,%edi
lea    0xe7b(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
movl   $0x0,-0x4(%rbp)
jmp    11bc <main+0x8c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114e <main+0x1e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x1388,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    $0x10,%edi
mov    $0x10000,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     1164 <main+0x34>
mov    %rax,%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%rbx
jne    1140 <main+0x10>
jmp    117a <main+0x4a>
lea    0xe95(%rip),%rsi
mov    $0x1,%edi
mov    $0x2,%edx
call   1040 <write@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
br label %8
8: ; preds = %19, %2
%9 = load i64, i64* %6
%10 = icmp ult i64 %9, 5000
br i1 %10, label %11, label %22
11: ; preds = %8
%12 = call i8* @calloc(i32 16, i32 65536)
store i8* %12, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
store i32 0, i32* %3
br label %23
16: ; preds = %11
%17 = load i8*, i8** %7
%18 = call i32 @free(i8* %17)
br label %19
19: ; preds = %16
%20 = load i64, i64* %6
%21 = add i64 %20, 1
store i64 %21, i64* %6
br label %8
22: ; preds = %8
store i32 0, i32* %3
br label %23
23: ; preds = %22, %14
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@.str = [3 x i8] c"X\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
br label %3
3: ; preds = %2, %9
%4 = phi i64 [ 0, %2 ], [ %11, %9 ]
%5 = tail call dereferenceable_or_null(1048576) i8* @calloc(i32 16, i32 65536)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %13
9: ; preds = %3
%10 = tail call i32 @free(i8* %5)
%11 = add nuw nsw i64 %4, 1
%12 = icmp eq i64 %11, 5000
br i1 %12, label %13, label %3
13: ; preds = %9, %7
ret i32 0
}