#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 ssize_t;
typedef size_t off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
char *data_mapreadbuf(size_t);
int /*<<< orphan*/ err(int, char *, char const *, size_t, ...);
int /*<<< orphan*/ errx(int, char *, char const *, size_t, size_t);
int lseek(int, size_t, int /*<<< orphan*/);
int read(int, char *, size_t);
void readfiledata(int fd, const char *namestr, off_t regionstart,
                  off_t checkstart, off_t checkend, off_t regionend) {
  char *readbuf;
  size_t bufpos, len;
  ssize_t ret;
  readbuf = data_mapreadbuf(regionend - regionstart);
  bufpos = checkstart - regionstart;
  len = checkend - checkstart;
  if (lseek(fd, checkstart, SEEK_SET) == -1) {
    err(1, "%s: lseek to %lld", namestr, checkstart);
  }
  while (len > 0) {
    ret = read(fd, readbuf + bufpos, len);
    if (ret == -1) {
      err(1, "%s: read %u at %lld", namestr, len, regionstart + bufpos);
    }
    if (ret == 0) {
      errx(1, "%s: read %u at %lld: Unexpected EOF", namestr, len,
           regionstart + bufpos);
    }
    bufpos += ret;
    assert(len <= (size_t)ret);
    len -= ret;
  }
}


x86 assembly (clang, -O0):
<readfiledata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
sub    -0x18(%rbp),%rdi
call   1080 <data_mapreadbuf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11e1 <readfiledata+0x81>
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%edi
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
jmp    11e6 <readfiledata+0x86>
cmpq   $0x0,-0x48(%rbp)
jbe    1298 <readfiledata+0x138>
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1235 <readfiledata+0xd5>
mov    -0x10(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%r8
add    -0x40(%rbp),%r8
mov    $0x1,%edi
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
cmpl   $0x0,-0x4c(%rbp)
jne    1260 <readfiledata+0x100>
mov    -0x10(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%r8
add    -0x40(%rbp),%r8
mov    $0x1,%edi
lea    0xdcb(%rip),%rsi
call   1030 <errx@plt>
movslq -0x4c(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rax
movslq -0x4c(%rbp),%rcx
cmp    %rcx,%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movslq -0x4c(%rbp),%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
jmp    11e6 <readfiledata+0x86>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfiledata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rsi,0x8(%rsp)
mov    %edi,%r13d
mov    %rdx,0x10(%rsp)
sub    %rdx,%r9
mov    %r9,%rdi
call   1080 <data_mapreadbuf@plt>
mov    %rax,%r15
sub    %r14,%rbx
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    %r14,%rsi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11b5 <readfiledata+0x55>
test   %rbx,%rbx
jne    11d9 <readfiledata+0x79>
jmp    125d <readfiledata+0xfd>
lea    0xe44(%rip),%rsi
mov    $0x1,%edi
mov    0x8(%rsp),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <err@plt>
test   %rbx,%rbx
je     125d <readfiledata+0xfd>
sub    0x10(%rsp),%r14
jmp    121b <readfiledata+0xbb>
mov    0x10(%rsp),%rax
lea    (%r14,%rax,1),%r8
mov    $0x1,%edi
lea    0xe31(%rip),%rsi
mov    0x8(%rsp),%rdx
mov    %rbx,%rcx
call   1030 <errx@plt>
movslq %r12d,%rbp
add    %rbp,%r14
xor    %edi,%edi
cmp    %rbp,%rbx
setbe  %dil
call   1060 <assert@plt>
sub    %rbp,%rbx
je     125d <readfiledata+0xfd>
lea    (%r15,%r14,1),%rsi
mov    %r13d,%edi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
je     11e0 <readfiledata+0x80>
cmp    $0xffffffff,%r12d
jne    1202 <readfiledata+0xa2>
mov    0x10(%rsp),%rax
lea    (%r14,%rax,1),%r8
mov    $0x1,%edi
lea    0xdc6(%rip),%rsi
mov    0x8(%rsp),%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1070 <err@plt>
jmp    1202 <readfiledata+0xa2>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [18 x i8] c"%s: lseek to %lld\00"
@.str.1 = [20 x i8] c"%s: read %u at %lld\00"
@.str.2 = [36 x i8] c"%s: read %u at %lld: Unexpected EOF\00"
define void @readfiledata(i32 %0, i8* %1, i64 %2, i64 %3, i64 %4, i64 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i64
%16 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64 %4, i64* %11
store i64 %5, i64* %12
%17 = load i64, i64* %12
%18 = load i64, i64* %9
%19 = sub i64 %17, %18
%20 = call i8* @data_mapreadbuf(i64 %19)
store i8* %20, i8** %13
%21 = load i64, i64* %10
%22 = load i64, i64* %9
%23 = sub i64 %21, %22
store i64 %23, i64* %14
%24 = load i64, i64* %11
%25 = load i64, i64* %10
%26 = sub i64 %24, %25
store i64 %26, i64* %15
%27 = load i32, i32* %7
%28 = load i64, i64* %10
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @lseek(i32 %27, i64 %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %6
%33 = load i8*, i8** %8
%34 = load i64, i64* %10
%35 = call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %33, i64 %34)
br label %36
36: ; preds = %32, %6
br label %37
37: ; preds = %66, %36
%38 = load i64, i64* %15
%39 = icmp ugt i64 %38, 0
br i1 %39, label %40, label %81
40: ; preds = %37
%41 = load i32, i32* %7
%42 = load i8*, i8** %13
%43 = load i64, i64* %14
%44 = getelementptr inbounds i8, i8* %42, i64 %43
%45 = load i64, i64* %15
%46 = call i32 @read(i32 %41, i8* %44, i64 %45)
store i32 %46, i32* %16
%47 = load i32, i32* %16
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %56
49: ; preds = %40
%50 = load i8*, i8** %8
%51 = load i64, i64* %15
%52 = load i64, i64* %9
%53 = load i64, i64* %14
%54 = add i64 %52, %53
%55 = call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %50, i64 %51, i64 %54)
br label %56
56: ; preds = %49, %40
%57 = load i32, i32* %16
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %56
%60 = load i8*, i8** %8
%61 = load i64, i64* %15
%62 = load i64, i64* %9
%63 = load i64, i64* %14
%64 = add i64 %62, %63
%65 = call i32 @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %60, i64 %61, i64 %64)
br label %66
66: ; preds = %59, %56
%67 = load i32, i32* %16
%68 = sext i32 %67 to i64
%69 = load i64, i64* %14
%70 = add i64 %69, %68
store i64 %70, i64* %14
%71 = load i64, i64* %15
%72 = load i32, i32* %16
%73 = sext i32 %72 to i64
%74 = icmp ule i64 %71, %73
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = load i32, i32* %16
%78 = sext i32 %77 to i64
%79 = load i64, i64* %15
%80 = sub i64 %79, %78
store i64 %80, i64* %15
br label %37
81: ; preds = %37
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [18 x i8] c"%s: lseek to %lld\00"
@.str.1 = [20 x i8] c"%s: read %u at %lld\00"
@.str.2 = [36 x i8] c"%s: read %u at %lld: Unexpected EOF\00"
define void @readfiledata(i32 %0, i8* %1, i64 %2, i64 %3, i64 %4, i64 %5) {
%7 = sub i64 %5, %2
%8 = tail call i8* @data_mapreadbuf(i64 %7)
%9 = sub i64 %3, %2
%10 = sub i64 %4, %3
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %0, i64 %3, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1, i64 %3)
br label %16
16: ; preds = %14, %6
%17 = icmp eq i64 %10, 0
br i1 %17, label %37, label %18
18: ; preds = %16, %29
%19 = phi i64 [ %31, %29 ], [ %9, %16 ]
%20 = phi i64 [ %35, %29 ], [ %10, %16 ]
%21 = getelementptr inbounds i8, i8* %8, i64 %19
%22 = tail call i32 @read(i32 %0, i8* %21, i64 %20)
switch i32 %22, label %29 [
i32 -1, label %23
i32 0, label %26
]
23: ; preds = %18
%24 = add i64 %19, %2
%25 = tail call i32 (i32, i8*, i8*, i64, ...) @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %1, i64 %20, i64 %24)
br label %29
26: ; preds = %18
%27 = add i64 %19, %2
%28 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %1, i64 %20, i64 %27)
br label %29
29: ; preds = %23, %18, %26
%30 = sext i32 %22 to i64
%31 = add i64 %19, %30
%32 = icmp ule i64 %20, %30
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
%35 = sub i64 %20, %30
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %18
37: ; preds = %29, %16
ret void
}