#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__ errno;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int mb_query();
scalar_t__ mb_register(int /*<<< orphan*/);
int mb_request_pages(int);
int mb_reserve_pages(int);
int /*<<< orphan*/ mb_return_pages(int);
int /*<<< orphan*/ mb_status();
int /*<<< orphan*/ mb_terminate();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int sscanf(char *, char *, char *, int *);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
int main(void) {
  char buf[500];
  char command[500];
  int pages;
  int my_pages = 0;
  if (mb_register(0) < 0) {
    printf("Failed to register\n");
    return -1;
  }
  do {
    int conv = 0;
    memset(command, 0, 500);
    memset(buf, 0, 500);
    pages = 0;
    printf("Enter Command:\n");
    do {
      conv += read(fileno(stdin), &buf[conv], sizeof(char));
      if (conv == 0 && errno == 0)
        break;
    } while (buf[conv - 1] != '\n');
    buf[conv] = '\0';
    conv = sscanf(buf, "%s %d", command, &pages);
    if (conv == 2 && 0 == strcmp(command, "request")) {
      int reaped = 0;
      printf("Requesting %d pages\n", pages);
      reaped = mb_request_pages(pages);
      if (reaped > 0) {
        my_pages += reaped;
        printf("Got %d pages.  Total: %d\n", reaped, my_pages);
      } else {
        printf("request pages returns %d\n", reaped);
      }
    } else if (conv == 2 && 0 == strcmp(command, "reserve")) {
      int reaped = 0;
      printf("Requesting %d pages\n", pages);
      reaped = mb_reserve_pages(pages);
      if (reaped > 0) {
        my_pages += reaped;
        printf("Got %d pages.  Total: %d\n", reaped, my_pages);
      } else {
        printf("request pages returns %d\n", reaped);
      }
    } else if (conv == 2 && 0 == strcmp(command, "return")) {
      int ret_pages = my_pages < pages ? my_pages : pages;
      printf("Returning %d pages\n", ret_pages);
      if (!mb_return_pages(ret_pages))
        my_pages -= ret_pages;
      printf("Total Pages: %d\n", my_pages);
    } else if (conv == 1 && 0 == strcmp(command, "query")) {
      int mb_pages = mb_query();
      if (mb_pages >= 0)
        printf("membroker pages: %d.  My Pages: %d\n", mb_pages, my_pages);
    } else if (conv == 1 && 0 == strcmp(command, "status")) {
      mb_status();
    } else if (conv == 1 && 0 != strcmp(command, "end")) {
      printf("Unknown command\nValid Commands: reserve|request|return [pages], "
             "query, end\n");
    }
  } while (0 != strcmp(command, "end"));
  if (my_pages > 0) {
    mb_return_pages(my_pages);
  }
  mb_terminate();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x408(%rbp)
xor    %edi,%edi
call   10f0 <mb_register@plt>
cmp    $0x0,%rax
jge    1217 <main+0x47>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1684 <main+0x4b4>
jmp    121c <main+0x4c>
movl   $0x0,-0x40c(%rbp)
lea    -0x400(%rbp),%rdi
xor    %esi,%esi
mov    $0x1f4,%edx
call   1050 <memset@plt>
lea    -0x200(%rbp),%rdi
xor    %esi,%esi
mov    $0x1f4,%edx
call   1050 <memset@plt>
movl   $0x0,-0x404(%rbp)
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1090 <fileno@plt>
mov    %eax,%edi
movslq -0x40c(%rbp),%rax
lea    -0x200(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %rax,%rcx
movslq -0x40c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x40c(%rbp)
cmpl   $0x0,-0x40c(%rbp)
jne    12c5 <main+0xf5>
mov    0x2d22(%rip),%rax
cmpq   $0x0,(%rax)
jne    12c5 <main+0xf5>
jmp    12e6 <main+0x116>
jmp    12ca <main+0xfa>
mov    -0x40c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x200(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1264 <main+0x94>
movslq -0x40c(%rbp),%rax
movb   $0x0,-0x200(%rbp,%rax,1)
lea    -0x200(%rbp),%rdi
lea    -0x400(%rbp),%rdx
lea    0xd1a(%rip),%rsi
lea    -0x404(%rbp),%rcx
call   10c0 <sscanf@plt>
mov    %eax,-0x40c(%rbp)
cmpl   $0x2,-0x40c(%rbp)
jne    13d0 <main+0x200>
lea    -0x400(%rbp),%rdi
lea    0xcf3(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    13d0 <main+0x200>
movl   $0x0,-0x410(%rbp)
mov    -0x404(%rbp),%esi
lea    0xcd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x404(%rbp),%edi
call   10d0 <mb_request_pages@plt>
mov    %eax,-0x410(%rbp)
cmpl   $0x0,-0x410(%rbp)
jle    13b7 <main+0x1e7>
mov    -0x410(%rbp),%eax
add    -0x408(%rbp),%eax
mov    %eax,-0x408(%rbp)
mov    -0x410(%rbp),%esi
mov    -0x408(%rbp),%edx
lea    0xc9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13cb <main+0x1fb>
mov    -0x410(%rbp),%esi
lea    0xc9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1638 <main+0x468>
cmpl   $0x2,-0x40c(%rbp)
jne    1484 <main+0x2b4>
lea    -0x400(%rbp),%rdi
lea    0xc90(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1484 <main+0x2b4>
movl   $0x0,-0x414(%rbp)
mov    -0x404(%rbp),%esi
lea    0xc1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x404(%rbp),%edi
call   1060 <mb_reserve_pages@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jle    146b <main+0x29b>
mov    -0x414(%rbp),%eax
add    -0x408(%rbp),%eax
mov    %eax,-0x408(%rbp)
mov    -0x414(%rbp),%esi
mov    -0x408(%rbp),%edx
lea    0xbe8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    147f <main+0x2af>
mov    -0x414(%rbp),%esi
lea    0xbe9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1633 <main+0x463>
cmpl   $0x2,-0x40c(%rbp)
jne    1542 <main+0x372>
lea    -0x400(%rbp),%rdi
lea    0xbe4(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1542 <main+0x372>
mov    -0x408(%rbp),%eax
cmp    -0x404(%rbp),%eax
jge    14d5 <main+0x305>
mov    -0x408(%rbp),%eax
mov    %eax,-0x420(%rbp)
jmp    14e1 <main+0x311>
mov    -0x404(%rbp),%eax
mov    %eax,-0x420(%rbp)
mov    -0x420(%rbp),%eax
mov    %eax,-0x418(%rbp)
mov    -0x418(%rbp),%esi
lea    0xb90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x418(%rbp),%edi
call   10a0 <mb_return_pages@plt>
cmp    $0x0,%eax
jne    1529 <main+0x359>
mov    -0x418(%rbp),%ecx
mov    -0x408(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x408(%rbp)
mov    -0x408(%rbp),%esi
lea    0xb68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    162e <main+0x45e>
cmpl   $0x1,-0x40c(%rbp)
jne    15a9 <main+0x3d9>
lea    -0x400(%rbp),%rdi
lea    0xb52(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    15a9 <main+0x3d9>
mov    $0x0,%al
call   10e0 <mb_query@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jl     15a4 <main+0x3d4>
mov    -0x41c(%rbp),%esi
mov    -0x408(%rbp),%edx
lea    0xb18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1629 <main+0x459>
cmpl   $0x1,-0x40c(%rbp)
jne    15e3 <main+0x413>
lea    -0x400(%rbp),%rdi
lea    0xb15(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    15e3 <main+0x413>
mov    $0x0,%al
call   1030 <mb_status@plt>
jmp    1624 <main+0x454>
cmpl   $0x1,-0x40c(%rbp)
jne    161f <main+0x44f>
lea    -0x400(%rbp),%rdi
lea    0xae2(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     161f <main+0x44f>
lea    0xacc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1624 <main+0x454>
jmp    1629 <main+0x459>
jmp    162e <main+0x45e>
jmp    1633 <main+0x463>
jmp    1638 <main+0x468>
jmp    163d <main+0x46d>
lea    -0x400(%rbp),%rdi
lea    0xa95(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    121c <main+0x4c>
cmpl   $0x0,-0x408(%rbp)
jle    1676 <main+0x4a6>
mov    -0x408(%rbp),%edi
call   10a0 <mb_return_pages@plt>
mov    $0x0,%al
call   10b0 <mb_terminate@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
xor    %edi,%edi
call   1100 <mb_register@plt>
test   %rax,%rax
js     14b7 <main+0x2d7>
lea    0x210(%rsp),%rbx
lea    0x10(%rsp),%rbp
mov    0x2dd3(%rip),%r13
movabs $0xffffffff00000000,%r14
ff ff ff
movl   $0x0,0x8(%rsp)
jmp    125b <main+0x7b>
mov    0x8(%rsp),%edx
add    %eax,%edx
lea    0xded(%rip),%rdi
mov    %eax,%esi
mov    %edx,0x8(%rsp)
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
lea    0xec5(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     149f <main+0x2bf>
mov    %rbx,%r15
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1f4,%edx
call   1060 <memset@plt>
mov    %rbp,%rbx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x1f4,%edx
call   1060 <memset@plt>
movl   $0x0,0xc(%rsp)
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
xor    %r12d,%r12d
jmp    12af <main+0xcf>
nopl   0x0(%rax,%rax,1)
lea    (%rax,%r14,1),%rcx
sar    $0x20,%rcx
cmpb   $0xa,0x10(%rsp,%rcx,1)
je     12ee <main+0x10e>
mov    0x0(%r13),%edi
call   10a0 <fileno@plt>
movslq %r12d,%rbp
lea    (%rsp,%rbp,1),%rsi
add    $0x10,%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %rax,%r12
add    %rbp,%r12
mov    %r12,%rax
shl    $0x20,%rax
test   %r12d,%r12d
jne    12a0 <main+0xc0>
mov    0x2cf0(%rip),%rcx
cmpq   $0x0,(%rcx)
jne    12a0 <main+0xc0>
sar    $0x20,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rbp
mov    %rbx,%rdi
lea    0xcfc(%rip),%rsi
mov    %r15,%rbx
mov    %r15,%rdx
lea    0xc(%rsp),%rcx
call   10d0 <sscanf@plt>
cmp    $0x1,%eax
je     13c0 <main+0x1e0>
cmp    $0x2,%eax
jne    1243 <main+0x63>
mov    %rbx,%rdi
lea    0xcd6(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1415 <main+0x235>
mov    %rbx,%rdi
lea    0xd0f(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     145b <main+0x27b>
mov    %rbx,%rdi
lea    0xcff(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1243 <main+0x63>
mov    0xc(%rsp),%r12d
mov    0x8(%rsp),%r15d
cmp    %r12d,%r15d
cmovl  %r15d,%r12d
lea    0xce0(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
call   10b0 <mb_return_pages@plt>
test   %eax,%eax
mov    $0x0,%eax
cmovne %eax,%r12d
sub    %r12d,%r15d
lea    0xccd(%rip),%rdi
mov    %r15d,0x8(%rsp)
mov    %r15d,%esi
jmp    1487 <main+0x2a7>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
lea    0xcc1(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     143a <main+0x25a>
mov    %rbx,%rdi
lea    0xcd7(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1493 <main+0x2b3>
mov    %rbx,%rdi
lea    0xd1c(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1243 <main+0x63>
lea    0xcc0(%rip),%rdi
call   1030 <puts@plt>
jmp    1243 <main+0x63>
mov    0xc(%rsp),%esi
lea    0xbee(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   10e0 <mb_request_pages@plt>
test   %eax,%eax
jg     1229 <main+0x49>
jmp    147e <main+0x29e>
xor    %eax,%eax
call   10f0 <mb_query@plt>
test   %eax,%eax
js     1243 <main+0x63>
lea    0xc41(%rip),%rdi
mov    %eax,%esi
mov    0x8(%rsp),%edx
jmp    123c <main+0x5c>
mov    0xc(%rsp),%esi
lea    0xba8(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <mb_reserve_pages@plt>
test   %eax,%eax
jg     1229 <main+0x49>
lea    0xbb8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1243 <main+0x63>
xor    %eax,%eax
call   1040 <mb_status@plt>
jmp    1243 <main+0x63>
mov    0x8(%rsp),%edi
test   %edi,%edi
jle    14ac <main+0x2cc>
call   10b0 <mb_return_pages@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <mb_terminate@plt>
jmp    14c8 <main+0x2e8>
lea    0xc58(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Failed to register\0A\00"
@.str.1 = [16 x i8] c"Enter Command:\0A\00"
@stdin = global i32 0
@errno = global i64 0
@.str.2 = [6 x i8] c"%s %d\00"
@.str.3 = [8 x i8] c"request\00"
@.str.4 = [21 x i8] c"Requesting %d pages\0A\00"
@.str.5 = [26 x i8] c"Got %d pages. Total: %d\0A\00"
@.str.6 = [26 x i8] c"request pages returns %d\0A\00"
@.str.7 = [8 x i8] c"reserve\00"
@.str.8 = [7 x i8] c"return\00"
@.str.9 = [20 x i8] c"Returning %d pages\0A\00"
@.str.10 = [17 x i8] c"Total Pages: %d\0A\00"
@.str.11 = [6 x i8] c"query\00"
@.str.12 = [36 x i8] c"membroker pages: %d. My Pages: %d\0A\00"
@.str.13 = [7 x i8] c"status\00"
@.str.14 = [4 x i8] c"end\00"
@.str.15 = [76 x i8] c"Unknown command\0AValid Commands: reserve|request|return [pages], query, end\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [500 x i8]
%3 = alloca [500 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%11 = call i64 @mb_register(i32 0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %185
15: ; preds = %0
br label %16
16: ; preds = %173, %15
store i32 0, i32* %6
%17 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 500)
%19 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 500)
store i32 0, i32* %4
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %40, %16
%23 = load i32, i32* @stdin
%24 = call i32 @fileno(i32 %23)
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 %26
%28 = call i64 @read(i32 %24, i8* %27, i32 1)
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = add nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %22
%36 = load i64, i64* @errno
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
br label %48
39: ; preds = %35, %22
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 10
br i1 %47, label %22, label %48
48: ; preds = %40, %38
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 %50
store i8 0, i8* %51
%52 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%53 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%54 = call i32 @sscanf(i8* %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %53, i32* %4)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = icmp eq i32 %55, 2
br i1 %56, label %57, label %79
57: ; preds = %48
%58 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%59 = call i64 @strcmp(i8* %58, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%60 = icmp eq i64 0, %59
br i1 %60, label %61, label %79
61: ; preds = %57
store i32 0, i32* %7
%62 = load i32, i32* %4
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %4
%65 = call i32 @mb_request_pages(i32 %64)
store i32 %65, i32* %7
%66 = load i32, i32* %7
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %61
%69 = load i32, i32* %7
%70 = load i32, i32* %5
%71 = add nsw i32 %70, %69
store i32 %71, i32* %5
%72 = load i32, i32* %7
%73 = load i32, i32* %5
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %72, i32 %73)
br label %78
75: ; preds = %61
%76 = load i32, i32* %7
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %76)
br label %78
78: ; preds = %75, %68
br label %172
79: ; preds = %57, %48
%80 = load i32, i32* %6
%81 = icmp eq i32 %80, 2
br i1 %81, label %82, label %104
82: ; preds = %79
%83 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%84 = call i64 @strcmp(i8* %83, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%85 = icmp eq i64 0, %84
br i1 %85, label %86, label %104
86: ; preds = %82
store i32 0, i32* %8
%87 = load i32, i32* %4
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %87)
%89 = load i32, i32* %4
%90 = call i32 @mb_reserve_pages(i32 %89)
store i32 %90, i32* %8
%91 = load i32, i32* %8
%92 = icmp sgt i32 %91, 0
br i1 %92, label %93, label %100
93: ; preds = %86
%94 = load i32, i32* %8
%95 = load i32, i32* %5
%96 = add nsw i32 %95, %94
store i32 %96, i32* %5
%97 = load i32, i32* %8
%98 = load i32, i32* %5
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %97, i32 %98)
br label %103
100: ; preds = %86
%101 = load i32, i32* %8
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %101)
br label %103
103: ; preds = %100, %93
br label %171
104: ; preds = %82, %79
%105 = load i32, i32* %6
%106 = icmp eq i32 %105, 2
br i1 %106, label %107, label %133
107: ; preds = %104
%108 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%109 = call i64 @strcmp(i8* %108, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%110 = icmp eq i64 0, %109
br i1 %110, label %111, label %133
111: ; preds = %107
%112 = load i32, i32* %5
%113 = load i32, i32* %4
%114 = icmp slt i32 %112, %113
br i1 %114, label %115, label %117
115: ; preds = %111
%116 = load i32, i32* %5
br label %119
117: ; preds = %111
%118 = load i32, i32* %4
br label %119
119: ; preds = %117, %115
%120 = phi i32 [ %116, %115 ], [ %118, %117 ]
store i32 %120, i32* %9
%121 = load i32, i32* %9
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %121)
%123 = load i32, i32* %9
%124 = call i32 @mb_return_pages(i32 %123)
%125 = icmp ne i32 %124, 0
br i1 %125, label %130, label %126
126: ; preds = %119
%127 = load i32, i32* %9
%128 = load i32, i32* %5
%129 = sub nsw i32 %128, %127
store i32 %129, i32* %5
br label %130
130: ; preds = %126, %119
%131 = load i32, i32* %5
%132 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i32 %131)
br label %170
133: ; preds = %107, %104
%134 = load i32, i32* %6
%135 = icmp eq i32 %134, 1
br i1 %135, label %136, label %149
136: ; preds = %133
%137 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%138 = call i64 @strcmp(i8* %137, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
%139 = icmp eq i64 0, %138
br i1 %139, label %140, label %149
140: ; preds = %136
%141 = call i32 (...) @mb_query()
store i32 %141, i32* %10
%142 = load i32, i32* %10
%143 = icmp sge i32 %142, 0
br i1 %143, label %144, label %148
144: ; preds = %140
%145 = load i32, i32* %10
%146 = load i32, i32* %5
%147 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.12, i64 0, i64 0), i32 %145, i32 %146)
br label %148
148: ; preds = %144, %140
br label %169
149: ; preds = %136, %133
%150 = load i32, i32* %6
%151 = icmp eq i32 %150, 1
br i1 %151, label %152, label %158
152: ; preds = %149
%153 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%154 = call i64 @strcmp(i8* %153, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
%155 = icmp eq i64 0, %154
br i1 %155, label %156, label %158
156: ; preds = %152
%157 = call i32 (...) @mb_status()
br label %168
158: ; preds = %152, %149
%159 = load i32, i32* %6
%160 = icmp eq i32 %159, 1
br i1 %160, label %161, label %167
161: ; preds = %158
%162 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%163 = call i64 @strcmp(i8* %162, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.14, i64 0, i64 0))
%164 = icmp ne i64 0, %163
br i1 %164, label %165, label %167
165: ; preds = %161
%166 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.15, i64 0, i64 0))
br label %167
167: ; preds = %165, %161, %158
br label %168
168: ; preds = %167, %156
br label %169
169: ; preds = %168, %148
br label %170
170: ; preds = %169, %130
br label %171
171: ; preds = %170, %103
br label %172
172: ; preds = %171, %78
br label %173
173: ; preds = %172
%174 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%175 = call i64 @strcmp(i8* %174, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.14, i64 0, i64 0))
%176 = icmp ne i64 0, %175
br i1 %176, label %16, label %177
177: ; preds = %173
%178 = load i32, i32* %5
%179 = icmp sgt i32 %178, 0
br i1 %179, label %180, label %183
180: ; preds = %177
%181 = load i32, i32* %5
%182 = call i32 @mb_return_pages(i32 %181)
br label %183
183: ; preds = %180, %177
%184 = call i32 (...) @mb_terminate()
store i32 0, i32* %1
br label %185
185: ; preds = %183, %13
%186 = load i32, i32* %1
ret i32 %186
}

LLVM IR (-O2):
@stdin = global i32 0
@errno = global i64 0
@.str.2 = [6 x i8] c"%s %d\00"
@.str.3 = [8 x i8] c"request\00"
@.str.4 = [21 x i8] c"Requesting %d pages\0A\00"
@.str.5 = [26 x i8] c"Got %d pages. Total: %d\0A\00"
@.str.6 = [26 x i8] c"request pages returns %d\0A\00"
@.str.7 = [8 x i8] c"reserve\00"
@.str.8 = [7 x i8] c"return\00"
@.str.9 = [20 x i8] c"Returning %d pages\0A\00"
@.str.10 = [17 x i8] c"Total Pages: %d\0A\00"
@.str.11 = [6 x i8] c"query\00"
@.str.12 = [36 x i8] c"membroker pages: %d. My Pages: %d\0A\00"
@.str.13 = [7 x i8] c"status\00"
@.str.14 = [4 x i8] c"end\00"
@str = [15 x i8] c"Enter Command:\00"
@str.16 = [75 x i8] c"Unknown command\0AValid Commands: reserve|request|return [pages], query, end\00"
@str.17 = [19 x i8] c"Failed to register\00"
define i32 @main() {
%1 = alloca [500 x i8]
%2 = alloca [500 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = tail call i64 @mb_register(i32 0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.17, i64 0, i64 0))
br label %110
11: ; preds = %0, %100
%12 = phi i32 [ %101, %100 ], [ 0, %0 ]
%13 = call i32 @memset(i8* %5, i32 0, i32 500)
%14 = call i32 @memset(i8* %4, i32 0, i32 500)
store i32 0, i32* %3
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %31, %11
%17 = phi i64 [ 0, %11 ], [ %24, %31 ]
%18 = load i32, i32* @stdin
%19 = call i32 @fileno(i32 %18)
%20 = shl i64 %17, 32
%21 = ashr exact i64 %20, 32
%22 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %21
%23 = call i64 @read(i32 %19, i8* %22, i32 1)
%24 = add nsw i64 %23, %21
%25 = trunc i64 %24 to i32
%26 = icmp eq i32 %25, 0
%27 = load i64, i64* @errno
%28 = icmp eq i64 %27, 0
%29 = select i1 %26, i1 %28, i1 false
%30 = shl i64 %24, 32
br i1 %29, label %37, label %31
31: ; preds = %16
%32 = add i64 %30, -4294967296
%33 = ashr exact i64 %32, 32
%34 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 10
br i1 %36, label %37, label %16
37: ; preds = %31, %16
%38 = ashr exact i64 %30, 32
%39 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %38
store i8 0, i8* %39
%40 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %5, i32* %3)
switch i32 %40, label %100 [
i32 2, label %41
i32 1, label %82
]
41: ; preds = %37
%42 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %55
44: ; preds = %41
%45 = load i32, i32* %3
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %3
%48 = call i32 @mb_request_pages(i32 %47)
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = add nsw i32 %48, %12
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %48, i32 %51)
br label %100
53: ; preds = %44
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %48)
br label %100
55: ; preds = %41
%56 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %69
58: ; preds = %55
%59 = load i32, i32* %3
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %3
%62 = call i32 @mb_reserve_pages(i32 %61)
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = add nsw i32 %62, %12
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %62, i32 %65)
br label %100
67: ; preds = %58
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %62)
br label %100
69: ; preds = %55
%70 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %100
72: ; preds = %69
%73 = load i32, i32* %3
%74 = icmp slt i32 %12, %73
%75 = select i1 %74, i32 %12, i32 %73
%76 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %75)
%77 = call i32 @mb_return_pages(i32 %75)
%78 = icmp eq i32 %77, 0
%79 = select i1 %78, i32 %75, i32 0
%80 = sub nsw i32 %12, %79
%81 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i32 %80)
br label %100
82: ; preds = %37
%83 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
%84 = icmp eq i64 %83, 0
br i1 %84, label %85, label %90
85: ; preds = %82
%86 = call i32 (...) @mb_query()
%87 = icmp sgt i32 %86, -1
br i1 %87, label %88, label %100
88: ; preds = %85
%89 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.12, i64 0, i64 0), i32 %86, i32 %12)
br label %100
90: ; preds = %82
%91 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
%92 = icmp eq i64 %91, 0
br i1 %92, label %93, label %95
93: ; preds = %90
%94 = call i32 (...) @mb_status()
br label %100
95: ; preds = %90
%96 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.14, i64 0, i64 0))
%97 = icmp eq i64 %96, 0
br i1 %97, label %100, label %98
98: ; preds = %95
%99 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @str.16, i64 0, i64 0))
br label %100
100: ; preds = %37, %69, %85, %88, %64, %67, %50, %53, %95, %98, %93, %72
%101 = phi i32 [ %80, %72 ], [ %12, %93 ], [ %12, %98 ], [ %12, %95 ], [ %51, %50 ], [ %12, %53 ], [ %65, %64 ], [ %12, %67 ], [ %12, %88 ], [ %12, %85 ], [ %12, %69 ], [ %12, %37 ]
%102 = call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.14, i64 0, i64 0))
%103 = icmp eq i64 %102, 0
br i1 %103, label %104, label %11
104: ; preds = %100
%105 = icmp sgt i32 %101, 0
br i1 %105, label %106, label %108
106: ; preds = %104
%107 = call i32 @mb_return_pages(i32 %101)
br label %108
108: ; preds = %106, %104
%109 = call i32 (...) @mb_terminate()
br label %110
110: ; preds = %108, %9
%111 = phi i32 [ -1, %9 ], [ 0, %108 ]
ret i32 %111
}