#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*/ printf(char *, int);
void test_malloc() {
  int i;
  char *p, *q;
  for (i = 0; i < 60; i++) {
    p = malloc(0x100000);
    q = malloc(0x100000);
    if (!p || !q) {
      printf("%d; malloc is broken!\n", i);
      for (;;)
        ;
    }
    free(p);
    free(q);
  }
}


x86 assembly (clang, -O0):
<test_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0x3c,-0x4(%rbp)
jge    11af <test_malloc+0x7f>
mov    $0x100000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x100000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117b <test_malloc+0x4b>
cmpq   $0x0,-0x18(%rbp)
jne    118f <test_malloc+0x5f>
mov    -0x4(%rbp),%esi
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
jmp    118a <test_malloc+0x5a>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    113f <test_malloc+0xf>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_malloc>:
push   %rbp
push   %r14
push   %rbx
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    $0x100000,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    $0x100000,%edi
call   1050 <malloc@plt>
test   %rbx,%rbx
je     1183 <test_malloc+0x53>
mov    %rax,%rbp
test   %rax,%rax
je     1183 <test_malloc+0x53>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
add    $0x1,%r14d
cmp    $0x3c,%r14d
jne    1140 <test_malloc+0x10>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe76(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
jmp    11a0 <test_malloc+0x70>

LLVM IR (-O0):
@.str = [23 x i8] c"%d; malloc is broken!\0A\00"
define void @test_malloc() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
br label %4
4: ; preds = %24, %0
%5 = load i32, i32* %1
%6 = icmp slt i32 %5, 60
br i1 %6, label %7, label %27
7: ; preds = %4
%8 = call i8* @malloc(i32 1048576)
store i8* %8, i8** %2
%9 = call i8* @malloc(i32 1048576)
store i8* %9, i8** %3
%10 = load i8*, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %12, %7
%16 = load i32, i32* %1
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %18, %15
br label %18
19: ; preds = %12
%20 = load i8*, i8** %2
%21 = call i32 @free(i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
br label %24
24: ; preds = %19
%25 = load i32, i32* %1
%26 = add nsw i32 %25, 1
store i32 %26, i32* %1
br label %4
27: ; preds = %4
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"%d; malloc is broken!\0A\00"
define void @test_malloc() {
br label %1
1: ; preds = %0, %11
%2 = phi i32 [ 0, %0 ], [ %14, %11 ]
%3 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%4 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%5 = icmp ne i8* %3, null
%6 = icmp ne i8* %4, null
%7 = and i1 %5, %6
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %10
10: ; preds = %10, %8
br label %10
11: ; preds = %1
%12 = tail call i32 @free(i8* %3)
%13 = tail call i32 @free(i8* %4)
%14 = add nuw nsw i32 %2, 1
%15 = icmp eq i32 %14, 60
br i1 %15, label %16, label %1
16: ; preds = %11
ret void
}