#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(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int main() {
  for (size_t i = 0; i < 1000; i++) {
    char *b = (char *)malloc(1000);
    if (!b) {
      printf("malloc appears to be broken\n");
      exit(9);
    }
    for (size_t j = 0; j < 1000; j++) {
      b[j] = 0;
    }
    free(b);
  }
  printf("malloc appears to work\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x3e8,-0x10(%rbp)
jae    11e1 <main+0xa1>
mov    $0x3e8,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <main+0x54>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x9,%edi
call   1060 <exit@plt>
movq   $0x0,-0x20(%rbp)
cmpq   $0x3e8,-0x20(%rbp)
jae    11c7 <main+0x87>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119c <main+0x5c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1157 <main+0x17>
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x3e8,%r14d
nopw   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a2 <main+0x52>
mov    %rax,%rbx
mov    $0x3e8,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%r14
jne    1160 <main+0x10>
lea    0xe6a(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe6e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x9,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"malloc appears to be broken\0A\00"
@.str.1 = [24 x i8] c"malloc appears to work\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
store i64 0, i64* %2
br label %5
5: ; preds = %30, %0
%6 = load i64, i64* %2
%7 = icmp ult i64 %6, 1000
br i1 %7, label %8, label %33
8: ; preds = %5
%9 = call i64 @malloc(i32 1000)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 9)
unreachable
16: ; preds = %8
store i64 0, i64* %4
br label %17
17: ; preds = %24, %16
%18 = load i64, i64* %4
%19 = icmp ult i64 %18, 1000
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i8*, i8** %3
%22 = load i64, i64* %4
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %20
%25 = load i64, i64* %4
%26 = add i64 %25, 1
store i64 %26, i64* %4
br label %17
27: ; preds = %17
%28 = load i8*, i8** %3
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %27
%31 = load i64, i64* %2
%32 = add i64 %31, 1
store i64 %32, i64* %2
br label %5
33: ; preds = %5
%34 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@str = [23 x i8] c"malloc appears to work\00"
@str.2 = [28 x i8] c"malloc appears to be broken\00"
define i32 @main() {
br label %4
1: ; preds = %9
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @exit(i32 0)
unreachable
4: ; preds = %0, %9
%5 = phi i64 [ 0, %0 ], [ %11, %9 ]
%6 = tail call i64 @malloc(i32 1000)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = tail call i32 @free(i8* %7)
%11 = add nuw nsw i64 %5, 1
%12 = icmp eq i64 %11, 1000
br i1 %12, label %1, label %4
13: ; preds = %4
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 9)
unreachable
}