#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*/ uint8_t;

/* Variables and functions */
int GetFileSize(char *);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int read(int, int /*<<< orphan*/ *, int);
uint8_t *load_check_firmware(char *path, int *psize, int range_len) {
  uint8_t *buf = NULL;
  int size = 0;
  int fd = 0;
  size = GetFileSize(path);
  if (size == -1) {
    perror("get file size failed");
    return NULL;
  }
  if (size > range_len || size == 0) {
    printf(">>> size of firmware [%d] is larger than range [%d]\n", size,
           range_len);
    return NULL;
  }
  *psize = size;
  buf = malloc(size);
  if (buf == NULL) {
    perror("malloc failed");
    return NULL;
  }
  memset(buf, 0, size);
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    perror("open failed");
    free(buf);
    return NULL;
  }
  size = read(fd, buf, size);
  if (size != *psize) {
    perror("read firmware failed");
    free(buf);
    close(fd);
    return NULL;
  }
  close(fd);
  return buf;
}


x86 assembly (clang, -O0):
<load_check_firmware>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <GetFileSize@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e8 <load_check_firmware+0x58>
lea    0xe2a(%rip),%rdi
call   10b0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1300 <load_check_firmware+0x170>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     11fe <load_check_firmware+0x6e>
cmpl   $0x0,-0x2c(%rbp)
jne    121d <load_check_firmware+0x8d>
mov    -0x2c(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1300 <load_check_firmware+0x170>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1256 <load_check_firmware+0xc6>
lea    0xe06(%rip),%rdi
call   10b0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1300 <load_check_firmware+0x170>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    12a5 <load_check_firmware+0x115>
lea    0xdce(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1300 <load_check_firmware+0x170>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     12f0 <load_check_firmware+0x160>
lea    0xd97(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1300 <load_check_firmware+0x170>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_check_firmware>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r12
call   1090 <GetFileSize@plt>
cmp    $0xffffffff,%eax
je     1222 <load_check_firmware+0x92>
mov    %eax,%ebp
cmp    %ebx,%eax
jg     1203 <load_check_firmware+0x73>
test   %ebp,%ebp
je     1203 <load_check_firmware+0x73>
mov    %ebp,(%r14)
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     122b <load_check_firmware+0x9b>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1239 <load_check_firmware+0xa9>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1070 <read@plt>
cmp    (%r14),%eax
jne    124f <load_check_firmware+0xbf>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1216 <load_check_firmware+0x86>
lea    0xe0b(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
jmp    1232 <load_check_firmware+0xa2>
lea    0xe18(%rip),%rdi
call   10b0 <perror@plt>
jmp    1213 <load_check_firmware+0x83>
lea    0xe18(%rip),%rdi
call   10b0 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1213 <load_check_firmware+0x83>
lea    0xe0e(%rip),%rdi
call   10b0 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1213 <load_check_firmware+0x83>

LLVM IR (-O0):
@.str = [21 x i8] c"get file size failed\00"
@.str.1 = [53 x i8] c">>> size of firmware [%d] is larger than range [%d]\0A\00"
@.str.2 = [14 x i8] c"malloc failed\00"
@O_RDONLY = global i32 0
@.str.3 = [12 x i8] c"open failed\00"
@.str.4 = [21 x i8] c"read firmware failed\00"
define i32* @load_check_firmware(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32* null, i32** %8
store i32 0, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** %5
%12 = call i32 @GetFileSize(i8* %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %4
br label %69
17: ; preds = %3
%18 = load i32, i32* %9
%19 = load i32, i32* %7
%20 = icmp sgt i32 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21, %17
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %26)
store i32* null, i32** %4
br label %69
28: ; preds = %21
%29 = load i32, i32* %9
%30 = load i32*, i32** %6
store i32 %29, i32* %30
%31 = load i32, i32* %9
%32 = call i32* @malloc(i32 %31)
store i32* %32, i32** %8
%33 = load i32*, i32** %8
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32* null, i32** %4
br label %69
37: ; preds = %28
%38 = load i32*, i32** %8
%39 = load i32, i32* %9
%40 = call i32 @memset(i32* %38, i32 0, i32 %39)
%41 = load i8*, i8** %5
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %37
%47 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32*, i32** %8
%49 = call i32 @free(i32* %48)
store i32* null, i32** %4
br label %69
50: ; preds = %37
%51 = load i32, i32* %10
%52 = load i32*, i32** %8
%53 = load i32, i32* %9
%54 = call i32 @read(i32 %51, i32* %52, i32 %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = load i32*, i32** %6
%57 = load i32, i32* %56
%58 = icmp ne i32 %55, %57
br i1 %58, label %59, label %65
59: ; preds = %50
%60 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32*, i32** %8
%62 = call i32 @free(i32* %61)
%63 = load i32, i32* %10
%64 = call i32 @close(i32 %63)
store i32* null, i32** %4
br label %69
65: ; preds = %50
%66 = load i32, i32* %10
%67 = call i32 @close(i32 %66)
%68 = load i32*, i32** %8
store i32* %68, i32** %4
br label %69
69: ; preds = %65, %59, %46, %35, %24, %15
%70 = load i32*, i32** %4
ret i32* %70
}

LLVM IR (-O2):
@.str = [21 x i8] c"get file size failed\00"
@.str.1 = [53 x i8] c">>> size of firmware [%d] is larger than range [%d]\0A\00"
@.str.2 = [14 x i8] c"malloc failed\00"
@O_RDONLY = global i32 0
@.str.3 = [12 x i8] c"open failed\00"
@.str.4 = [21 x i8] c"read firmware failed\00"
define i32* @load_check_firmware(i8* %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32 @GetFileSize(i8* %0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %37
8: ; preds = %3
%9 = icmp sgt i32 %4, %2
%10 = icmp eq i32 %4, 0
%11 = or i1 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %2)
br label %37
14: ; preds = %8
store i32 %4, i32* %1
%15 = tail call i32* @malloc(i32 %4)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %37
19: ; preds = %14
%20 = tail call i32 @memset(i32* %15, i32 0, i32 %4)
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 @open(i8* %0, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 @free(i32* %15)
br label %37
27: ; preds = %19
%28 = tail call i32 @read(i32 %22, i32* %15, i32 %4)
%29 = load i32, i32* %1
%30 = icmp eq i32 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%33 = tail call i32 @free(i32* %15)
%34 = tail call i32 @close(i32 %22)
br label %37
35: ; preds = %27
%36 = tail call i32 @close(i32 %22)
br label %37
37: ; preds = %35, %31, %24, %17, %12, %6
%38 = phi i32* [ null, %6 ], [ null, %12 ], [ null, %17 ], [ null, %24 ], [ null, %31 ], [ %15, %35 ]
ret i32* %38
}