#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_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
char *check_zero(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int read_eof(char *filename) {
  int fd;
  int i;
  int r;
  char buf[4096];
  if ((fd = open(filename, O_RDWR)) < 0) {
    fprintf(stderr, "can't open file %s \n", filename);
    exit(1);
  }
  for (i = 0; i < 100000; i++) {
    off_t offset;
    char *bufoff;
    offset = lseek(fd, 4096, SEEK_END);
    r = write(fd, "A", 1);
    offset = lseek(fd, offset - 4096, SEEK_SET);
    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);
      }
    }
  }
  fprintf(stderr, "read_checkzero done\n");
  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    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c2 <read_eof+0x52>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x186a0,-0x10(%rbp)
jge    12b8 <read_eof+0x148>
mov    -0xc(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1040 <lseek@plt>
mov    %eax,-0x1024(%rbp)
mov    -0xc(%rbp),%edi
lea    0xe19(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x1024(%rbp),%esi
sub    $0x1000,%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %eax,-0x1024(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12a5 <read_eof+0x135>
lea    -0x1020(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <check_zero@plt>
mov    %rax,-0x1030(%rbp)
cmp    $0x0,%rax
je     12a0 <read_eof+0x130>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x1024(%rbp),%eax
mov    -0x1030(%rbp),%rdx
cltq
add    %rax,%rdx
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12a5 <read_eof+0x135>
jmp    12aa <read_eof+0x13a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11c9 <read_eof+0x59>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_eof>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbp
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     127f <read_eof+0x10f>
mov    %eax,%r12d
mov    $0x186a0,%ebx
lea    0xe6c(%rip),%r14
mov    0x2e18(%rip),%r13
mov    %rsp,%r15
jmp    11c9 <read_eof+0x59>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%ebx
je     1254 <read_eof+0xe4>
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    $0x1000,%esi
call   1040 <lseek@plt>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0xfffff000,%ebp
mov    0x0(%r13),%edx
mov    %r12d,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11c0 <read_eof+0x50>
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <check_zero@plt>
test   %rax,%rax
je     11c0 <read_eof+0x50>
mov    0x2d9f(%rip),%rcx
mov    (%rcx),%edi
movslq %ebp,%rcx
add    %rcx,%rax
lea    0xdd7(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
mov    %rbp,%rdx
jmp    1243 <read_eof+0xd3>

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"can't open file %s \0A\00"
@SEEK_END = global i32 0
@.str.1 = [2 x i8] c"A\00"
@SEEK_SET = global i32 0
@.str.2 = [28 x i8] c"non-zero read at offset %p\0A\00"
@.str.3 = [21 x i8] c"read_checkzero done\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
%9 = load i8*, i8** %2
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
store i32 0, i32* %4
br label %19
19: ; preds = %53, %18
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 100000
br i1 %21, label %22, label %56
22: ; preds = %19
%23 = load i32, i32* %3
%24 = load i32, i32* @SEEK_END
%25 = call i32 @lseek(i32 %23, i32 4096, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %3
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %27, i32* %5
%28 = load i32, i32* %3
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, 4096
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @lseek(i32 %28, i32 %30, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %3
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 4096)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %52
38: ; preds = %22
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* %5
%41 = call i8* @check_zero(i8* %39, i32 %40)
store i8* %41, i8** %8
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %51
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i32, i32* %7
%46 = load i8*, i8** %8
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %38
br label %52
52: ; preds = %51, %22
br label %53
53: ; preds = %52
%54 = load i32, i32* %4
%55 = add nsw i32 %54, 1
store i32 %55, i32* %4
br label %19
56: ; preds = %19
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"can't open file %s \0A\00"
@SEEK_END = global i32 0
@.str.1 = [2 x i8] c"A\00"
@SEEK_SET = global i32 0
@.str.2 = [28 x i8] c"non-zero read at offset %p\0A\00"
@.str.3 = [21 x i8] c"read_checkzero done\0A\00"
define i32 @read_eof(i8* %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_RDWR
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1, %30
%12 = phi i32 [ %31, %30 ], [ 0, %1 ]
%13 = load i32, i32* @SEEK_END
%14 = call i32 @lseek(i32 %5, i32 4096, i32 %13)
%15 = call i32 @write(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%16 = add nsw i32 %14, -4096
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek(i32 %5, i32 %16, i32 %17)
%19 = call i32 @read(i32 %5, i8* %3, i32 4096)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %11
%22 = call i8* @check_zero(i8* %3, i32 %19)
%23 = icmp eq i8* %22, null
br i1 %23, label %30, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = sext i32 %18 to i64
%27 = getelementptr inbounds i8, i8* %22, i64 %26
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21, %11
%31 = add nuw nsw i32 %12, 1
%32 = icmp eq i32 %31, 100000
br i1 %32, label %33, label %11
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}