#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 */
typedef int /*<<< orphan*/ page_buf;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ pread(int, char *, int, int);
int random();
scalar_t__ read(int, char *, int);
void read_fd(int fd, int max_pages, int rand_flag) {
  char page_buf[4096];
  int i;
  int offset = random();
  lseek(fd, offset, random() % 2 ? SEEK_SET : SEEK_END);
  for (i = 0; i < max_pages; i++) {
    if (rand_flag) {
      if (pread(fd, page_buf, sizeof(page_buf), (random() % max_pages) * 4096) <
          0) {
        perror("pread:");
      }
    } else {
      if (read(fd, page_buf, sizeof(page_buf)) < 0) {
        perror("read:");
      }
    }
  }
}


x86 assembly (clang, -O0):
<read_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    %eax,-0x1018(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1020(%rbp)
mov    -0x1018(%rbp),%eax
mov    %eax,-0x101c(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11b2 <read_fd+0x62>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
jmp    11c1 <read_fd+0x71>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    -0x101c(%rbp),%esi
mov    -0x1020(%rbp),%edi
mov    -0x1024(%rbp),%edx
call   1030 <lseek@plt>
movl   $0x0,-0x1014(%rbp)
mov    -0x1014(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    129c <read_fd+0x14c>
cmpl   $0x0,-0xc(%rbp)
je     1254 <read_fd+0x104>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1034(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    -0x1034(%rbp),%edi
mov    -0x1030(%rbp),%rsi
cltd
idivl  -0x8(%rbp)
mov    %edx,%ecx
shl    $0xc,%ecx
mov    $0x1000,%edx
call   1070 <pread@plt>
cmp    $0x0,%rax
jge    124f <read_fd+0xff>
lea    0xdb6(%rip),%rdi
call   1060 <perror@plt>
jmp    1283 <read_fd+0x133>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jge    127e <read_fd+0x12e>
lea    0xd88(%rip),%rdi
call   1060 <perror@plt>
jmp    1283 <read_fd+0x133>
jmp    1288 <read_fd+0x138>
mov    -0x1014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1014(%rbp)
jmp    11e2 <read_fd+0x92>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %edx,%ebx
mov    %esi,%r12d
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <random@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1050 <random@plt>
test   $0x1,%al
jne    1186 <read_fd+0x36>
mov    0x2e64(%rip),%rax
jmp    118d <read_fd+0x3d>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r15d,%esi
call   1030 <lseek@plt>
test   %r12d,%r12d
jle    1211 <read_fd+0xc1>
mov    %rsp,%r15
lea    0xe58(%rip),%r13
mov    %r12d,%r14d
jmp    11e0 <read_fd+0x90>
nopl   (%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
lea    0xe3b(%rip),%rdi
test   %rax,%rax
jns    120b <read_fd+0xbb>
call   1060 <perror@plt>
jmp    120b <read_fd+0xbb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
je     11b0 <read_fd+0x60>
xor    %eax,%eax
call   1050 <random@plt>
cltd
idiv   %r12d
mov    %edx,%ecx
shl    $0xc,%ecx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1070 <pread@plt>
mov    %r13,%rdi
test   %rax,%rax
js     11cb <read_fd+0x7b>
add    $0xffffffff,%r14d
jne    11e0 <read_fd+0x90>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str = [7 x i8] c"pread:\00"
@.str.1 = [6 x i8] c"read:\00"
define void @read_fd(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = call i32 (...) @random()
store i32 %10, i32* %9
%11 = load i32, i32* %4
%12 = load i32, i32* %9
%13 = call i32 (...) @random()
%14 = srem i32 %13, 2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* @SEEK_SET
br label %20
18: ; preds = %3
%19 = load i32, i32* @SEEK_END
br label %20
20: ; preds = %18, %16
%21 = phi i32 [ %17, %16 ], [ %19, %18 ]
%22 = call i32 @lseek(i32 %11, i32 %12, i32 %21)
store i32 0, i32* %8
br label %23
23: ; preds = %51, %20
%24 = load i32, i32* %8
%25 = load i32, i32* %5
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %54
27: ; preds = %23
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = load i32, i32* %4
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%33 = call i32 (...) @random()
%34 = load i32, i32* %5
%35 = srem i32 %33, %34
%36 = mul nsw i32 %35, 4096
%37 = call i64 @pread(i32 %31, i8* %32, i32 4096, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %30
br label %50
42: ; preds = %27
%43 = load i32, i32* %4
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%45 = call i64 @read(i32 %43, i8* %44, i32 4096)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %47, %42
br label %50
50: ; preds = %49, %41
br label %51
51: ; preds = %50
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %23
54: ; preds = %23
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str = [7 x i8] c"pread:\00"
@.str.1 = [6 x i8] c"read:\00"
define void @read_fd(i32 %0, i32 %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (...) @random()
%7 = tail call i32 (...) @random()
%8 = and i32 %7, 1
%9 = icmp eq i32 %8, 0
%10 = load i32, i32* @SEEK_SET
%11 = load i32, i32* @SEEK_END
%12 = select i1 %9, i32 %11, i32 %10
%13 = tail call i32 @lseek(i32 %0, i32 %6, i32 %12)
%14 = icmp sgt i32 %1, 0
br i1 %14, label %15, label %34
15: ; preds = %3
%16 = icmp eq i32 %2, 0
br label %17
17: ; preds = %15, %31
%18 = phi i32 [ 0, %15 ], [ %32, %31 ]
br i1 %16, label %25, label %19
19: ; preds = %17
%20 = tail call i32 (...) @random()
%21 = srem i32 %20, %1
%22 = shl nsw i32 %21, 12
%23 = call i64 @pread(i32 %0, i8* %5, i32 4096, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %28, label %31
25: ; preds = %17
%26 = call i64 @read(i32 %0, i8* %5, i32 4096)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25, %19
%29 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %19 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %25 ]
%30 = tail call i32 @perror(i8* %29)
br label %31
31: ; preds = %28, %19, %25
%32 = add nuw nsw i32 %18, 1
%33 = icmp eq i32 %32, %1
br i1 %33, label %34, label %17
34: ; preds = %31, %3
ret void
}