#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 EOF;
int /*<<< orphan*/ SEEK_END;
int errno;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int read_file(char *file_path, char **file_buffer, size_t *file_size) {
  FILE *program_handle;
  program_handle = fopen(file_path, "r");
  if (program_handle == NULL) {
    printf("ERROR: File %s cannot be opened!\n", file_path);
    return errno;
  }
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           33);
    printf("file %s opened successfully", file_path);
    printf("\n");
  };
  fseek(program_handle, 0, SEEK_END);
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           35);
    printf("seek successful");
    printf("\n");
  }
  *file_size = ftell(program_handle);
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           37);
    printf("File size is %zd", *file_size);
    printf("\n");
  };
  rewind(program_handle);
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           39);
    printf("rewind successful");
    printf("\n");
  };
  if (*file_size > (1 << 30)) {
    *file_size = (1 << 30);
  }
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           43);
    printf("going to allocate buffer...");
    printf("\n");
  };
  *file_buffer = (char *)malloc(*file_size + 1);
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           45);
    printf("Buffer allocated successfully");
    printf("\n");
  };
  file_buffer[0][*file_size] = '\0';
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           47);
    printf("Program size is %zd.", *file_size);
    printf("\n");
  };
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           48);
    printf("Initializing read bufferers successful");
    printf("\n");
  }
  fread(*file_buffer, sizeof(char), *file_size, program_handle);
  if (0) {
    printf("[DEBUG]: %s:%d: ",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/master/"
           "src/common.c",
           50);
    printf("Reading streams successful\n%s", *file_buffer);
    printf("\n");
  };
  int res = fclose(program_handle);
  if (res == EOF) {
    if (0) {
      printf("[DEBUG]: %s:%d: ",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/"
             "master/src/common.c",
             53);
      printf("closing streams failed.");
      printf("\n");
    };
  } else {
    if (0) {
      printf("[DEBUG]: %s:%d: ",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/meisam/opencl-commons/refs/heads/"
             "master/src/common.c",
             55);
      printf("Closing streams succeeded");
      printf("\n");
    };
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d6 <read_file+0x56>
mov    -0x10(%rbp),%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <read_file+0x119>
mov    -0x28(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x20(%rbp),%rax
cmpq   $0x40000000,(%rax)
jbe    1222 <read_file+0xa2>
mov    -0x20(%rbp),%rax
movq   $0x40000000,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2d60(%rip),%rcx
cmp    (%rcx),%eax
jne    128d <read_file+0x10d>
jmp    1292 <read_file+0x112>
jmp    1292 <read_file+0x112>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe68(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     121b <read_file+0x9b>
mov    %rax,%rbx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,(%r12)
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    (%r12),%rdi
cmp    $0x40000001,%rdi
jb     11e9 <read_file+0x69>
movq   $0x40000000,(%r12)
40
mov    $0x40000000,%edi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,(%r15)
mov    (%r12),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    (%r15),%rdi
mov    (%r12),%rdx
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1236 <read_file+0xb6>
lea    0xde0(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [34 x i8] c"ERROR: File %s cannot be opened!\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@EOF = global i32 0
define i32 @read_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @errno
store i32 %17, i32* %4
br label %59
18: ; preds = %3
%19 = load i32*, i32** %8
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %8
%23 = call i64 @ftell(i32* %22)
%24 = load i64*, i64** %7
store i64 %23, i64* %24
%25 = load i32*, i32** %8
%26 = call i32 @rewind(i32* %25)
%27 = load i64*, i64** %7
%28 = load i64, i64* %27
%29 = icmp ugt i64 %28, 1073741824
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = load i64*, i64** %7
store i64 1073741824, i64* %31
br label %32
32: ; preds = %30, %18
%33 = load i64*, i64** %7
%34 = load i64, i64* %33
%35 = add i64 %34, 1
%36 = call i64 @malloc(i64 %35)
%37 = inttoptr i64 %36 to i8*
%38 = load i8**, i8*** %6
store i8* %37, i8** %38
%39 = load i8**, i8*** %6
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = load i64*, i64** %7
%43 = load i64, i64* %42
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 0, i8* %44
%45 = load i8**, i8*** %6
%46 = load i8*, i8** %45
%47 = load i64*, i64** %7
%48 = load i64, i64* %47
%49 = load i32*, i32** %8
%50 = call i32 @fread(i8* %46, i32 1, i64 %48, i32* %49)
%51 = load i32*, i32** %8
%52 = call i32 @fclose(i32* %51)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = load i32, i32* @EOF
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %57
56: ; preds = %32
br label %58
57: ; preds = %32
br label %58
58: ; preds = %57, %56
store i32 0, i32* %4
br label %59
59: ; preds = %58, %14
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [34 x i8] c"ERROR: File %s cannot be opened!\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@EOF = global i32 0
define i32 @read_file(i8* %0, i8** nocapture %1, i64* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @errno
br label %28
9: ; preds = %3
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %4, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %4)
store i64 %12, i64* %2
%13 = tail call i32 @rewind(i32* %4)
%14 = load i64, i64* %2
%15 = icmp ugt i64 %14, 1073741824
br i1 %15, label %16, label %17
16: ; preds = %9
store i64 1073741824, i64* %2
br label %17
17: ; preds = %16, %9
%18 = phi i64 [ 1073741824, %16 ], [ %14, %9 ]
%19 = add nuw nsw i64 %18, 1
%20 = tail call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %1
%22 = load i64, i64* %2
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %1
%25 = load i64, i64* %2
%26 = tail call i32 @fread(i8* %24, i32 1, i64 %25, i32* %4)
%27 = tail call i32 @fclose(i32* %4)
br label %28
28: ; preds = %17, %6
%29 = phi i32 [ %8, %6 ], [ 0, %17 ]
ret i32 %29
}