#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;
scalar_t__ chk_malloc(unsigned long long, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long long fread(char *, int, unsigned long long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long long,
                         int /*<<< orphan*/);
unsigned long long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ print_error(char *, ...);
char *readFileChunk(char *infile, unsigned long long startpos,
                    unsigned long long chunklen) {
  FILE *file = NULL;
  char *buffer = NULL;
  file = fopen(infile, "rb");
  if (!file) {
    print_error("Unable to open file %s", infile);
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  unsigned long long infilelen = ftell(file);
  if (infilelen < startpos + chunklen) {
    print_error("Impossible to read %ul bytes starting from byte %lu.\n"
                "From that position only %ul bytes can be read.\n"
                "For this reason, the buffer was not loaded.",
                chunklen, startpos, startpos + chunklen - infilelen);
    return 0;
  }
  buffer = (char *)chk_malloc(chunklen, sizeof(char));
  fseek(file, startpos, SEEK_SET);
  unsigned long long readBytes = fread(buffer, 1, chunklen, file);
  if (readBytes != chunklen)
    print_error("ERROR reading from %s\n", infile);
  fclose(file);
  return buffer;
}


x86 assembly (clang, -O0):
<readFileChunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d2 <readFileChunk+0x62>
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <print_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <readFileChunk+0x13c>
mov    -0x28(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    -0x20(%rbp),%rcx
cmp    %rcx,%rax
jae    1239 <readFileChunk+0xc9>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x18(%rbp),%rcx
add    -0x20(%rbp),%rcx
sub    -0x38(%rbp),%rcx
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <print_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <readFileChunk+0x13c>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1080 <chk_malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     129b <readFileChunk+0x12b>
mov    -0x10(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <print_error@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFileChunk>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r14
lea    0xe77(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11d3 <readFileChunk+0x63>
mov    %rax,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
lea    (%r15,%r13,1),%rcx
sub    %rax,%rcx
jbe    11e9 <readFileChunk+0x79>
lea    0xe59(%rip),%rdi
xor    %r12d,%r12d
mov    %r15,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <print_error@plt>
jmp    123e <readFileChunk+0xce>
lea    0xe29(%rip),%rdi
xor    %r12d,%r12d
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <print_error@plt>
jmp    123e <readFileChunk+0xce>
mov    %r15,%rdi
mov    $0x1,%esi
call   1080 <chk_malloc@plt>
mov    %rax,%r12
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <fseek@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15,%rax
je     1236 <readFileChunk+0xc6>
lea    0xe7e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <print_error@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@.str.2 = [144 x i8] c"Impossible to read %ul bytes starting from byte %lu.\0AFrom that position only %ul bytes can be read.\0AFor this reason, the buffer was not loaded.\00"
@SEEK_SET = global i32 0
@.str.3 = [23 x i8] c"ERROR reading from %s\0A\00"
define i8* @readFileChunk(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* null, i32** %8
store i8* null, i8** %9
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i8* null, i8** %4
br label %61
19: ; preds = %3
%20 = load i32*, i32** %8
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i64 0, i32 %21)
%23 = load i32*, i32** %8
%24 = call i64 @ftell(i32* %23)
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = load i64, i64* %6
%27 = load i64, i64* %7
%28 = add i64 %26, %27
%29 = icmp ult i64 %25, %28
br i1 %29, label %30, label %39
30: ; preds = %19
%31 = load i64, i64* %7
%32 = load i64, i64* %6
%33 = load i64, i64* %6
%34 = load i64, i64* %7
%35 = add i64 %33, %34
%36 = load i64, i64* %10
%37 = sub i64 %35, %36
%38 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str.2, i64 0, i64 0), i64 %31, i64 %32, i64 %37)
store i8* null, i8** %4
br label %61
39: ; preds = %19
%40 = load i64, i64* %7
%41 = call i64 @chk_malloc(i64 %40, i32 1)
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %9
%43 = load i32*, i32** %8
%44 = load i64, i64* %6
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %43, i64 %44, i32 %45)
%47 = load i8*, i8** %9
%48 = load i64, i64* %7
%49 = load i32*, i32** %8
%50 = call i64 @fread(i8* %47, i32 1, i64 %48, i32* %49)
store i64 %50, i64* %11
%51 = load i64, i64* %11
%52 = load i64, i64* %7
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %39
%55 = load i8*, i8** %5
%56 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %54, %39
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
%60 = load i8*, i8** %9
store i8* %60, i8** %4
br label %61
61: ; preds = %57, %30, %16
%62 = load i8*, i8** %4
ret i8* %62
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@.str.2 = [144 x i8] c"Impossible to read %ul bytes starting from byte %lu.\0AFrom that position only %ul bytes can be read.\0AFor this reason, the buffer was not loaded.\00"
@SEEK_SET = global i32 0
@.str.3 = [23 x i8] c"ERROR reading from %s\0A\00"
define i8* @readFileChunk(i8* %0, i64 %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %28
8: ; preds = %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i64 0, i32 %9)
%11 = tail call i64 @ftell(i32* %4)
%12 = add i64 %2, %1
%13 = icmp ugt i64 %12, %11
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = sub i64 %12, %11
%16 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str.2, i64 0, i64 0), i64 %2, i64 %1, i64 %15)
br label %28
17: ; preds = %8
%18 = tail call i64 @chk_malloc(i64 %2, i32 1)
%19 = inttoptr i64 %18 to i8*
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %4, i64 %1, i32 %20)
%22 = tail call i64 @fread(i8* %19, i32 1, i64 %2, i32* %4)
%23 = icmp eq i64 %22, %2
br i1 %23, label %26, label %24
24: ; preds = %17
%25 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %26
26: ; preds = %24, %17
%27 = tail call i32 @fclose(i32* %4)
br label %28
28: ; preds = %14, %26, %6
%29 = phi i8* [ null, %6 ], [ null, %14 ], [ %19, %26 ]
ret i8* %29
}