#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_SET;
int /*<<< orphan*/ exitc(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long,
                         int /*<<< orphan*/);
long get_file_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, size_t, long);
size_t read_file_2_buffer(char *buf, FILE *fp, unsigned long off, long lSize) {
  size_t res;
  if (lSize == -1)
    lSize = get_file_size(fp);
  if (fp == NULL) {
    free(buf);
    exitc(2, "Can't open file!");
  }
  if (off > 0)
    fseek(fp, off, SEEK_SET);
  res = fread(buf, 1, lSize, fp);
  if (res != lSize) {
    printf("res: %u lS: %u\n", res, lSize);
    free(buf);
    fclose(fp);
    exitc(3, "Can't read file!");
  }
  return res;
}


x86 assembly (clang, -O0):
<read_file_2_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11a0 <read_file_2_buffer+0x30>
mov    -0x10(%rbp),%rdi
call   1080 <get_file_size@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c5 <read_file_2_buffer+0x55>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    $0x2,%edi
lea    0xe40(%rip),%rsi
call   1090 <exitc@plt>
cmpq   $0x0,-0x18(%rbp)
jbe    11e6 <read_file_2_buffer+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1245 <read_file_2_buffer+0xd5>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdf4(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    $0x3,%edi
lea    0xde1(%rip),%rsi
call   1090 <exitc@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_2_buffer>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
cmp    $0xffffffffffffffff,%rcx
je     1208 <read_file_2_buffer+0x98>
test   %r15,%r15
je     121c <read_file_2_buffer+0xac>
test   %r12,%r12
je     11ad <read_file_2_buffer+0x3d>
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
cmp    %r13,%rax
je     11fb <read_file_2_buffer+0x8b>
lea    0xe42(%rip),%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xe30(%rip),%rsi
mov    $0x3,%edi
call   1090 <exitc@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1080 <get_file_size@plt>
mov    %rax,%r13
test   %r15,%r15
jne    1194 <read_file_2_buffer+0x24>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xdd5(%rip),%rsi
mov    $0x2,%edi
call   1090 <exitc@plt>
test   %r12,%r12
jne    1199 <read_file_2_buffer+0x29>
jmp    11ad <read_file_2_buffer+0x3d>

LLVM IR (-O0):
@.str = [17 x i8] c"Can't open file!\00"
@SEEK_SET = global i32 0
@.str.1 = [16 x i8] c"res: %u lS: %u\0A\00"
@.str.2 = [17 x i8] c"Can't read file!\00"
define i64 @read_file_2_buffer(i8* %0, i32* %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%10 = load i64, i64* %8
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = load i32*, i32** %6
%14 = call i64 @get_file_size(i32* %13)
store i64 %14, i64* %8
br label %15
15: ; preds = %12, %4
%16 = load i32*, i32** %6
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i8*, i8** %5
%20 = call i32 @free(i8* %19)
%21 = call i32 @exitc(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %18, %15
%23 = load i64, i64* %7
%24 = icmp ugt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32*, i32** %6
%27 = load i64, i64* %7
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %26, i64 %27, i32 %28)
br label %30
30: ; preds = %25, %22
%31 = load i8*, i8** %5
%32 = load i64, i64* %8
%33 = load i32*, i32** %6
%34 = call i64 @fread(i8* %31, i32 1, i64 %32, i32* %33)
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = load i64, i64* %8
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %47
38: ; preds = %30
%39 = load i64, i64* %9
%40 = load i64, i64* %8
%41 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %39, i64 %40)
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = call i32 @exitc(i32 3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %38, %30
%48 = load i64, i64* %9
ret i64 %48
}

LLVM IR (-O2):
@.str = [17 x i8] c"Can't open file!\00"
@SEEK_SET = global i32 0
@.str.1 = [16 x i8] c"res: %u lS: %u\0A\00"
@.str.2 = [17 x i8] c"Can't read file!\00"
define i64 @read_file_2_buffer(i8* nocapture %0, i32* %1, i64 %2, i64 %3) {
%5 = icmp eq i64 %3, -1
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i64 @get_file_size(i32* %1)
br label %8
8: ; preds = %6, %4
%9 = phi i64 [ %7, %6 ], [ %3, %4 ]
%10 = icmp eq i32* %1, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @free(i8* %0)
%13 = tail call i32 @exitc(i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %8
%15 = icmp eq i64 %2, 0
br i1 %15, label %19, label %16
16: ; preds = %14
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %1, i64 %2, i32 %17)
br label %19
19: ; preds = %16, %14
%20 = tail call i64 @fread(i8* %0, i32 1, i64 %9, i32* %1)
%21 = icmp eq i64 %20, %9
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %20, i64 %9)
%24 = tail call i32 @free(i8* %0)
%25 = tail call i32 @fclose(i32* %1)
%26 = tail call i32 @exitc(i32 3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %22, %19
ret i64 %20
}