#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*/ uint64_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_SET;
int ULONG_MAX;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(char *, long);
scalar_t__ lseek(int, unsigned long, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
long read(int, int /*<<< orphan*/ *, unsigned long);
unsigned long do_u64_read(int fd, char *name, uint64_t *buf,
                          unsigned long index, unsigned long count) {
  long bytes;
  if (index > ULONG_MAX / 8)
    fatal("index overflow: %lu\n", index);
  if (lseek(fd, index * 8, SEEK_SET) < 0) {
    perror(name);
    exit(EXIT_FAILURE);
  }
  bytes = read(fd, buf, count * 8);
  if (bytes < 0) {
    perror(name);
    exit(EXIT_FAILURE);
  }
  if (bytes % 8)
    fatal("partial read: %lu bytes\n", bytes);
  return bytes / 8;
}


x86 assembly (clang, -O0):
<do_u64_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
jbe    11a6 <do_u64_read+0x56>
mov    -0x20(%rbp),%rsi
lea    0xe5f(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
shl    $0x3,%rsi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    11e0 <do_u64_read+0x90>
mov    -0x10(%rbp),%rdi
call   1050 <perror@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
shl    $0x3,%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    121a <do_u64_read+0xca>
mov    -0x10(%rbp),%rdi
call   1050 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x30(%rbp),%rax
mov    $0x8,%ecx
cqto
idiv   %rcx
cmp    $0x0,%rdx
je     1242 <do_u64_read+0xf2>
mov    -0x30(%rbp),%rsi
lea    0xdd8(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x30(%rbp),%rax
mov    $0x8,%ecx
cqto
idiv   %rcx
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_u64_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
lea    0x7(%rax),%ebx
test   %eax,%eax
cmovns %eax,%ebx
mov    %r8,%r13
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%r12d
sar    $0x3,%ebx
movslq %ebx,%rax
cmp    %rcx,%rax
jae    1195 <do_u64_read+0x45>
lea    0xe73(%rip),%rdi
mov    %rbp,%rsi
call   1070 <fatal@plt>
shl    $0x3,%rbp
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1030 <lseek@plt>
test   %rax,%rax
js     11f6 <do_u64_read+0xa6>
shl    $0x3,%r13
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r13,%rdx
call   1040 <read@plt>
test   %rax,%rax
js     11f6 <do_u64_read+0xa6>
mov    %rax,%rbx
test   $0x7,%bl
je     11e0 <do_u64_read+0x90>
lea    0xe3d(%rip),%rdi
mov    %rbx,%rsi
call   1070 <fatal@plt>
shr    $0x3,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1050 <perror@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@ULONG_MAX = global i32 0
@.str = [21 x i8] c"index overflow: %lu\0A\00"
@SEEK_SET = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"partial read: %lu bytes\0A\00"
define i64 @do_u64_read(i32 %0, i8* nocapture %1, i32* nocapture %2, i64 %3, i64 %4) {
%6 = load i32, i32* @ULONG_MAX
%7 = sdiv i32 %6, 8
%8 = sext i32 %7 to i64
%9 = icmp ult i64 %8, %3
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %3)
br label %12
12: ; preds = %10, %5
%13 = shl i64 %3, 3
%14 = load i32, i32* @SEEK_SET
%15 = tail call i64 @lseek(i32 %0, i64 %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = tail call i32 @perror(i8* %1)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = shl i64 %4, 3
%23 = tail call i64 @read(i32 %0, i32* %2, i64 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = tail call i32 @perror(i8* %1)
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = and i64 %23, 7
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = tail call i32 @fatal(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %23)
br label %34
34: ; preds = %32, %29
%35 = lshr i64 %23, 3
ret i64 %35
}