#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*/ io_error(char *);
int lseek(int, long, 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, probe, 0) != 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, (long)probe, 0) != 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
movslq -0x4014(%rbp),%rsi
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    -0x4014(%rbp),%eax
je     1199 <doseek+0x49>
lea    0xe6c(%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    11c8 <doseek+0x78>
lea    0xe4d(%rip),%rdi
call   1070 <io_error@plt>
cmpl   $0x0,-0xc(%rbp)
je     1258 <doseek+0x108>
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
movslq -0x4014(%rbp),%rsi
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    -0x4014(%rbp),%eax
je     1229 <doseek+0xd9>
lea    0xdfb(%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    1253 <doseek+0x103>
lea    0xde8(%rip),%rdi
call   1070 <io_error@plt>
jmp    1258 <doseek+0x108>
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doseek>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4008,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
shl    $0xe,%edi
movslq %edi,%rbx
mov    %esi,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    %ebx,%eax
je     1184 <doseek+0x34>
lea    0xe81(%rip),%rdi
call   1070 <io_error@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     11a2 <doseek+0x52>
test   %r15d,%r15d
jne    11b3 <doseek+0x63>
jmp    1215 <doseek+0xc5>
lea    0xe67(%rip),%rdi
call   1070 <io_error@plt>
test   %r15d,%r15d
je     1215 <doseek+0xc5>
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     11de <doseek+0x8e>
mov    %rcx,%rax
xor    %edx,%edx
div    %rsi
jmp    11e2 <doseek+0x92>
xor    %edx,%edx
div    %esi
addl   $0xffffffff,0x4(%rsp,%rdx,4)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    %ebx,%eax
je     1203 <doseek+0xb3>
lea    0xe21(%rip),%rdi
call   1070 <io_error@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1223 <doseek+0xd3>
add    $0x4008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0c(%rip),%rdi
add    $0x4008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <io_error@plt>

LLVM IR (-O0):
@.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 = sext i32 %13 to i64
%15 = call i32 @lseek(i32 %12, i64 %14, i32 0)
%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 = sext i32 %45 to i64
%47 = call i32 @lseek(i32 %44, i64 %46, i32 0)
%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):
@.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 = sext i32 %6 to i64
%8 = tail call i32 @lseek(i32 %1, i64 %7, i32 0)
%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 %40, 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 = tail call i32 @lseek(i32 %1, i64 %7, i32 0)
%32 = icmp eq i32 %31, %6
br i1 %32, label %35, label %33
33: ; preds = %19
%34 = tail call i32 @io_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %19
%36 = call i32 @write(i32 %1, i8* %5, i32 %13)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = tail call i32 @io_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %35, %38, %17
ret void
}