#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ decode_mibs(unsigned char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int process_mib_file(const char *mib_file) {
  FILE *fp;
  unsigned char *mib_data;
  int data_len;
  struct stat st;
  if (stat(mib_file, &st)) {
    perror("Failed to stat img file");
    return -1;
  }
  data_len = st.st_size;
  fp = fopen(mib_file, "r");
  if (!fp) {
    perror("Failed to open MIB file");
    exit(1);
  }
  mib_data = (unsigned char *)malloc(data_len);
  if (mib_data == NULL) {
    perror("Failed to allocate memory for firmware");
    fclose(fp);
    return -1;
  }
  if (fread(mib_data, 1, data_len, fp) != data_len) {
    perror("Failed to read MIB file");
    free(mib_data);
    fclose(fp);
    return -1;
  }
  fclose(fp);
  decode_mibs(mib_data, data_len);
  free(mib_data);
  return 0;
}


x86 assembly (clang, -O0):
<process_mib_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11cb <process_mib_file+0x3b>
lea    0xe46(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <process_mib_file+0x11b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1206 <process_mib_file+0x76>
lea    0xe23(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123e <process_mib_file+0xae>
lea    0xe0e(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <process_mib_file+0x11b>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
cmp    -0x24(%rbp),%eax
je     1286 <process_mib_file+0xf6>
lea    0xdf6(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <process_mib_file+0x11b>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <decode_mibs@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_mib_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    1213 <process_mib_file+0x83>
mov    0x8(%rsp),%ebp
lea    0xe63(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1257 <process_mib_file+0xc7>
mov    %rax,%r14
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1226 <process_mib_file+0x96>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    %ebp,%eax
jne    1234 <process_mib_file+0xa4>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <decode_mibs@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    120a <process_mib_file+0x7a>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
jmp    1248 <process_mib_file+0xb8>
lea    0xe1e(%rip),%rdi
call   10a0 <perror@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%eax
jmp    120a <process_mib_file+0x7a>
lea    0xdbc(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [24 x i8] c"Failed to stat img file\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Failed to open MIB file\00"
@.str.3 = [39 x i8] c"Failed to allocate memory for firmware\00"
@.str.4 = [24 x i8] c"Failed to read MIB file\00"
define i32 @process_mib_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @stat(i8* %8, %struct.stat* %7)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %6
%16 = load i8*, i8** %3
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = load i32, i32* %6
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
store i32 -1, i32* %2
br label %54
33: ; preds = %23
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = load i32*, i32** %4
%37 = call i32 @fread(i8* %34, i32 1, i32 %35, i32* %36)
%38 = load i32, i32* %6
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
store i32 -1, i32* %2
br label %54
46: ; preds = %33
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %5
%50 = load i32, i32* %6
%51 = call i32 @decode_mibs(i8* %49, i32 %50)
%52 = load i8*, i8** %5
%53 = call i32 @free(i8* %52)
store i32 0, i32* %2
br label %54
54: ; preds = %46, %40, %29, %11
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [24 x i8] c"Failed to stat img file\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Failed to open MIB file\00"
@.str.3 = [39 x i8] c"Failed to allocate memory for firmware\00"
@.str.4 = [24 x i8] c"Failed to read MIB file\00"
define i32 @process_mib_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %34
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = call i64 @malloc(i32 %10)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @fclose(i32* %11)
br label %34
23: ; preds = %16
%24 = call i32 @fread(i8* %18, i32 1, i32 %10, i32* %11)
%25 = icmp eq i32 %24, %10
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @free(i8* %18)
%29 = call i32 @fclose(i32* %11)
br label %34
30: ; preds = %23
%31 = call i32 @fclose(i32* %11)
%32 = call i32 @decode_mibs(i8* %18, i32 %10)
%33 = call i32 @free(i8* %18)
br label %34
34: ; preds = %30, %26, %20, %6
%35 = phi i32 [ -1, %6 ], [ -1, %20 ], [ -1, %26 ], [ 0, %30 ]
ret i32 %35
}