#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*/ aide();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char getchar();
int /*<<< orphan*/ heap_base();
char *memory_alloc(int);
int /*<<< orphan*/ memory_free(int /*<<< orphan*/);
int /*<<< orphan*/ memory_init();
int /*<<< orphan*/ print_free_blocks();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char buffer[128];
  char commande;
  char *adresse;
  int offset;
  int taille;
  aide();
  memory_init();
  while (1) {
    printf("? ");
    fflush(stdout);
    commande = getchar();
    switch (commande) {
    case 'a':
      scanf("%d", &taille);
      adresse = memory_alloc(taille);
      if (adresse == NULL)
        printf("Allocation failed\n");
      else
        printf("Memory allocated at %d\n", (int)(adresse - heap_base()));
      break;
    case 'f':
      scanf("%d", &offset);
      memory_free(heap_base() + offset);
    case 'p':
      print_free_blocks();
      break;
    case 'h':
      aide();
      break;
    case 'q':
      exit(0);
    default:
      fprintf(stderr, "Command not found !\n");
    }
    fgets(buffer, 128, stdin);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10e0 <aide@plt>
mov    $0x0,%al
call   1030 <memory_init@plt>
lea    0xe46(%rip),%rdi
xor    %eax,%eax
mov    %al,-0xb1(%rbp)
call   1060 <printf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    -0xb1(%rbp),%al
call   1090 <getchar@plt>
mov    %al,-0x91(%rbp)
movsbl -0x91(%rbp),%eax
add    $0xffffff9f,%eax
mov    %eax,%ecx
mov    %rcx,-0xb0(%rbp)
sub    $0x10,%eax
ja     1327 <main+0x157>
mov    -0xb0(%rbp),%rax
lea    0xdac(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xde3(%rip),%rdi
lea    -0xa8(%rbp),%rsi
call   10c0 <scanf@plt>
mov    -0xa8(%rbp),%edi
call   1070 <memory_alloc@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jne    12a3 <main+0xd3>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12dc <main+0x10c>
mov    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    $0x0,%al
call   1050 <heap_base@plt>
mov    %eax,%ecx
mov    -0xc0(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %eax,%esi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    133c <main+0x16c>
lea    0xd5f(%rip),%rdi
lea    -0xa4(%rbp),%rsi
call   10c0 <scanf@plt>
mov    $0x0,%al
call   1050 <heap_base@plt>
mov    %eax,%edi
add    -0xa4(%rbp),%edi
call   1040 <memory_free@plt>
mov    $0x0,%al
call   10d0 <print_free_blocks@plt>
jmp    133c <main+0x16c>
mov    $0x0,%al
call   10e0 <aide@plt>
jmp    133c <main+0x16c>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
call   10a0 <fprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2c9e(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1080 <fgets@plt>
jmp    11f7 <main+0x27>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
xor    %eax,%eax
call   10f0 <aide@plt>
xor    %eax,%eax
call   1030 <memory_init@plt>
lea    0xe3e(%rip),%r15
mov    0x2dc3(%rip),%rbp
mov    0x2dd4(%rip),%r13
lea    0x10(%rsp),%r12
lea    0xde0(%rip),%rbx
lea    0xe20(%rip),%r14
jmp    1251 <main+0x71>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   10b0 <fprintf@plt>
xchg   %ax,%ax
mov    0x0(%r13),%edx
mov    %r12,%rdi
mov    $0x80,%esi
call   1090 <fgets@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x0(%rbp),%edi
call   10c0 <fflush@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
movsbl %al,%eax
add    $0xffffff9f,%eax
cmp    $0x10,%eax
ja     1229 <main+0x49>
movslq (%rbx,%rax,4),%rax
add    %rbx,%rax
jmp    *%rax
mov    %r15,%rbx
mov    %r14,%r15
mov    %r14,%rdi
lea    0x8(%rsp),%rsi
call   10d0 <scanf@plt>
mov    0x8(%rsp),%edi
call   1080 <memory_alloc@plt>
test   %rax,%rax
je     12f6 <main+0x116>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <heap_base@plt>
sub    %eax,%r14d
lea    0xd97(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1302 <main+0x122>
mov    %r14,%rdi
lea    0xc(%rsp),%rsi
call   10d0 <scanf@plt>
xor    %eax,%eax
call   1060 <heap_base@plt>
add    0xc(%rsp),%eax
mov    %eax,%edi
call   1050 <memory_free@plt>
xor    %eax,%eax
call   10e0 <print_free_blocks@plt>
jmp    1240 <main+0x60>
xor    %eax,%eax
call   10f0 <aide@plt>
jmp    1240 <main+0x60>
lea    0xd7a(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%r14
mov    %rbx,%r15
lea    0xcf1(%rip),%rbx
jmp    1240 <main+0x60>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"? \00"
@stdout = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [19 x i8] c"Allocation failed\0A\00"
@.str.3 = [24 x i8] c"Memory allocated at %d\0A\00"
@stderr = global i32 0
@.str.4 = [21 x i8] c"Command not found !\0A\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [128 x i8]
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @aide()
%12 = call i32 (...) @memory_init()
br label %13
13: ; preds = %2, %52
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = call signext i8 (...) @getchar()
store i8 %17, i8* %7
%18 = load i8, i8* %7
%19 = sext i8 %18 to i32
switch i32 %19, label %49 [
i32 97, label %20
i32 102, label %37
i32 112, label %43
i32 104, label %45
i32 113, label %47
]
20: ; preds = %13
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %10)
%22 = load i32, i32* %10
%23 = call i8* @memory_alloc(i32 %22)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %36
28: ; preds = %20
%29 = load i8*, i8** %8
%30 = call i32 (...) @heap_base()
%31 = sext i32 %30 to i64
%32 = sub i64 0, %31
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = ptrtoint i8* %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %28, %26
br label %52
37: ; preds = %13
%38 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%39 = call i32 (...) @heap_base()
%40 = load i32, i32* %9
%41 = add nsw i32 %39, %40
%42 = call i32 @memory_free(i32 %41)
br label %43
43: ; preds = %13, %37
%44 = call i32 (...) @print_free_blocks()
br label %52
45: ; preds = %13
%46 = call i32 (...) @aide()
br label %52
47: ; preds = %13
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %13
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %49, %45, %43, %36
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%54 = load i32, i32* @stdin
%55 = call i32 @fgets(i8* %53, i32 128, i32 %54)
br label %13
}

LLVM IR (-O2):
@.str = [3 x i8] c"? \00"
@stdout = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [24 x i8] c"Memory allocated at %d\0A\00"
@stderr = global i32 0
@.str.4 = [21 x i8] c"Command not found !\0A\00"
@stdin = global i32 0
@str = [18 x i8] c"Allocation failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i32 (...) @aide()
%10 = tail call i32 (...) @memory_init()
br label %11
11: ; preds = %47, %2
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stdout
%14 = call i32 @fflush(i32 %13)
%15 = call signext i8 (...) @getchar()
%16 = sext i8 %15 to i32
switch i32 %16, label %44 [
i32 97, label %17
i32 102, label %32
i32 112, label %38
i32 104, label %40
i32 113, label %42
]
17: ; preds = %11
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%19 = load i32, i32* %5
%20 = call i8* @memory_alloc(i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %47
24: ; preds = %17
%25 = call i32 (...) @heap_base()
%26 = sext i32 %25 to i64
%27 = sub nsw i64 0, %26
%28 = getelementptr inbounds i8, i8* %20, i64 %27
%29 = ptrtoint i8* %28 to i64
%30 = trunc i64 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %47
32: ; preds = %11
%33 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%34 = call i32 (...) @heap_base()
%35 = load i32, i32* %4
%36 = add nsw i32 %35, %34
%37 = call i32 @memory_free(i32 %36)
br label %38
38: ; preds = %11, %32
%39 = call i32 (...) @print_free_blocks()
br label %47
40: ; preds = %11
%41 = call i32 (...) @aide()
br label %47
42: ; preds = %11
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %11
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %22, %24, %44, %40, %38
%48 = load i32, i32* @stdin
%49 = call i32 @fgets(i8* %6, i32 128, i32 %48)
br label %11
}