#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int main() {
  int i = 0;
  for (; i < 5; ++i) {
    char *c = (char *)malloc(sizeof(char));
    if (NULL == c) {
      printf("malloc fails\n");
    } else {
      printf("malloc ok\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    118a <main+0x6a>
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    116b <main+0x4b>
lea    0xe9f(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x57>
lea    0xe9c(%rip),%rdi
call   1030 <printf@plt>
jmp    117c <main+0x5c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1136 <main+0x16>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
lea    0xed2(%rip),%r14
lea    0xec1(%rip),%rbx
mov    %rbx,%rdi
cmove  %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
mov    %rbx,%rdi
cmove  %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
mov    %rbx,%rdi
cmove  %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
mov    %rbx,%rdi
cmove  %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
cmove  %r14,%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"malloc fails\0A\00"
@.str.1 = [11 x i8] c"malloc ok\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
br label %4
4: ; preds = %17, %0
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 5
br i1 %6, label %7, label %20
7: ; preds = %4
%8 = call i64 @malloc(i32 1)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* null, %10
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
14: ; preds = %7
%15 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %12
br label %17
17: ; preds = %16
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
store i32 %19, i32* %2
br label %4
20: ; preds = %4
ret i32 0
}

LLVM IR (-O2):
@str = [10 x i8] c"malloc ok\00"
@str.2 = [13 x i8] c"malloc fails\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 1)
%2 = icmp eq i64 %1, 0
%3 = select i1 %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0)
%4 = tail call i32 @puts(i8* dereferenceable(1) %3)
%5 = tail call i64 @malloc(i32 1)
%6 = icmp eq i64 %5, 0
%7 = select i1 %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0)
%8 = tail call i32 @puts(i8* dereferenceable(1) %7)
%9 = tail call i64 @malloc(i32 1)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0)
%12 = tail call i32 @puts(i8* dereferenceable(1) %11)
%13 = tail call i64 @malloc(i32 1)
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0)
%16 = tail call i32 @puts(i8* dereferenceable(1) %15)
%17 = tail call i64 @malloc(i32 1)
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0)
%20 = tail call i32 @puts(i8* dereferenceable(1) %19)
ret i32 0
}