#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ io_error(char *);
int lseek(int, int, int /*<<< orphan*/);
scalar_t__ random();
int read(int, char *, int);
int write(int, char *, int);
void doseek(off_t where, int fd, int update) {
  int buf[(16384) / (sizeof(int))];
  off_t probe;
  off_t size;
  probe = where * (16384);
  if (lseek(fd, (off_t)probe, SEEK_SET) != probe)
    io_error("lseek in doseek");
  if ((size = read(fd, (char *)buf, (16384))) == -1)
    io_error("read in doseek");
  if (update) {
    buf[((int)random() % (size / (sizeof(int)) - 2)) + 1]--;
    if (lseek(fd, (off_t)probe, SEEK_SET) != probe)
      io_error("lseek in doseek update");
    if (write(fd, (char *)buf, size) == -1)
      io_error("write in doseek");
  }
}


x86 assembly (clang, -O0):
<doseek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%eax
shl    $0xe,%eax
mov    %eax,-0x4014(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x4014(%rbp),%esi
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    -0x4014(%rbp),%eax
je     119f <doseek+0x4f>
lea    0xe66(%rip),%rdi
call   1070 <io_error@plt>
mov    -0x8(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %eax,-0x4018(%rbp)
cmp    $0xffffffff,%eax
jne    11ce <doseek+0x7e>
lea    0xe47(%rip),%rdi
call   1070 <io_error@plt>
cmpl   $0x0,-0xc(%rbp)
je     1264 <doseek+0x114>
mov    $0x0,%al
call   1060 <random@plt>
cltq
movslq -0x4018(%rbp),%rcx
shr    $0x2,%rcx
sub    $0x2,%rcx
xor    %edx,%edx
div    %rcx
mov    -0x400c(%rbp,%rdx,4),%eax
add    $0xffffffff,%eax
mov    %eax,-0x400c(%rbp,%rdx,4)
mov    -0x8(%rbp),%edi
mov    -0x4014(%rbp),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    -0x4014(%rbp),%eax
je     1235 <doseek+0xe5>
lea    0xdef(%rip),%rdi
call   1070 <io_error@plt>
mov    -0x8(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    -0x4018(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    125f <doseek+0x10f>
lea    0xddc(%rip),%rdi
call   1070 <io_error@plt>
jmp    1264 <doseek+0x114>
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doseek>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x4000,%rsp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%ebp
shl    $0xe,%ebp
mov    0x2e70(%rip),%r12
mov    (%r12),%edx
mov    %esi,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
cmp    %ebp,%eax
je     118d <doseek+0x3d>
lea    0xe78(%rip),%rdi
call   1070 <io_error@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     11ab <doseek+0x5b>
test   %r15d,%r15d
jne    11bc <doseek+0x6c>
jmp    121f <doseek+0xcf>
lea    0xe5e(%rip),%rdi
call   1070 <io_error@plt>
test   %r15d,%r15d
je     121f <doseek+0xcf>
xor    %eax,%eax
call   1060 <random@plt>
movslq %eax,%rcx
movslq %r14d,%rsi
shr    $0x2,%rsi
add    $0xfffffffffffffffe,%rsi
mov    %rcx,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     11e7 <doseek+0x97>
mov    %rcx,%rax
xor    %edx,%edx
div    %rsi
jmp    11eb <doseek+0x9b>
xor    %edx,%edx
div    %esi
addl   $0xffffffff,0x4(%rsp,%rdx,4)
mov    (%r12),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
cmp    %ebp,%eax
je     120d <doseek+0xbd>
lea    0xe17(%rip),%rdi
call   1070 <io_error@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122f <doseek+0xdf>
add    $0x4000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe00(%rip),%rdi
add    $0x4000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <io_error@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [16 x i8] c"lseek in doseek\00"
@.str.1 = [15 x i8] c"read in doseek\00"
@.str.2 = [23 x i8] c"lseek in doseek update\00"
@.str.3 = [16 x i8] c"write in doseek\00"
define void @doseek(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i32]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %4
%11 = mul nsw i32 %10, 16384
store i32 %11, i32* %8
%12 = load i32, i32* %5
%13 = load i32, i32* %8
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %12, i32 %13, i32 %14)
%16 = load i32, i32* %8
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @io_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %3
%21 = load i32, i32* %5
%22 = getelementptr inbounds [4096 x i32], [4096 x i32]* %7, i64 0, i64 0
%23 = bitcast i32* %22 to i8*
%24 = call i32 @read(i32 %21, i8* %23, i32 16384)
store i32 %24, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @io_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %62
31: ; preds = %28
%32 = call i64 (...) @random()
%33 = trunc i64 %32 to i32
%34 = sext i32 %33 to i64
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = udiv i64 %36, 4
%38 = sub i64 %37, 2
%39 = urem i64 %34, %38
%40 = add i64 %39, 1
%41 = getelementptr inbounds [4096 x i32], [4096 x i32]* %7, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = add nsw i32 %42, -1
store i32 %43, i32* %41
%44 = load i32, i32* %5
%45 = load i32, i32* %8
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @lseek(i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* %8
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %31
%51 = call i32 @io_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %31
%53 = load i32, i32* %5
%54 = getelementptr inbounds [4096 x i32], [4096 x i32]* %7, i64 0, i64 0
%55 = bitcast i32* %54 to i8*
%56 = load i32, i32* %9
%57 = call i32 @write(i32 %53, i8* %55, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %52
%60 = call i32 @io_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %52
br label %62
62: ; preds = %61, %28
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [16 x i8] c"lseek in doseek\00"
@.str.1 = [15 x i8] c"read in doseek\00"
@.str.2 = [23 x i8] c"lseek in doseek update\00"
@.str.3 = [16 x i8] c"write in doseek\00"
define void @doseek(i32 %0, i32 %1, i32 %2) {
%4 = alloca [4096 x i32]
%5 = bitcast [4096 x i32]* %4 to i8*
%6 = shl nsw i32 %0, 14
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @lseek(i32 %1, i32 %6, i32 %7)
%9 = icmp eq i32 %8, %6
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = tail call i32 @io_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
%13 = call i32 @read(i32 %1, i8* %5, i32 16384)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @io_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = icmp eq i32 %2, 0
br i1 %18, label %41, label %19
19: ; preds = %17
%20 = tail call i64 (...) @random()
%21 = shl i64 %20, 32
%22 = ashr exact i64 %21, 32
%23 = sext i32 %13 to i64
%24 = lshr i64 %23, 2
%25 = add nsw i64 %24, -2
%26 = urem i64 %22, %25
%27 = add nuw i64 %26, 1
%28 = getelementptr inbounds [4096 x i32], [4096 x i32]* %4, i64 0, i64 %27
%29 = load i32, i32* %28
%30 = add nsw i32 %29, -1
store i32 %30, i32* %28
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @lseek(i32 %1, i32 %6, i32 %31)
%33 = icmp eq i32 %32, %6
br i1 %33, label %36, label %34
34: ; preds = %19
%35 = tail call i32 @io_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %19
%37 = call i32 @write(i32 %1, i8* %5, i32 %13)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i32 @io_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %36, %39, %17
ret void
}