#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 atoi(char *);
void *best_fit_alloc(int);
int best_fit_count_extfrag(int);
int /*<<< orphan*/ best_fit_dealloc(void *);
int /*<<< orphan*/ best_fit_memory_init(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
void *worst_fit_alloc(int);
int worst_fit_count_extfrag(int);
int /*<<< orphan*/ worst_fit_dealloc(void *);
int /*<<< orphan*/ worst_fit_memory_init(int);
int main(int argc, char *argv[]) {
  int num = 0;
  int algo = 0;
  void *p, *q;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <0/1>. 0 for best fit and 1 for worst fit \n",
            argv[0]);
    exit(1);
  } else if (!strcmp(argv[1], "1") || !strcmp(argv[1], "0")) {
    algo = atoi(argv[1]);
  } else {
    fprintf(stderr, "Invalid argument, please specify 0 or 1\n");
    exit(1);
  }
  if (algo == 0) {
    best_fit_memory_init(1024);
    p = best_fit_alloc(8);
    printf("best fit: p=%p\n", p);
    if (p != NULL) {
      best_fit_dealloc(p);
    }
    num = best_fit_count_extfrag(4);
  } else if (algo == 1) {
    worst_fit_memory_init(1024);
    q = worst_fit_alloc(8);
    printf("worst fit: q=%p\n", q);
    if (q != NULL) {
      worst_fit_dealloc(q);
    }
    num = worst_fit_count_extfrag(4);
  } else {
    fprintf(stderr, "Should not reach here!\n");
    exit(1);
  }
  printf("num = %d\n", num);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1226 <main+0x56>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe01(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     1260 <main+0x90>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xde6(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1275 <main+0xa5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1296 <main+0xc6>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    129b <main+0xcb>
cmpl   $0x0,-0x18(%rbp)
jne    12f5 <main+0x125>
mov    $0x400,%edi
call   10a0 <best_fit_memory_init@plt>
mov    $0x8,%edi
call   1070 <best_fit_alloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xd99(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     12e3 <main+0x113>
mov    -0x20(%rbp),%rdi
call   1030 <best_fit_dealloc@plt>
mov    $0x4,%edi
call   1090 <best_fit_count_extfrag@plt>
mov    %eax,-0x14(%rbp)
jmp    1375 <main+0x1a5>
cmpl   $0x1,-0x18(%rbp)
jne    134f <main+0x17f>
mov    $0x400,%edi
call   10f0 <worst_fit_memory_init@plt>
mov    $0x8,%edi
call   1050 <worst_fit_alloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xd4f(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
je     133d <main+0x16d>
mov    -0x28(%rbp),%rdi
call   10d0 <worst_fit_dealloc@plt>
mov    $0x4,%edi
call   10b0 <worst_fit_count_extfrag@plt>
mov    %eax,-0x14(%rbp)
jmp    1370 <main+0x1a0>
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    1375 <main+0x1a5>
mov    -0x14(%rbp),%esi
lea    0xd1d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    12a9 <main+0xd9>
mov    0x8(%rsi),%rbx
lea    0xe51(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1208 <main+0x38>
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    12cd <main+0xfd>
mov    %rbx,%rdi
call   10c0 <atoi@plt>
test   %eax,%eax
je     125b <main+0x8b>
cmp    $0x1,%eax
jne    12df <main+0x10f>
mov    $0x400,%edi
call   10f0 <worst_fit_memory_init@plt>
mov    $0x8,%edi
call   1050 <worst_fit_alloc@plt>
mov    %rax,%rbx
lea    0xe38(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
test   %rbx,%rbx
je     124f <main+0x7f>
mov    %rbx,%rdi
call   10d0 <worst_fit_dealloc@plt>
mov    $0x4,%edi
call   10b0 <worst_fit_count_extfrag@plt>
jmp    1297 <main+0xc7>
mov    $0x400,%edi
call   10a0 <best_fit_memory_init@plt>
mov    $0x8,%edi
call   1070 <best_fit_alloc@plt>
mov    %rax,%rbx
lea    0xdea(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
test   %rbx,%rbx
je     128d <main+0xbd>
mov    %rbx,%rdi
call   1030 <best_fit_dealloc@plt>
mov    $0x4,%edi
call   1090 <best_fit_count_extfrag@plt>
mov    %eax,%esi
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd44(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
jmp    12ef <main+0x11f>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c"Usage: %s <0/1>. 0 for best fit and 1 for worst fit \0A\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"0\00"
@.str.3 = [41 x i8] c"Invalid argument, please specify 0 or 1\0A\00"
@.str.4 = [16 x i8] c"best fit: p=%p\0A\00"
@.str.5 = [17 x i8] c"worst fit: q=%p\0A\00"
@.str.6 = [24 x i8] c"Should not reach here!\0A\00"
@.str.7 = [10 x i8] c"num = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp ne i32 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %25, %19
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %7
br label %40
36: ; preds = %25
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %31
br label %41
41: ; preds = %40
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %41
%45 = call i32 @best_fit_memory_init(i32 1024)
%46 = call i8* @best_fit_alloc(i32 8)
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = ptrtoint i8* %47 to i32
%49 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i8*, i8** %8
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %44
%53 = load i8*, i8** %8
%54 = call i32 @best_fit_dealloc(i8* %53)
br label %55
55: ; preds = %52, %44
%56 = call i32 @best_fit_count_extfrag(i32 4)
store i32 %56, i32* %6
br label %78
57: ; preds = %41
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 1
br i1 %59, label %60, label %73
60: ; preds = %57
%61 = call i32 @worst_fit_memory_init(i32 1024)
%62 = call i8* @worst_fit_alloc(i32 8)
store i8* %62, i8** %9
%63 = load i8*, i8** %9
%64 = ptrtoint i8* %63 to i32
%65 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %64)
%66 = load i8*, i8** %9
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %71
68: ; preds = %60
%69 = load i8*, i8** %9
%70 = call i32 @worst_fit_dealloc(i8* %69)
br label %71
71: ; preds = %68, %60
%72 = call i32 @worst_fit_count_extfrag(i32 4)
store i32 %72, i32* %6
br label %77
73: ; preds = %57
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %71
br label %78
78: ; preds = %77, %55
%79 = load i32, i32* %6
%80 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %79)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c"Usage: %s <0/1>. 0 for best fit and 1 for worst fit \0A\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [2 x i8] c"0\00"
@.str.3 = [41 x i8] c"Invalid argument, please specify 0 or 1\0A\00"
@.str.4 = [16 x i8] c"best fit: p=%p\0A\00"
@.str.5 = [17 x i8] c"worst fit: q=%p\0A\00"
@.str.6 = [24 x i8] c"Should not reach here!\0A\00"
@.str.7 = [10 x i8] c"num = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @strcmp(i8* dereferenceable(1) %11, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call i32 @strcmp(i8* dereferenceable(1) %11, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14, %9
%18 = tail call i32 @atoi(i8* %11)
switch i32 %18, label %45 [
i32 0, label %23
i32 1, label %34
]
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = tail call i32 @best_fit_memory_init(i32 1024)
%25 = tail call i8* @best_fit_alloc(i32 8)
%26 = ptrtoint i8* %25 to i64
%27 = trunc i64 %26 to i32
%28 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = icmp eq i8* %25, null
br i1 %29, label %32, label %30
30: ; preds = %23
%31 = tail call i32 @best_fit_dealloc(i8* %25)
br label %32
32: ; preds = %30, %23
%33 = tail call i32 @best_fit_count_extfrag(i32 4)
br label %49
34: ; preds = %17
%35 = tail call i32 @worst_fit_memory_init(i32 1024)
%36 = tail call i8* @worst_fit_alloc(i32 8)
%37 = ptrtoint i8* %36 to i64
%38 = trunc i64 %37 to i32
%39 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = icmp eq i8* %36, null
br i1 %40, label %43, label %41
41: ; preds = %34
%42 = tail call i32 @worst_fit_dealloc(i8* %36)
br label %43
43: ; preds = %41, %34
%44 = tail call i32 @worst_fit_count_extfrag(i32 4)
br label %49
45: ; preds = %17
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %43, %32
%50 = phi i32 [ %33, %32 ], [ %44, %43 ]
%51 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %50)
ret i32 0
}