#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_SET;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pwrite(int, char *, int, int);
int random();
scalar_t__ write(int, char *, int);
void write_fd(int fd, int max_pages, int rand_flag) {
  char page_buf[4096];
  int i;
  memset(page_buf, random() % 255, sizeof(page_buf));
  lseek(fd, 0, SEEK_SET);
  for (i = 0; i < max_pages; i++) {
    if (rand_flag) {
      if (pwrite(fd, page_buf, sizeof(page_buf),
                 (random() % max_pages) * 4096) < 0)
        ;
      { perror("pwrite:"); }
    } else {
      if (write(fd, page_buf, sizeof(page_buf)) < 0) {
        perror("write:");
      }
    }
  }
}


x86 assembly (clang, -O0):
<write_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1020(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    -0x1020(%rbp),%rdi
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
movl   $0x0,-0x1014(%rbp)
mov    -0x1014(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1280 <write_fd+0x120>
cmpl   $0x0,-0xc(%rbp)
je     1238 <write_fd+0xd8>
mov    -0x4(%rbp),%eax
mov    %eax,-0x102c(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    -0x102c(%rbp),%edi
mov    -0x1028(%rbp),%rsi
cltd
idivl  -0x8(%rbp)
mov    %edx,%ecx
shl    $0xc,%ecx
mov    $0x1000,%edx
call   1070 <pwrite@plt>
cmp    $0x0,%rax
jge    1227 <write_fd+0xc7>
jmp    1227 <write_fd+0xc7>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
jmp    1267 <write_fd+0x107>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1262 <write_fd+0x102>
lea    0xda4(%rip),%rdi
call   1080 <perror@plt>
jmp    1267 <write_fd+0x107>
jmp    126c <write_fd+0x10c>
mov    -0x1014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1014(%rbp)
jmp    11c1 <write_fd+0x61>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %edx,%ebx
mov    %esi,%r13d
mov    %edi,%ebp
xor    %eax,%eax
call   1060 <random@plt>
cltq
imul   $0xffffffff80808081,%rax,%rsi
shr    $0x20,%rsi
add    %eax,%esi
mov    %esi,%ecx
shr    $0x1f,%ecx
sar    $0x7,%esi
add    %ecx,%esi
mov    %esi,%ecx
shl    $0x8,%ecx
sub    %ecx,%esi
add    %eax,%esi
mov    %rsp,%rdi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %r13d,%r13d
jle    1223 <write_fd+0xc3>
mov    %rsp,%r15
lea    0xe32(%rip),%r12
mov    %r13d,%r14d
jmp    1206 <write_fd+0xa6>
xor    %eax,%eax
call   1060 <random@plt>
cltd
idiv   %r13d
mov    %edx,%ecx
shl    $0xc,%ecx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1070 <pwrite@plt>
lea    0xe06(%rip),%rdi
call   1080 <perror@plt>
nop
add    $0xffffffff,%r14d
je     1223 <write_fd+0xc3>
test   %ebx,%ebx
jne    11d4 <write_fd+0x74>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %r12,%rdi
test   %rax,%rax
jns    1200 <write_fd+0xa0>
jmp    11fa <write_fd+0x9a>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [8 x i8] c"pwrite:\00"
@.str.1 = [7 x i8] c"write:\00"
define void @write_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 = srem i32 %6, 255
%8 = call i32 @memset(i8* %5, i32 %7, i32 4096)
%9 = load i32, i32* @SEEK_SET
%10 = call i32 @lseek(i32 %0, i32 0, i32 %9)
%11 = icmp sgt i32 %1, 0
br i1 %11, label %12, label %30
12: ; preds = %3
%13 = icmp eq i32 %2, 0
br label %14
14: ; preds = %12, %27
%15 = phi i32 [ 0, %12 ], [ %28, %27 ]
br i1 %13, label %21, label %16
16: ; preds = %14
%17 = call i32 (...) @random()
%18 = srem i32 %17, %1
%19 = shl nsw i32 %18, 12
%20 = call i64 @pwrite(i32 %0, i8* %5, i32 4096, i32 %19)
br label %24
21: ; preds = %14
%22 = call i64 @write(i32 %0, i8* %5, i32 4096)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21, %16
%25 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %16 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %21 ]
%26 = call i32 @perror(i8* %25)
br label %27
27: ; preds = %24, %21
%28 = add nuw nsw i32 %15, 1
%29 = icmp eq i32 %28, %1
br i1 %29, label %30, label %14
30: ; preds = %27, %3
ret void
}