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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(unsigned char *);
int get_file_size(char *);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
unsigned char *read_edid_file(char *path, int *psize) {
  unsigned char *buf = NULL;
  int size = 0;
  int fd = 0;
  int ret = 0;
  size = get_file_size(path);
  if (size == -1) {
    perror("get file size failed");
    return NULL;
  }
  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;
  }
  *psize = size;
  ret = read(fd, buf, size);
  if (size != ret) {
    perror("read firmware failed");
    free(buf);
    close(fd);
    return NULL;
  }
  close(fd);
  return buf;
}


x86 assembly (clang, -O0):
<read_edid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <get_file_size@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11dc <read_edid_file+0x5c>
lea    0xe36(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <read_edid_file+0x13c>
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120c <read_edid_file+0x8c>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <read_edid_file+0x13c>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    125b <read_edid_file+0xdb>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <read_edid_file+0x13c>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     12ac <read_edid_file+0x12c>
lea    0xda6(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <read_edid_file+0x13c>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_edid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1080 <get_file_size@plt>
cmp    $0xffffffff,%eax
je     11f3 <read_edid_file+0x73>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11fc <read_edid_file+0x7c>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     120d <read_edid_file+0x8d>
mov    %eax,%ebx
mov    %ebp,(%r14)
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
cmp    %eax,%ebp
jne    1226 <read_edid_file+0xa6>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe06(%rip),%rdi
jmp    1203 <read_edid_file+0x83>
lea    0xe12(%rip),%rdi
call   10a0 <perror@plt>
xor    %r15d,%r15d
jmp    11e5 <read_edid_file+0x65>
lea    0xe0f(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    11e5 <read_edid_file+0x65>
lea    0xe02(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %r15d,%r15d
jmp    11e5 <read_edid_file+0x65>

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

LLVM IR (-O2):
@.str = [21 x i8] c"get file size failed\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@O_RDONLY = global i32 0
@.str.2 = [12 x i8] c"open failed\00"
@.str.3 = [21 x i8] c"read firmware failed\00"
define i8* @read_edid_file(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @get_file_size(i8* %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %29
7: ; preds = %2
%8 = tail call i8* @malloc(i32 %3)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %29
12: ; preds = %7
%13 = tail call i32 @memset(i8* %8, i32 0, i32 %3)
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %0, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @free(i8* %8)
br label %29
20: ; preds = %12
store i32 %3, i32* %1
%21 = tail call i32 @read(i32 %15, i8* %8, i32 %3)
%22 = icmp eq i32 %3, %21
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @free(i8* %8)
%26 = tail call i32 @close(i32 %15)
br label %29
27: ; preds = %20
%28 = tail call i32 @close(i32 %15)
br label %29
29: ; preds = %27, %23, %17, %10, %5
%30 = phi i8* [ null, %5 ], [ null, %10 ], [ null, %17 ], [ null, %23 ], [ %8, %27 ]
ret i8* %30
}