#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ logger(int, char *);
int file_read(char *file_name, char *buf, int buf_len, char *mode) {
  FILE *fp;
  int file_size;
  int n_read = 0;
  fp = fopen(file_name, mode);
  if (NULL == fp) {
    logger(3, "file_read failed\n");
    return -1;
  }
  fseek(fp, 0, SEEK_END);
  file_size = ftell(fp);
  if (buf_len < file_size) {
    logger(3, "buf space is not enough for read file\n ");
    fclose(fp);
    return -1;
  }
  fseek(fp, 0, SEEK_SET);
  n_read = fread(buf, sizeof(unsigned char), file_size, fp);
  fclose(fp);
  return n_read;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    11b8 <file_read+0x58>
mov    $0x3,%edi
lea    0xe59(%rip),%rsi
call   1060 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <file_read+0xe5>
mov    -0x30(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    120a <file_read+0xaa>
mov    $0x3,%edi
lea    0xe22(%rip),%rsi
call   1060 <logger@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <file_read+0xe5>
mov    -0x30(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rcx,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11b8 <file_read+0x58>
mov    %rax,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
cmp    %r15d,%eax
jle    11d0 <file_read+0x70>
lea    0xe6e(%rip),%rsi
mov    $0x3,%edi
call   1060 <logger@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11c9 <file_read+0x69>
lea    0xe41(%rip),%rsi
mov    $0x3,%edi
call   1060 <logger@plt>
mov    $0xffffffff,%eax
jmp    1203 <file_read+0xa3>
mov    %eax,%ebp
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"file_read failed\0A\00"
@SEEK_END = global i32 0
@.str.1 = [40 x i8] c"buf space is not enough for read file\0A \00"
@SEEK_SET = global i32 0
define i32 @file_read(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %12
%13 = load i8*, i8** %6
%14 = load i8*, i8** %9
%15 = call i32* @fopen(i8* %13, i8* %14)
store i32* %15, i32** %10
%16 = load i32*, i32** %10
%17 = icmp eq i32* null, %16
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 @logger(i32 3, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %44
20: ; preds = %4
%21 = load i32*, i32** %10
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %10
%25 = call i32 @ftell(i32* %24)
store i32 %25, i32* %11
%26 = load i32, i32* %8
%27 = load i32, i32* %11
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = call i32 @logger(i32 3, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32*, i32** %10
%32 = call i32 @fclose(i32* %31)
store i32 -1, i32* %5
br label %44
33: ; preds = %20
%34 = load i32*, i32** %10
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @fseek(i32* %34, i32 0, i32 %35)
%37 = load i8*, i8** %7
%38 = load i32, i32* %11
%39 = load i32*, i32** %10
%40 = call i32 @fread(i8* %37, i32 1, i32 %38, i32* %39)
store i32 %40, i32* %12
%41 = load i32*, i32** %10
%42 = call i32 @fclose(i32* %41)
%43 = load i32, i32* %12
store i32 %43, i32* %5
br label %44
44: ; preds = %33, %29, %18
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@.str = [18 x i8] c"file_read failed\0A\00"
@SEEK_END = global i32 0
@.str.1 = [40 x i8] c"buf space is not enough for read file\0A \00"
@SEEK_SET = global i32 0
define i32 @file_read(i8* nocapture %0, i8* nocapture %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %0, i8* %3)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @logger(i32 3, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %4
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %5, i32 0, i32 %10)
%12 = tail call i32 @ftell(i32* %5)
%13 = icmp sgt i32 %12, %2
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i32 @logger(i32 3, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @fclose(i32* %5)
br label %22
17: ; preds = %9
%18 = load i32, i32* @SEEK_SET
%19 = tail call i32 @fseek(i32* %5, i32 0, i32 %18)
%20 = tail call i32 @fread(i8* %1, i32 1, i32 %12, i32* %5)
%21 = tail call i32 @fclose(i32* %5)
br label %22
22: ; preds = %17, %14, %7
%23 = phi i32 [ -1, %7 ], [ -1, %14 ], [ %20, %17 ]
ret i32 %23
}