#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*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
char *check_zero(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int read_eof(char *filename) {
  int fd;
  int i;
  int r;
  char buf[4096];
  while ((fd = open(filename, O_RDONLY)) < 0) {
    sleep(1);
  }
  for (i = 0; i < 1000000; i++) {
    off_t offset;
    char *bufoff;
    offset = lseek(fd, SEEK_END, 0);
    r = read(fd, buf, 4096);
    if (r > 0) {
      if ((bufoff = check_zero(buf, r))) {
        fprintf(stderr, "non-zero read at offset %p\n", offset + bufoff);
        exit(1);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_eof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11ac <read_eof+0x3c>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    117f <read_eof+0xf>
movl   $0x0,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jge    1263 <read_eof+0xf3>
mov    -0xc(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lseek@plt>
mov    %eax,-0x1024(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1250 <read_eof+0xe0>
lea    -0x1020(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <check_zero@plt>
mov    %rax,-0x1030(%rbp)
cmp    $0x0,%rax
je     124b <read_eof+0xdb>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    -0x1024(%rbp),%eax
mov    -0x1030(%rbp),%rdx
cltq
add    %rax,%rdx
lea    0xdc4(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1250 <read_eof+0xe0>
jmp    1255 <read_eof+0xe5>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11b3 <read_eof+0x43>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_eof>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rdi,%r14
mov    0x2e57(%rip),%rbp
mov    0x0(%rbp),%esi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11bb <read_eof+0x4b>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11a0 <read_eof+0x30>
mov    %eax,%ebx
mov    $0xf4240,%ebp
mov    0x2e21(%rip),%r12
mov    %rsp,%r14
jmp    11d5 <read_eof+0x65>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
je     122f <read_eof+0xbf>
mov    (%r12),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <lseek@plt>
mov    %eax,%r15d
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11d0 <read_eof+0x60>
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <check_zero@plt>
test   %rax,%rax
je     11d0 <read_eof+0x60>
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%edi
movslq %r15d,%rcx
add    %rcx,%rax
lea    0xde3(%rip),%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"non-zero read at offset %p\0A\00"
define i32 @read_eof(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %2
br label %9
9: ; preds = %14, %1
%10 = load i8*, i8** %2
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @sleep(i32 1)
br label %9
16: ; preds = %9
store i32 0, i32* %4
br label %17
17: ; preds = %44, %16
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 1000000
br i1 %19, label %20, label %47
20: ; preds = %17
%21 = load i32, i32* %3
%22 = load i32, i32* @SEEK_END
%23 = call i32 @lseek(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %7
%24 = load i32, i32* %3
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 4096)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %20
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* %5
%32 = call i8* @check_zero(i8* %30, i32 %31)
store i8* %32, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %42
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i32, i32* %7
%37 = load i8*, i8** %8
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
br label %43
43: ; preds = %42, %20
br label %44
44: ; preds = %43
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %17
47: ; preds = %17
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"non-zero read at offset %p\0A\00"
define i32 @read_eof(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %9, %1
%8 = phi i32 [ %5, %1 ], [ %12, %9 ]
br label %14
9: ; preds = %1, %9
%10 = tail call i32 @sleep(i32 1)
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %9, label %7
14: ; preds = %7, %29
%15 = phi i32 [ 0, %7 ], [ %30, %29 ]
%16 = load i32, i32* @SEEK_END
%17 = call i32 @lseek(i32 %8, i32 %16, i32 0)
%18 = call i32 @read(i32 %8, i8* %3, i32 4096)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %14
%21 = call i8* @check_zero(i8* %3, i32 %18)
%22 = icmp eq i8* %21, null
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = sext i32 %17 to i64
%26 = getelementptr inbounds i8, i8* %21, i64 %25
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20, %14
%30 = add nuw nsw i32 %15, 1
%31 = icmp eq i32 %30, 1000000
br i1 %31, label %32, label %14
32: ; preds = %29
ret i32 0
}