#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(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *);
int main() {
  int i;
  for (i = 0; i < 1; i++) {
    int *ptr = malloc(sizeof(int));
    if (ptr == NULL) {
      printf("Memory failed to allocate!\n");
      return 1;
    }
    *ptr = 4;
    free(ptr);
  }
  printf("Memory was allocated, used, and freed!\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   $0x1,-0x8(%rbp)
jge    11a2 <main+0x72>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1181 <main+0x51>
lea    0xe90(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b5 <main+0x85>
mov    -0x10(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1146 <main+0x16>
lea    0xe73(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x4,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1159 <main+0x29>
movl   $0x4,(%rax)
mov    %rax,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
lea    0xea9(%rip),%rdi
jmp    1165 <main+0x35>
mov    $0x1,%ebx
lea    0xec2(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Memory failed to allocate!\0A\00"
@.str.1 = [40 x i8] c"Memory was allocated, used, and freed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
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, 1
br i1 %6, label %7, label %20
7: ; preds = %4
%8 = call i32* @malloc(i32 4)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %22
13: ; preds = %7
%14 = load i32*, i32** %3
store i32 4, i32* %14
%15 = load i32*, i32** %3
%16 = call i32 @free(i32* %15)
br label %17
17: ; preds = %13
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
store i32 %19, i32* %2
br label %4
20: ; preds = %4
%21 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %22
22: ; preds = %20, %11
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@str = [39 x i8] c"Memory was allocated, used, and freed!\00"
@str.2 = [27 x i8] c"Memory failed to allocate!\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 4)
%2 = icmp eq i32* %1, null
br i1 %2, label %5, label %3
3: ; preds = %0
store i32 4, i32* %1
%4 = tail call i32 @free(i32* %1)
br label %5
5: ; preds = %0, %3
%6 = phi i8* [ getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.2, i64 0, i64 0), %0 ]
%7 = phi i32 [ 0, %3 ], [ 1, %0 ]
%8 = tail call i32 @puts(i8* dereferenceable(1) %6)
ret i32 %7
}