#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 *);
char *malloc(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  size_t i;
  char *x;
  for (i = 0; i < 5000; i += 1) {
    if ((x = malloc(1 << 20)) == 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    11b0 <main+0x80>
mov    $0x100000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1196 <main+0x66>
mov    $0x1,%edi
lea    0xe80(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
movl   $0x0,-0x4(%rbp)
jmp    11b7 <main+0x87>
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    $0x100000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     115f <main+0x2f>
mov    %rax,%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%rbx
jne    1140 <main+0x10>
jmp    1175 <main+0x45>
lea    0xe9a(%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* @malloc(i32 1048576)
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* @malloc(i32 1048576)
%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
}