#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 int, unsigned long long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long long fread(char *, int, unsigned long long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long long,
                         int /*<<< orphan*/);
unsigned long long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ print_error(char *, ...);
char *readSetOfChunksFromFile(char *infile, unsigned int numchuncks,
                              unsigned long long *chunkpositions,
                              unsigned long long chunklen) {
  FILE *file = NULL;
  char *buffer = NULL;
  if (chunkpositions == NULL) {
    print_error("Impossible to read the chunks from the file since the "
                "positions were not set.\n");
    return 0;
  }
  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);
  buffer = (char *)chk_malloc(numchuncks, chunklen * sizeof(char));
  unsigned int i;
  for (i = 0; i < numchuncks; i++) {
    fseek(file, chunkpositions[i], SEEK_SET);
    if (chunkpositions[i] + chunklen > infilelen) {
      print_error("The chunk %lu cannot be read because exceeds the file size. "
                  "The buffer will be freed.\n",
                  i);
      free(buffer);
      return 0;
    }
    unsigned long long readBytes =
        fread(buffer + (chunklen * i), 1, chunklen, file);
    if (readBytes != chunklen)
      print_error("ERROR reading from %s. Chunk %lu not read.\n", infile, i);
  }
  fclose(file);
  return buffer;
}


x86 assembly (clang, -O0):
<readSetOfChunksFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cd <readSetOfChunksFromFile+0x4d>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <print_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1321 <readSetOfChunksFromFile+0x1a1>
mov    -0x10(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120b <readSetOfChunksFromFile+0x8b>
mov    -0x10(%rbp),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <print_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1321 <readSetOfChunksFromFile+0x1a1>
mov    -0x30(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
shl    $0x0,%rsi
call   1090 <chk_malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1310 <readSetOfChunksFromFile+0x190>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    (%rax,%rcx,8),%rsi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
add    -0x28(%rbp),%rax
cmp    -0x40(%rbp),%rax
jbe    12b2 <readSetOfChunksFromFile+0x132>
mov    -0x44(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <print_error@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1321 <readSetOfChunksFromFile+0x1a1>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x44(%rbp),%ecx
imul   %rcx,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     12fd <readSetOfChunksFromFile+0x17d>
mov    -0x10(%rbp),%rsi
mov    -0x44(%rbp),%edx
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1050 <print_error@plt>
jmp    1302 <readSetOfChunksFromFile+0x182>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1249 <readSetOfChunksFromFile+0xc9>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readSetOfChunksFromFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     126f <readSetOfChunksFromFile+0xef>
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xea5(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     128f <readSetOfChunksFromFile+0x10f>
mov    %rax,%r13
mov    %rbp,0x8(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r13,%rdi
call   1070 <ftell@plt>
mov    %rax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
call   1090 <chk_malloc@plt>
mov    %rax,(%rsp)
test   %r14d,%r14d
je     1281 <readSetOfChunksFromFile+0x101>
mov    %r14d,%ebp
mov    (%rsp),%r15
xor    %r14d,%r14d
jmp    121c <readSetOfChunksFromFile+0x9c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r14
add    %rbx,%r15
cmp    %r14,%rbp
je     1281 <readSetOfChunksFromFile+0x101>
mov    (%r12,%r14,8),%rsi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
call   1080 <fseek@plt>
mov    (%r12,%r14,8),%rax
add    %rbx,%rax
cmp    0x10(%rsp),%rax
ja     12a4 <readSetOfChunksFromFile+0x124>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
cmp    %rbx,%rax
je     1210 <readSetOfChunksFromFile+0x90>
lea    0xe62(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <print_error@plt>
jmp    1210 <readSetOfChunksFromFile+0x90>
lea    0xd8a(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <print_error@plt>
jmp    12c0 <readSetOfChunksFromFile+0x140>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    (%rsp),%rbx
jmp    12c0 <readSetOfChunksFromFile+0x140>
lea    0xdbc(%rip),%rdi
xor    %ebx,%ebx
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <print_error@plt>
jmp    12c0 <readSetOfChunksFromFile+0x140>
lea    0xdbe(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <print_error@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [79 x i8] c"Impossible to read the chunks from the file since the positions were not set.\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [87 x i8] c"The chunk %lu cannot be read because exceeds the file size. The buffer will be freed.\0A\00"
@.str.4 = [44 x i8] c"ERROR reading from %s. Chunk %lu not read.\0A\00"
define i8* @readSetOfChunksFromFile(i8* %0, i32 %1, i64* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
store i8* %0, i8** %6
store i32 %1, i32* %7
store i64* %2, i64** %8
store i64 %3, i64* %9
store i32* null, i32** %10
store i8* null, i8** %11
%15 = load i64*, i64** %8
%16 = icmp eq i64* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %5
br label %90
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %10
%22 = load i32*, i32** %10
%23 = icmp ne i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i8* null, i8** %5
br label %90
27: ; preds = %19
%28 = load i32*, i32** %10
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i64 0, i32 %29)
%31 = load i32*, i32** %10
%32 = call i64 @ftell(i32* %31)
store i64 %32, i64* %12
%33 = load i32, i32* %7
%34 = load i64, i64* %9
%35 = mul i64 %34, 1
%36 = call i64 @chk_malloc(i32 %33, i64 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %11
store i32 0, i32* %13
br label %38
38: ; preds = %83, %27
%39 = load i32, i32* %13
%40 = load i32, i32* %7
%41 = icmp ult i32 %39, %40
br i1 %41, label %42, label %86
42: ; preds = %38
%43 = load i32*, i32** %10
%44 = load i64*, i64** %8
%45 = load i32, i32* %13
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i64, i64* %44, i64 %46
%48 = load i64, i64* %47
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @fseek(i32* %43, i64 %48, i32 %49)
%51 = load i64*, i64** %8
%52 = load i32, i32* %13
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds i64, i64* %51, i64 %53
%55 = load i64, i64* %54
%56 = load i64, i64* %9
%57 = add i64 %55, %56
%58 = load i64, i64* %12
%59 = icmp ugt i64 %57, %58
br i1 %59, label %60, label %65
60: ; preds = %42
%61 = load i32, i32* %13
%62 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.3, i64 0, i64 0), i32 %61)
%63 = load i8*, i8** %11
%64 = call i32 @free(i8* %63)
store i8* null, i8** %5
br label %90
65: ; preds = %42
%66 = load i8*, i8** %11
%67 = load i64, i64* %9
%68 = load i32, i32* %13
%69 = zext i32 %68 to i64
%70 = mul i64 %67, %69
%71 = getelementptr inbounds i8, i8* %66, i64 %70
%72 = load i64, i64* %9
%73 = load i32*, i32** %10
%74 = call i64 @fread(i8* %71, i32 1, i64 %72, i32* %73)
store i64 %74, i64* %14
%75 = load i64, i64* %14
%76 = load i64, i64* %9
%77 = icmp ne i64 %75, %76
br i1 %77, label %78, label %82
78: ; preds = %65
%79 = load i8*, i8** %6
%80 = load i32, i32* %13
%81 = call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %79, i32 %80)
br label %82
82: ; preds = %78, %65
br label %83
83: ; preds = %82
%84 = load i32, i32* %13
%85 = add i32 %84, 1
store i32 %85, i32* %13
br label %38
86: ; preds = %38
%87 = load i32*, i32** %10
%88 = call i32 @fclose(i32* %87)
%89 = load i8*, i8** %11
store i8* %89, i8** %5
br label %90
90: ; preds = %86, %60, %24, %17
%91 = load i8*, i8** %5
ret i8* %91
}

LLVM IR (-O2):
@.str = [79 x i8] c"Impossible to read the chunks from the file since the positions were not set.\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [87 x i8] c"The chunk %lu cannot be read because exceeds the file size. The buffer will be freed.\0A\00"
@.str.4 = [44 x i8] c"ERROR reading from %s. Chunk %lu not read.\0A\00"
define i8* @readSetOfChunksFromFile(i8* %0, i32 %1, i64* %2, i64 %3) {
%5 = icmp eq i64* %2, null
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str, i64 0, i64 0))
br label %48
8: ; preds = %4
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %48
13: ; preds = %8
%14 = load i32, i32* @SEEK_END
%15 = tail call i32 @fseek(i32* %9, i64 0, i32 %14)
%16 = tail call i64 @ftell(i32* %9)
%17 = tail call i64 @chk_malloc(i32 %1, i64 %3)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i32 %1, 0
br i1 %19, label %46, label %20
20: ; preds = %13
%21 = zext i32 %1 to i64
br label %22
22: ; preds = %20, %43
%23 = phi i64 [ 0, %20 ], [ %44, %43 ]
%24 = getelementptr inbounds i64, i64* %2, i64 %23
%25 = load i64, i64* %24
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @fseek(i32* %9, i64 %25, i32 %26)
%28 = load i64, i64* %24
%29 = add i64 %28, %3
%30 = icmp ugt i64 %29, %16
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = trunc i64 %23 to i32
%33 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = tail call i32 @free(i8* %18)
br label %48
35: ; preds = %22
%36 = mul i64 %23, %3
%37 = getelementptr inbounds i8, i8* %18, i64 %36
%38 = tail call i64 @fread(i8* %37, i32 1, i64 %3, i32* %9)
%39 = icmp eq i64 %38, %3
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = trunc i64 %23 to i32
%42 = tail call i32 (i8*, ...) @print_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %41)
br label %43
43: ; preds = %40, %35
%44 = add nuw nsw i64 %23, 1
%45 = icmp eq i64 %44, %21
br i1 %45, label %46, label %22
46: ; preds = %43, %13
%47 = tail call i32 @fclose(i32* %9)
br label %48
48: ; preds = %31, %46, %11, %6
%49 = phi i8* [ null, %6 ], [ null, %11 ], [ null, %31 ], [ %18, %46 ]
ret i8* %49
}