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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, unsigned int,
                           unsigned int);
int /*<<< orphan*/ perror(char *);
size_t read(int, unsigned char *, size_t);
int /*<<< orphan*/ stderr;
int read_data(int fd, size_t len, size_t *offset, int nonblock, int *eof_out) {
  unsigned char buffer[65536];
  size_t got = 0;
  size_t need = len - *offset;
  ssize_t bytes;
  size_t i, j;
  while (got < need) {
    size_t to_get = (need - got < 65536) ? need - got : 65536;
    do {
      bytes = read(fd, buffer, to_get);
    } while (bytes < 0 &&
             (EINTR == errno ||
              (!nonblock && (EAGAIN == errno || EWOULDBLOCK == errno))));
    if (bytes < 0) {
      if (nonblock && (EAGAIN == errno || EWOULDBLOCK == errno)) {
        break;
      }
      perror("Reading data");
      return -1;
    }
    if (0 == bytes) {
      *eof_out = 1;
      break;
    }
    for (i = 0, j = *offset; i < bytes; i++, j++) {
      if (buffer[i] != (((j) >> ((((j)&3) << 3) + 2)) & 0xff)) {
        fprintf(stderr,
                "Incorrect data read at offset %zd : expected %02x, got %02x\n",
                j, (unsigned int)(((j) >> ((((j)&3) << 3) + 2)) & 0xff),
                (unsigned int)buffer[i]);
        return -1;
      }
    }
    got += bytes;
    *offset += bytes;
  }
  return got;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10070,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x10038(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
mov    %rax,-0x10040(%rbp)
mov    -0x10038(%rbp),%rax
cmp    -0x10040(%rbp),%rax
jae    1452 <read_data+0x322>
mov    -0x10040(%rbp),%rax
sub    -0x10038(%rbp),%rax
cmp    $0x10000,%rax
jae    11b2 <read_data+0x82>
mov    -0x10040(%rbp),%rax
sub    -0x10038(%rbp),%rax
mov    %rax,-0x10068(%rbp)
jmp    11c3 <read_data+0x93>
mov    $0x10000,%eax
mov    %rax,-0x10068(%rbp)
jmp    11c3 <read_data+0x93>
mov    -0x10068(%rbp),%rax
mov    %rax,-0x10060(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10030(%rbp),%rsi
mov    -0x10060(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x10048(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10048(%rbp)
mov    %al,-0x10069(%rbp)
jae    129b <read_data+0x16b>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2dc1(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1006a(%rbp)
je     128f <read_data+0x15f>
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1006b(%rbp)
jne    1283 <read_data+0x153>
mov    0x2d79(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2d8d(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1006c(%rbp)
je     1277 <read_data+0x147>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1006c(%rbp)
mov    -0x1006c(%rbp),%al
mov    %al,-0x1006b(%rbp)
mov    -0x1006b(%rbp),%al
mov    %al,-0x1006a(%rbp)
mov    -0x1006a(%rbp),%al
mov    %al,-0x10069(%rbp)
mov    -0x10069(%rbp),%al
test   $0x1,%al
jne    11d1 <read_data+0xa1>
cmpq   $0x0,-0x10048(%rbp)
jae    1312 <read_data+0x1e2>
cmpl   $0x0,-0x1c(%rbp)
je     12fa <read_data+0x1ca>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2d06(%rip),%rcx
cmp    (%rcx),%rax
je     12f5 <read_data+0x1c5>
mov    0x2cee(%rip),%rax
mov    (%rax),%rax
mov    0x2cec(%rip),%rcx
cmp    (%rcx),%rax
jne    12fa <read_data+0x1ca>
jmp    1452 <read_data+0x322>
lea    0xcff(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    145c <read_data+0x32c>
xor    %eax,%eax
cmp    -0x10048(%rbp),%rax
jne    1330 <read_data+0x200>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
jmp    1452 <read_data+0x322>
movq   $0x0,-0x10050(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10058(%rbp)
mov    -0x10050(%rbp),%rax
cmp    -0x10048(%rbp),%rax
jae    1427 <read_data+0x2f7>
mov    -0x10050(%rbp),%rax
movzbl -0x10030(%rbp,%rax,1),%eax
ff
mov    -0x10058(%rbp),%rdx
mov    -0x10058(%rbp),%rcx
and    $0x3,%rcx
shl    $0x3,%rcx
add    $0x2,%rcx
shr    %cl,%rdx
mov    %rdx,%rcx
and    $0xff,%rcx
cmp    %rcx,%rax
je     13f9 <read_data+0x2c9>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10058(%rbp),%rdx
mov    -0x10058(%rbp),%rax
mov    -0x10058(%rbp),%rcx
and    $0x3,%rcx
shl    $0x3,%rcx
add    $0x2,%rcx
shr    %cl,%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x10050(%rbp),%rax
movzbl -0x10030(%rbp,%rax,1),%r8d
fe ff
lea    0xc25(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    145c <read_data+0x32c>
jmp    13fe <read_data+0x2ce>
mov    -0x10050(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10050(%rbp)
mov    -0x10058(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10058(%rbp)
jmp    1349 <read_data+0x219>
mov    -0x10048(%rbp),%rax
add    -0x10038(%rbp),%rax
mov    %rax,-0x10038(%rbp)
mov    -0x10048(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
jmp    116a <read_data+0x3a>
mov    -0x10038(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %rsi,%r12
sub    (%rdx),%r12
je     11f2 <read_data+0xd2>
mov    %rdx,%r15
mov    %edi,%r13d
mov    %r8,0x8(%rsp)
xor    %r14d,%r14d
mov    $0x10000,%eax
lea    0x10(%rsp),%rbx
mov    %r12,%rdx
sub    %r14,%rdx
cmp    $0x10000,%rdx
cmovae %rax,%rdx
mov    %r13d,%edi
mov    %rbx,%rsi
call   1030 <read@plt>
test   %rax,%rax
je     120a <read_data+0xea>
mov    %rbx,%r9
mov    (%r15),%rsi
lea    0x0(,%rsi,8),%rdi
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
lea    (%rsi,%rbp,1),%rdx
movzbl 0x10(%rsp,%rbp,1),%r8d
mov    %edi,%ecx
and    $0x18,%cl
or     $0x2,%cl
mov    %rdx,%rbx
shr    %cl,%rbx
cmp    %bl,%r8b
jne    11d2 <read_data+0xb2>
add    $0x1,%rbp
add    $0x8,%rdi
cmp    %rbp,%rax
jne    1190 <read_data+0x70>
add    %rax,%r14
add    %rax,%rsi
mov    %rsi,(%r15)
cmp    %r14,%r12
mov    $0x10000,%eax
mov    %r9,%rbx
ja     1155 <read_data+0x35>
jmp    11f5 <read_data+0xd5>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
movzbl %bl,%ecx
lea    0xe1b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
jmp    11f5 <read_data+0xd5>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rax
movl   $0x1,(%rax)
jmp    11f5 <read_data+0xd5>

LLVM IR (-O0):
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [13 x i8] c"Reading data\00"
@stderr = global i32 0
@.str.1 = [61 x i8] c"Incorrect data read at offset %zd : expected %02x, got %02x\0A\00"
define i32 @read_data(i32 %0, i64 %1, i64* %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca [65536 x i8]
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i64
store i32 %0, i32* %7
store i64 %1, i64* %8
store i64* %2, i64** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
store i64 0, i64* %13
%19 = load i64, i64* %8
%20 = load i64*, i64** %9
%21 = load i64, i64* %20
%22 = sub i64 %19, %21
store i64 %22, i64* %14
br label %23
23: ; preds = %134, %5
%24 = load i64, i64* %13
%25 = load i64, i64* %14
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %142
27: ; preds = %23
%28 = load i64, i64* %14
%29 = load i64, i64* %13
%30 = sub i64 %28, %29
%31 = icmp ult i64 %30, 65536
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i64, i64* %14
%34 = load i64, i64* %13
%35 = sub i64 %33, %34
br label %37
36: ; preds = %27
br label %37
37: ; preds = %36, %32
%38 = phi i64 [ %35, %32 ], [ 65536, %36 ]
store i64 %38, i64* %18
br label %39
39: ; preds = %68, %37
%40 = load i32, i32* %7
%41 = getelementptr inbounds [65536 x i8], [65536 x i8]* %12, i64 0, i64 0
%42 = load i64, i64* %18
%43 = call i64 @read(i32 %40, i8* %41, i64 %42)
store i64 %43, i64* %15
br label %44
44: ; preds = %39
%45 = load i64, i64* %15
%46 = icmp ult i64 %45, 0
br i1 %46, label %47, label %68
47: ; preds = %44
%48 = load i64, i64* @EINTR
%49 = load i64, i64* @errno
%50 = icmp eq i64 %48, %49
br i1 %50, label %66, label %51
51: ; preds = %47
%52 = load i32, i32* %10
%53 = icmp ne i32 %52, 0
br i1 %53, label %64, label %54
54: ; preds = %51
%55 = load i64, i64* @EAGAIN
%56 = load i64, i64* @errno
%57 = icmp eq i64 %55, %56
br i1 %57, label %62, label %58
58: ; preds = %54
%59 = load i64, i64* @EWOULDBLOCK
%60 = load i64, i64* @errno
%61 = icmp eq i64 %59, %60
br label %62
62: ; preds = %58, %54
%63 = phi i1 [ true, %54 ], [ %61, %58 ]
br label %64
64: ; preds = %62, %51
%65 = phi i1 [ false, %51 ], [ %63, %62 ]
br label %66
66: ; preds = %64, %47
%67 = phi i1 [ true, %47 ], [ %65, %64 ]
br label %68
68: ; preds = %66, %44
%69 = phi i1 [ false, %44 ], [ %67, %66 ]
br i1 %69, label %39, label %70
70: ; preds = %68
%71 = load i64, i64* %15
%72 = icmp ult i64 %71, 0
br i1 %72, label %73, label %87
73: ; preds = %70
%74 = load i32, i32* %10
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %85
76: ; preds = %73
%77 = load i64, i64* @EAGAIN
%78 = load i64, i64* @errno
%79 = icmp eq i64 %77, %78
br i1 %79, label %84, label %80
80: ; preds = %76
%81 = load i64, i64* @EWOULDBLOCK
%82 = load i64, i64* @errno
%83 = icmp eq i64 %81, %82
br i1 %83, label %84, label %85
84: ; preds = %80, %76
br label %142
85: ; preds = %80, %73
%86 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %145
87: ; preds = %70
%88 = load i64, i64* %15
%89 = icmp eq i64 0, %88
br i1 %89, label %90, label %92
90: ; preds = %87
%91 = load i32*, i32** %11
store i32 1, i32* %91
br label %142
92: ; preds = %87
store i64 0, i64* %16
%93 = load i64*, i64** %9
%94 = load i64, i64* %93
store i64 %94, i64* %17
br label %95
95: ; preds = %129, %92
%96 = load i64, i64* %16
%97 = load i64, i64* %15
%98 = icmp ult i64 %96, %97
br i1 %98, label %99, label %134
99: ; preds = %95
%100 = load i64, i64* %16
%101 = getelementptr inbounds [65536 x i8], [65536 x i8]* %12, i64 0, i64 %100
%102 = load i8, i8* %101
%103 = zext i8 %102 to i64
%104 = load i64, i64* %17
%105 = load i64, i64* %17
%106 = and i64 %105, 3
%107 = shl i64 %106, 3
%108 = add i64 %107, 2
%109 = lshr i64 %104, %108
%110 = and i64 %109, 255
%111 = icmp ne i64 %103, %110
br i1 %111, label %112, label %128
112: ; preds = %99
%113 = load i32, i32* @stderr
%114 = load i64, i64* %17
%115 = load i64, i64* %17
%116 = load i64, i64* %17
%117 = and i64 %116, 3
%118 = shl i64 %117, 3
%119 = add i64 %118, 2
%120 = lshr i64 %115, %119
%121 = and i64 %120, 255
%122 = trunc i64 %121 to i32
%123 = load i64, i64* %16
%124 = getelementptr inbounds [65536 x i8], [65536 x i8]* %12, i64 0, i64 %123
%125 = load i8, i8* %124
%126 = zext i8 %125 to i32
%127 = call i32 @fprintf(i32 %113, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i64 %114, i32 %122, i32 %126)
store i32 -1, i32* %6
br label %145
128: ; preds = %99
br label %129
129: ; preds = %128
%130 = load i64, i64* %16
%131 = add i64 %130, 1
store i64 %131, i64* %16
%132 = load i64, i64* %17
%133 = add i64 %132, 1
store i64 %133, i64* %17
br label %95
134: ; preds = %95
%135 = load i64, i64* %15
%136 = load i64, i64* %13
%137 = add i64 %136, %135
store i64 %137, i64* %13
%138 = load i64, i64* %15
%139 = load i64*, i64** %9
%140 = load i64, i64* %139
%141 = add i64 %140, %138
store i64 %141, i64* %139
br label %23
142: ; preds = %90, %84, %23
%143 = load i64, i64* %13
%144 = trunc i64 %143 to i32
store i32 %144, i32* %6
br label %145
145: ; preds = %142, %112, %85
%146 = load i32, i32* %6
ret i32 %146
}

LLVM IR (-O2):
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str.1 = [61 x i8] c"Incorrect data read at offset %zd : expected %02x, got %02x\0A\00"
define i32 @read_data(i32 %0, i64 %1, i64* nocapture %2, i32 %3, i32* nocapture writeonly %4) {
%6 = alloca [65536 x i8]
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%8 = load i64, i64* %2
%9 = sub i64 %1, %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %46, label %11
11: ; preds = %5, %42
%12 = phi i64 [ %43, %42 ], [ 0, %5 ]
%13 = sub i64 %9, %12
%14 = icmp ult i64 %13, 65536
%15 = select i1 %14, i64 %13, i64 65536
%16 = call i64 @read(i32 %0, i8* %7, i64 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %11
store i32 1, i32* %4
br label %46
19: ; preds = %11
%20 = load i64, i64* %2
br label %21
21: ; preds = %19, %38
%22 = phi i64 [ %20, %19 ], [ %40, %38 ]
%23 = phi i64 [ 0, %19 ], [ %39, %38 ]
%24 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = shl i64 %22, 3
%27 = and i64 %26, 24
%28 = or i64 %27, 2
%29 = lshr i64 %22, %28
%30 = trunc i64 %29 to i8
%31 = icmp eq i8 %25, %30
br i1 %31, label %38, label %32
32: ; preds = %21
%33 = load i32, i32* @stderr
%34 = trunc i64 %29 to i32
%35 = and i32 %34, 255
%36 = zext i8 %25 to i32
%37 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i64 %22, i32 %35, i32 %36)
br label %49
38: ; preds = %21
%39 = add nuw i64 %23, 1
%40 = add i64 %22, 1
%41 = icmp eq i64 %39, %16
br i1 %41, label %42, label %21
42: ; preds = %38
%43 = add i64 %16, %12
%44 = add i64 %20, %16
store i64 %44, i64* %2
%45 = icmp ugt i64 %9, %43
br i1 %45, label %11, label %46
46: ; preds = %42, %5, %18
%47 = phi i64 [ %12, %18 ], [ 0, %5 ], [ %43, %42 ]
%48 = trunc i64 %47 to i32
br label %49
49: ; preds = %32, %46
%50 = phi i32 [ %48, %46 ], [ -1, %32 ]
ret i32 %50
}