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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fatal_error(char *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long fread(char *, int, int const, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int fwrite(char *, long, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ validate_args_or_die(int, char **, long *, char **);
int main(int argc, char **argv) {
  if (sizeof(long) != 8) {
    fatal_error("expected sizeof(long) to be 8 -- we want fseek(-, long, -) to "
                "be safe");
  }
  long offset;
  char *path;
  validate_args_or_die(argc, argv, &offset, &path);
  FILE *file = fopen(path, "r");
  if (!file) {
    fatal_error("we failed to open the file");
  }
  if (fseek(file, offset, SEEK_SET) != 0) {
    fatal_error("we failed to seek to that offset in the file");
  }
  long total_bytes_read = 0;
  const int kBufferSize = 32000;
  char buffer[kBufferSize];
  while (1) {
    long num_bytes_read = fread(buffer, 1, kBufferSize, file);
    if (num_bytes_read == 0) {
      if (feof(file) == 0) {
        fatal_error("fread failed");
      }
      if (total_bytes_read == 0) {
        fseek(file, 0, SEEK_END);
        long file_size = ftell(file);
        if (offset > file_size) {
          fatal_error("the offset is larger than the file");
        }
      }
      return 0;
    }
    if (fwrite(buffer, num_bytes_read, 1, stdout) != 1) {
      fatal_error("writing to stdout failed");
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1090 <validate_args_or_die@plt>
mov    -0x20(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d5 <main+0x55>
lea    0xe32(%rip),%rdi
call   1060 <fatal_error@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     1201 <main+0x81>
lea    0xe21(%rip),%rdi
call   1060 <fatal_error@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x7d00,-0x34(%rbp)
mov    %rsp,%rax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rax
add    $0xffffffffffff8300,%rax
mov    %rax,-0x58(%rbp)
mov    %rax,%rsp
jmp    122c <main+0xac>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7d00,%edx
call   1030 <fread@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12d4 <main+0x154>
mov    -0x28(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
jne    1271 <main+0xf1>
lea    0xdde(%rip),%rdi
call   1060 <fatal_error@plt>
cmpq   $0x0,-0x30(%rbp)
jne    12be <main+0x13e>
mov    -0x28(%rbp),%rdi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x50(%rbp),%rax
jle    12b9 <main+0x139>
lea    0xda3(%rip),%rdi
call   1060 <fatal_error@plt>
jmp    12be <main+0x13e>
movl   $0x0,-0x4(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret
mov    -0x58(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    0x2ced(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     1304 <main+0x184>
lea    0xd7b(%rip),%rdi
call   1060 <fatal_error@plt>
jmp    1227 <main+0xa7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <validate_args_or_die@plt>
mov    -0x38(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11c2 <main+0x42>
lea    0xe45(%rip),%rdi
call   1060 <fatal_error@plt>
mov    -0x30(%rbp),%rsi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1070 <fseek@plt>
test   %rax,%rax
je     11e8 <main+0x68>
lea    0xe3a(%rip),%rdi
call   1060 <fatal_error@plt>
mov    %rsp,%r12
mov    %rsp,%rbx
add    $0xffffffffffff8300,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x7d00,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     126d <main+0xed>
mov    0x2db7(%rip),%r13
lea    0xe5a(%rip),%r15
jmp    124a <main+0xca>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x7d00,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     126d <main+0xed>
mov    0x0(%r13),%ecx
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     1230 <main+0xb0>
mov    %r15,%rdi
call   1060 <fatal_error@plt>
jmp    1230 <main+0xb0>
mov    %r14,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    1286 <main+0x106>
lea    0xdc9(%rip),%rdi
call   1060 <fatal_error@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1040 <ftell@plt>
cmp    %rax,-0x30(%rbp)
jle    12b3 <main+0x133>
lea    0xda9(%rip),%rdi
call   1060 <fatal_error@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"we failed to open the file\00"
@SEEK_SET = global i32 0
@.str.2 = [45 x i8] c"we failed to seek to that offset in the file\00"
@.str.3 = [13 x i8] c"fread failed\00"
@SEEK_END = global i32 0
@.str.4 = [35 x i8] c"the offset is larger than the file\00"
@stdout = global i32 0
@.str.5 = [25 x i8] c"writing to stdout failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @validate_args_or_die(i32 %14, i8** %15, i64* %6, i8** %7)
%17 = load i8*, i8** %7
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %2
%22 = call i32 @fatal_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = load i32*, i32** %8
%25 = load i64, i64* %6
%26 = load i32, i32* @SEEK_SET
%27 = call i64 @fseek(i32* %24, i64 %25, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @fatal_error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
store i64 0, i64* %9
store i32 32000, i32* %10
%32 = call i8* @llvm.stacksave()
store i8* %32, i8** %11
%33 = alloca i8, i64 32000
br label %34
34: ; preds = %71, %31
br label %35
35: ; preds = %34
%36 = load i32*, i32** %8
%37 = call i64 @fread(i8* %33, i32 1, i32 32000, i32* %36)
store i64 %37, i64* %12
%38 = load i64, i64* %12
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %64
40: ; preds = %35
%41 = load i32*, i32** %8
%42 = call i64 @feof(i32* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @fatal_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = load i64, i64* %9
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %61
49: ; preds = %46
%50 = load i32*, i32** %8
%51 = load i32, i32* @SEEK_END
%52 = call i64 @fseek(i32* %50, i64 0, i32 %51)
%53 = load i32*, i32** %8
%54 = call i64 @ftell(i32* %53)
store i64 %54, i64* %13
%55 = load i64, i64* %6
%56 = load i64, i64* %13
%57 = icmp sgt i64 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %49
%59 = call i32 @fatal_error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %58, %49
br label %61
61: ; preds = %60, %46
store i32 0, i32* %3
%62 = load i8*, i8** %11
%63 = load i32, i32* %3
ret i32 %63
64: ; preds = %35
%65 = load i64, i64* %12
%66 = load i32, i32* @stdout
%67 = call i32 @fwrite(i8* %33, i64 %65, i32 1, i32 %66)
%68 = icmp ne i32 %67, 1
br i1 %68, label %69, label %71
69: ; preds = %64
%70 = call i32 @fatal_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %71
71: ; preds = %69, %64
br label %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"we failed to open the file\00"
@SEEK_SET = global i32 0
@.str.2 = [45 x i8] c"we failed to seek to that offset in the file\00"
@.str.3 = [13 x i8] c"fread failed\00"
@SEEK_END = global i32 0
@.str.4 = [35 x i8] c"the offset is larger than the file\00"
@stdout = global i32 0
@.str.5 = [25 x i8] c"writing to stdout failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = bitcast i64* %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = call i32 @validate_args_or_die(i32 %0, i8** %1, i64* %3, i8** %4)
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @fatal_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i64, i64* %3
%15 = load i32, i32* @SEEK_SET
%16 = call i64 @fseek(i32* %9, i64 %14, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @fatal_error(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = call i8* @llvm.stacksave()
%22 = alloca [32000 x i8]
%23 = getelementptr inbounds [32000 x i8], [32000 x i8]* %22, i64 0, i64 0
%24 = call i64 @fread(i8* %23, i32 1, i32 32000, i32* %9)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %46, %20
%27 = call i64 @feof(i32* %9)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @fatal_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %26, %29
%32 = load i32, i32* @SEEK_END
%33 = call i64 @fseek(i32* %9, i64 0, i32 %32)
%34 = call i64 @ftell(i32* %9)
%35 = load i64, i64* %3
%36 = icmp sgt i64 %35, %34
br i1 %36, label %37, label %49
37: ; preds = %31
%38 = call i32 @fatal_error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %49
39: ; preds = %20, %46
%40 = phi i64 [ %47, %46 ], [ %24, %20 ]
%41 = load i32, i32* @stdout
%42 = call i32 @fwrite(i8* %23, i64 %40, i32 1, i32 %41)
%43 = icmp eq i32 %42, 1
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = call i32 @fatal_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %39, %44
%47 = call i64 @fread(i8* %23, i32 1, i32 32000, i32* %9)
%48 = icmp eq i64 %47, 0
br i1 %48, label %26, label %39
49: ; preds = %37, %31
ret i32 0
}