#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*/ mem_init();
int /*<<< orphan*/ mm_free(char *);
scalar_t__ mm_init();
char *mm_malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int main(void) {
  mem_init();
  if (mm_init() < 0)
    return -1;
  char *buf = mm_malloc(1);
  printf("buf : %p\n", buf);
  if (buf)
    mm_free(buf);
  char *buf2 = mm_malloc(4);
  printf("buf2: %p\n", buf2);
  if (buf2)
    mm_free(buf2);
  char *buf3 = mm_malloc(8);
  printf("buf3: %p\n", buf3);
  char *buf4 = mm_malloc(12);
  printf("buf4: %p\n", buf4);
  if (buf3)
    mm_free(buf3);
  if (buf4)
    mm_free(buf4);
  buf = mm_malloc(4);
  printf("buf : %p\n", buf);
  buf2 = mm_malloc(9);
  printf("buf2: %p\n", buf2);
  if (buf)
    mm_free(buf);
  if (buf2)
    mm_free(buf2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <mem_init@plt>
mov    $0x0,%al
call   1050 <mm_init@plt>
cmp    $0x0,%rax
jge    1183 <main+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <main+0x166>
mov    $0x1,%edi
call   1030 <mm_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe64(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     11b5 <main+0x65>
mov    -0x10(%rbp),%rdi
call   1070 <mm_free@plt>
mov    $0x4,%edi
call   1030 <mm_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     11e7 <main+0x97>
mov    -0x18(%rbp),%rdi
call   1070 <mm_free@plt>
mov    $0x8,%edi
call   1030 <mm_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
mov    $0xc,%edi
call   1030 <mm_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     1237 <main+0xe7>
mov    -0x20(%rbp),%rdi
call   1070 <mm_free@plt>
cmpq   $0x0,-0x28(%rbp)
je     124b <main+0xfb>
mov    -0x28(%rbp),%rdi
call   1070 <mm_free@plt>
mov    $0x4,%edi
call   1030 <mm_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd9c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x9,%edi
call   1030 <mm_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xd88(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     129b <main+0x14b>
mov    -0x10(%rbp),%rdi
call   1070 <mm_free@plt>
cmpq   $0x0,-0x18(%rbp)
je     12af <main+0x15f>
mov    -0x18(%rbp),%rdi
call   1070 <mm_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1060 <mem_init@plt>
xor    %eax,%eax
call   1050 <mm_init@plt>
test   %rax,%rax
js     1265 <main+0x115>
mov    $0x1,%edi
call   1030 <mm_malloc@plt>
mov    %rax,%rbx
lea    0xe81(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
test   %rbx,%rbx
je     1194 <main+0x44>
mov    %rbx,%rdi
call   1070 <mm_free@plt>
mov    $0x4,%edi
call   1030 <mm_malloc@plt>
mov    %rax,%rbx
lea    0xe62(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
test   %rbx,%rbx
je     11bd <main+0x6d>
mov    %rbx,%rdi
call   1070 <mm_free@plt>
mov    $0x8,%edi
call   1030 <mm_malloc@plt>
mov    %rax,%r14
lea    0xe43(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xc,%edi
call   1030 <mm_malloc@plt>
mov    %rax,%rbx
lea    0xe31(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
test   %r14,%r14
je     1202 <main+0xb2>
mov    %r14,%rdi
call   1070 <mm_free@plt>
test   %rbx,%rbx
je     120f <main+0xbf>
mov    %rbx,%rdi
call   1070 <mm_free@plt>
mov    $0x4,%edi
call   1030 <mm_malloc@plt>
mov    %rax,%r14
lea    0xddd(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x9,%edi
call   1030 <mm_malloc@plt>
mov    %rax,%rbx
lea    0xdcb(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
test   %r14,%r14
je     1254 <main+0x104>
mov    %r14,%rdi
call   1070 <mm_free@plt>
xor    %ebp,%ebp
test   %rbx,%rbx
je     126a <main+0x11a>
mov    %rbx,%rdi
call   1070 <mm_free@plt>
jmp    126a <main+0x11a>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"buf : %p\0A\00"
@.str.1 = [10 x i8] c"buf2: %p\0A\00"
@.str.2 = [10 x i8] c"buf3: %p\0A\00"
@.str.3 = [10 x i8] c"buf4: %p\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i32 (...) @mem_init()
%7 = call i64 (...) @mm_init()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %65
10: ; preds = %0
%11 = call i8* @mm_malloc(i32 1)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i8*, i8** %2
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i8*, i8** %2
%18 = call i32 @mm_free(i8* %17)
br label %19
19: ; preds = %16, %10
%20 = call i8* @mm_malloc(i32 4)
store i8* %20, i8** %3
%21 = load i8*, i8** %3
%22 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %3
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8*, i8** %3
%27 = call i32 @mm_free(i8* %26)
br label %28
28: ; preds = %25, %19
%29 = call i8* @mm_malloc(i32 8)
store i8* %29, i8** %4
%30 = load i8*, i8** %4
%31 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = call i8* @mm_malloc(i32 12)
store i8* %32, i8** %5
%33 = load i8*, i8** %5
%34 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = load i8*, i8** %4
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %28
%38 = load i8*, i8** %4
%39 = call i32 @mm_free(i8* %38)
br label %40
40: ; preds = %37, %28
%41 = load i8*, i8** %5
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8*, i8** %5
%45 = call i32 @mm_free(i8* %44)
br label %46
46: ; preds = %43, %40
%47 = call i8* @mm_malloc(i32 4)
store i8* %47, i8** %2
%48 = load i8*, i8** %2
%49 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %48)
%50 = call i8* @mm_malloc(i32 9)
store i8* %50, i8** %3
%51 = load i8*, i8** %3
%52 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %51)
%53 = load i8*, i8** %2
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %58
55: ; preds = %46
%56 = load i8*, i8** %2
%57 = call i32 @mm_free(i8* %56)
br label %58
58: ; preds = %55, %46
%59 = load i8*, i8** %3
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i8*, i8** %3
%63 = call i32 @mm_free(i8* %62)
br label %64
64: ; preds = %61, %58
store i32 0, i32* %1
br label %65
65: ; preds = %64, %9
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [10 x i8] c"buf : %p\0A\00"
@.str.1 = [10 x i8] c"buf2: %p\0A\00"
@.str.2 = [10 x i8] c"buf3: %p\0A\00"
@.str.3 = [10 x i8] c"buf4: %p\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @mem_init()
%2 = tail call i64 (...) @mm_init()
%3 = icmp slt i64 %2, 0
br i1 %3, label %40, label %4
4: ; preds = %0
%5 = tail call i8* @mm_malloc(i32 1)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = icmp eq i8* %5, null
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 @mm_free(i8* %5)
br label %10
10: ; preds = %8, %4
%11 = tail call i8* @mm_malloc(i32 4)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = icmp eq i8* %11, null
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @mm_free(i8* %11)
br label %16
16: ; preds = %14, %10
%17 = tail call i8* @mm_malloc(i32 8)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = tail call i8* @mm_malloc(i32 12)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = icmp eq i8* %17, null
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = tail call i32 @mm_free(i8* %17)
br label %24
24: ; preds = %22, %16
%25 = icmp eq i8* %19, null
br i1 %25, label %28, label %26
26: ; preds = %24
%27 = tail call i32 @mm_free(i8* %19)
br label %28
28: ; preds = %26, %24
%29 = tail call i8* @mm_malloc(i32 4)
%30 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = tail call i8* @mm_malloc(i32 9)
%32 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = icmp eq i8* %29, null
br i1 %33, label %36, label %34
34: ; preds = %28
%35 = tail call i32 @mm_free(i8* %29)
br label %36
36: ; preds = %34, %28
%37 = icmp eq i8* %31, null
br i1 %37, label %40, label %38
38: ; preds = %36
%39 = tail call i32 @mm_free(i8* %31)
br label %40
40: ; preds = %36, %38, %0
%41 = phi i32 [ -1, %0 ], [ 0, %38 ], [ 0, %36 ]
ret i32 %41
}