#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*/ exit(int /*<<< orphan*/);
int lrand48();
int /*<<< orphan*/ mb_register(int /*<<< orphan*/);
int mb_request_pages(int);
scalar_t__ mb_return_pages(int);
int /*<<< orphan*/ mb_terminate();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand48(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
void client_loop(void) {
  int pages = 0;
  int failcnt = 0;
  int termcount = 0;
  int page_ceiling;
  int mode = 0;
  srand48(time(0));
  page_ceiling = lrand48() % 40 * 1024 * 1024 / 4096;
  mb_register(0);
  while (1) {
    if (pages < page_ceiling && mode == 0) {
      int ask = lrand48() % 200;
      int req;
      req = mb_request_pages(ask);
      if (req < 0) {
        printf("transmission error\n");
        exit(0);
      } else if (req == 0) {
        if (failcnt > 10) {
          sleep(1);
          failcnt = 0;
          mode = 1;
          termcount++;
        } else {
          usleep(100);
        }
        failcnt++;
      } else {
        if (req != ask) {
          printf("requested %d pages, got %d\n", ask, req);
          failcnt++;
          usleep(10);
        } else if (termcount > 0)
          termcount--;
        pages += req;
      }
    } else if (pages > 2) {
      int ret = lrand48() % pages;
      printf("returning %d of %d pages\n", ret, pages);
      if (mb_return_pages(ret) < 0) {
        printf("transmission error\n");
        exit(0);
      } else {
        pages -= ret;
      }
    } else {
      usleep(50);
      mode = mode == 0 ? 1 : 0;
      termcount++;
    }
    if (termcount > 10) {
      printf("failed to get pages 10 times.\n");
      mb_terminate();
      exit(0);
    }
  }
}


x86 assembly (clang, -O0):
<client_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,%edi
call   1090 <srand48@plt>
mov    $0x0,%al
call   1070 <lrand48@plt>
mov    $0x28,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
shl    $0xa,%eax
shl    $0xa,%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   10b0 <mb_register@plt>
mov    -0x4(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1312 <client_loop+0x162>
cmpl   $0x0,-0x14(%rbp)
jne    1312 <client_loop+0x162>
mov    $0x0,%al
call   1070 <lrand48@plt>
mov    $0xc8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <mb_request_pages@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    125d <client_loop+0xad>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12af <client_loop+0xff>
cmpl   $0xa,-0x8(%rbp)
jle    1297 <client_loop+0xe7>
mov    $0x1,%edi
call   10c0 <sleep@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12a1 <client_loop+0xf1>
mov    $0x64,%edi
call   10d0 <usleep@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1308 <client_loop+0x158>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     12e7 <client_loop+0x137>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0xa,%edi
call   10d0 <usleep@plt>
jmp    12ff <client_loop+0x14f>
cmpl   $0x0,-0xc(%rbp)
jle    12fa <client_loop+0x14a>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    12ff <client_loop+0x14f>
mov    -0x1c(%rbp),%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130d <client_loop+0x15d>
jmp    13a0 <client_loop+0x1f0>
cmpl   $0x2,-0x4(%rbp)
jle    1375 <client_loop+0x1c5>
mov    $0x0,%al
call   1070 <lrand48@plt>
cltd
idivl  -0x4(%rbp)
mov    %edx,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xcf9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <mb_return_pages@plt>
cmp    $0x0,%rax
jge    1365 <client_loop+0x1b5>
lea    0xca9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    139b <client_loop+0x1eb>
mov    $0x32,%edi
call   10d0 <usleep@plt>
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    13a0 <client_loop+0x1f0>
cmpl   $0xa,-0xc(%rbp)
jle    13c6 <client_loop+0x216>
lea    0xc99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <mb_terminate@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    120b <client_loop+0x5b>

x86 assembly (clang, -O2):
<client_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   10a0 <srand48@plt>
xor    %eax,%eax
call   1080 <lrand48@plt>
movslq %eax,%r13
imul   $0x66666667,%r13,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x24,%rax
add    %ecx,%eax
shl    $0x3,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%r13d
shl    $0x8,%r13d
xor    %edi,%edi
call   10c0 <mb_register@plt>
lea    0x1(%rbx),%eax
mov    %eax,0x4(%rsp)
xor    %ebp,%ebp
xor    %r14d,%r14d
xor    %r15d,%r15d
xor    %r12d,%r12d
jmp    1251 <client_loop+0x91>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
test   %ebp,%ebp
sete   %bl
mov    $0x32,%edi
call   10e0 <usleep@plt>
add    $0x1,%r14d
mov    %ebx,%ebp
cmp    $0xb,%r14d
jge    1354 <client_loop+0x194>
cmp    %r13d,%r12d
jge    12b0 <client_loop+0xf0>
test   %ebp,%ebp
jne    12b0 <client_loop+0xf0>
xor    %eax,%eax
call   1080 <lrand48@plt>
movslq %eax,%rbp
imul   $0x51eb851f,%rbp,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0xc8,%eax,%eax
sub    %eax,%ebp
mov    %ebp,%edi
call   1090 <mb_request_pages@plt>
test   %eax,%eax
js     136e <client_loop+0x1ae>
je     12f2 <client_loop+0x132>
mov    %eax,%ebx
cmp    %ebp,%eax
jne    1315 <client_loop+0x155>
xor    %eax,%eax
test   %r14d,%r14d
setg   %al
sub    %eax,%r14d
jmp    1335 <client_loop+0x175>
nopw   0x0(%rax,%rax,1)
cmp    $0x3,%r12d
jl     1230 <client_loop+0x70>
xor    %eax,%eax
call   1080 <lrand48@plt>
cltd
idiv   %r12d
mov    %edx,%ebx
lea    0xd4e(%rip),%rdi
mov    %edx,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <mb_return_pages@plt>
test   %rax,%rax
js     136e <client_loop+0x1ae>
sub    %ebx,%r12d
jmp    1247 <client_loop+0x87>
cmp    $0xb,%r15d
jl     133f <client_loop+0x17f>
mov    $0x1,%ebp
mov    $0x1,%edi
call   10d0 <sleep@plt>
add    $0x1,%r14d
mov    0x4(%rsp),%r15d
jmp    1247 <client_loop+0x87>
lea    0xce4(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r15d
mov    $0xa,%edi
call   10e0 <usleep@plt>
add    %ebx,%r12d
xor    %ebp,%ebp
jmp    1247 <client_loop+0x87>
mov    $0x64,%edi
call   10e0 <usleep@plt>
xor    %ebp,%ebp
add    $0x1,%r15d
jmp    1247 <client_loop+0x87>
lea    0xcdb(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <mb_terminate@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xcdf(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"transmission error\0A\00"
@.str.1 = [28 x i8] c"requested %d pages, got %d\0A\00"
@.str.2 = [26 x i8] c"returning %d of %d pages\0A\00"
@.str.3 = [31 x i8] c"failed to get pages 10 times.\0A\00"
define void @client_loop() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %5
%9 = call i32 @time(i32 0)
%10 = call i32 @srand48(i32 %9)
%11 = call i32 (...) @lrand48()
%12 = srem i32 %11, 40
%13 = mul nsw i32 %12, 1024
%14 = mul nsw i32 %13, 1024
%15 = sdiv i32 %14, 4096
store i32 %15, i32* %4
%16 = call i32 @mb_register(i32 0)
br label %17
17: ; preds = %0, %110
%18 = load i32, i32* %1
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %73
21: ; preds = %17
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %73
24: ; preds = %21
%25 = call i32 (...) @lrand48()
%26 = srem i32 %25, 200
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = call i32 @mb_request_pages(i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %24
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %34
%38 = load i32, i32* %2
%39 = icmp sgt i32 %38, 10
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @sleep(i32 1)
store i32 0, i32* %2
store i32 1, i32* %5
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
store i32 %43, i32* %3
br label %46
44: ; preds = %37
%45 = call i32 @usleep(i32 100)
br label %46
46: ; preds = %44, %40
%47 = load i32, i32* %2
%48 = add nsw i32 %47, 1
store i32 %48, i32* %2
br label %71
49: ; preds = %34
%50 = load i32, i32* %7
%51 = load i32, i32* %6
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i32, i32* %6
%55 = load i32, i32* %7
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %54, i32 %55)
%57 = load i32, i32* %2
%58 = add nsw i32 %57, 1
store i32 %58, i32* %2
%59 = call i32 @usleep(i32 10)
br label %67
60: ; preds = %49
%61 = load i32, i32* %3
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* %3
%65 = add nsw i32 %64, -1
store i32 %65, i32* %3
br label %66
66: ; preds = %63, %60
br label %67
67: ; preds = %66, %53
%68 = load i32, i32* %7
%69 = load i32, i32* %1
%70 = add nsw i32 %69, %68
store i32 %70, i32* %1
br label %71
71: ; preds = %67, %46
br label %72
72: ; preds = %71
br label %103
73: ; preds = %21, %17
%74 = load i32, i32* %1
%75 = icmp sgt i32 %74, 2
br i1 %75, label %76, label %94
76: ; preds = %73
%77 = call i32 (...) @lrand48()
%78 = load i32, i32* %1
%79 = srem i32 %77, %78
store i32 %79, i32* %8
%80 = load i32, i32* %8
%81 = load i32, i32* %1
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %80, i32 %81)
%83 = load i32, i32* %8
%84 = call i64 @mb_return_pages(i32 %83)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %76
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%88 = call i32 @exit(i32 0)
unreachable
89: ; preds = %76
%90 = load i32, i32* %8
%91 = load i32, i32* %1
%92 = sub nsw i32 %91, %90
store i32 %92, i32* %1
br label %93
93: ; preds = %89
br label %102
94: ; preds = %73
%95 = call i32 @usleep(i32 50)
%96 = load i32, i32* %5
%97 = icmp eq i32 %96, 0
%98 = zext i1 %97 to i64
%99 = select i1 %97, i32 1, i32 0
store i32 %99, i32* %5
%100 = load i32, i32* %3
%101 = add nsw i32 %100, 1
store i32 %101, i32* %3
br label %102
102: ; preds = %94, %93
br label %103
103: ; preds = %102, %72
%104 = load i32, i32* %3
%105 = icmp sgt i32 %104, 10
br i1 %105, label %106, label %110
106: ; preds = %103
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%108 = call i32 (...) @mb_terminate()
%109 = call i32 @exit(i32 0)
unreachable
110: ; preds = %103
br label %17
}

LLVM IR (-O2):
@.str.1 = [28 x i8] c"requested %d pages, got %d\0A\00"
@.str.2 = [26 x i8] c"returning %d of %d pages\0A\00"
@str = [30 x i8] c"failed to get pages 10 times.\00"
@str.5 = [19 x i8] c"transmission error\00"
define void @client_loop() {
%1 = tail call i32 @time(i32 0)
%2 = tail call i32 @srand48(i32 %1)
%3 = tail call i32 (...) @lrand48()
%4 = srem i32 %3, 40
%5 = shl nsw i32 %4, 8
%6 = tail call i32 @mb_register(i32 0)
br label %7
7: ; preds = %68, %0
%8 = phi i32 [ 0, %0 ], [ %69, %68 ]
%9 = phi i32 [ 0, %0 ], [ %70, %68 ]
%10 = phi i32 [ 0, %0 ], [ %71, %68 ]
%11 = phi i32 [ 0, %0 ], [ %72, %68 ]
%12 = icmp slt i32 %11, %5
%13 = icmp eq i32 %8, 0
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %15, label %51
15: ; preds = %7
%16 = tail call i32 (...) @lrand48()
%17 = srem i32 %16, 200
%18 = tail call i32 @mb_request_pages(i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %15
%24 = icmp eq i32 %18, 0
br i1 %24, label %25, label %37
25: ; preds = %23
%26 = icmp sgt i32 %10, 10
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = tail call i32 @sleep(i32 1)
%29 = add nsw i32 %9, 1
br label %32
30: ; preds = %25
%31 = tail call i32 @usleep(i32 100)
br label %32
32: ; preds = %30, %27
%33 = phi i32 [ 1, %27 ], [ 0, %30 ]
%34 = phi i32 [ %29, %27 ], [ %9, %30 ]
%35 = phi i32 [ 0, %27 ], [ %10, %30 ]
%36 = add nsw i32 %35, 1
br label %68
37: ; preds = %23
%38 = icmp eq i32 %18, %17
br i1 %38, label %43, label %39
39: ; preds = %37
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
%41 = add nsw i32 %10, 1
%42 = tail call i32 @usleep(i32 10)
br label %47
43: ; preds = %37
%44 = icmp sgt i32 %9, 0
%45 = sext i1 %44 to i32
%46 = add nsw i32 %9, %45
br label %47
47: ; preds = %43, %39
%48 = phi i32 [ %9, %39 ], [ %46, %43 ]
%49 = phi i32 [ %41, %39 ], [ %10, %43 ]
%50 = add nsw i32 %18, %11
br label %68
51: ; preds = %7
%52 = icmp sgt i32 %11, 2
br i1 %52, label %53, label %64
53: ; preds = %51
%54 = tail call i32 (...) @lrand48()
%55 = srem i32 %54, %11
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %55, i32 %11)
%57 = tail call i64 @mb_return_pages(i32 %55)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%61 = tail call i32 @exit(i32 0)
unreachable
62: ; preds = %53
%63 = sub nsw i32 %11, %55
br label %68
64: ; preds = %51
%65 = tail call i32 @usleep(i32 50)
%66 = zext i1 %13 to i32
%67 = add nsw i32 %9, 1
br label %68
68: ; preds = %47, %32, %62, %64
%69 = phi i32 [ %8, %62 ], [ %66, %64 ], [ %33, %32 ], [ 0, %47 ]
%70 = phi i32 [ %9, %62 ], [ %67, %64 ], [ %34, %32 ], [ %48, %47 ]
%71 = phi i32 [ %10, %62 ], [ %10, %64 ], [ %36, %32 ], [ %49, %47 ]
%72 = phi i32 [ %63, %62 ], [ %11, %64 ], [ %11, %32 ], [ %50, %47 ]
%73 = icmp sgt i32 %70, 10
br i1 %73, label %74, label %7
74: ; preds = %68
%75 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%76 = tail call i32 (...) @mb_terminate()
%77 = tail call i32 @exit(i32 0)
unreachable
}