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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
off_t get_bootimg_size(const char *path) {
  struct stat info;
  if (stat(path, &info) < 0) {
    fprintf(stderr, "Failed to get boot image size (%s)!", strerror(errno));
    return -1;
  }
  if (info.st_size == 0) {
    off_t size;
    FILE *f;
    f = fopen(path, "r");
    if (!f) {
      fprintf(stderr, "Failed to get boot image size (%s)!", strerror(errno));
      return -1;
    }
    fseek(f, 0, SEEK_END);
    size = ftell(f);
    fclose(f);
    if (size <= 0) {
      fprintf(stderr,
              "Failed to get boot image size (fseek -> ftell failed, %s)!",
              strerror(errno));
      return -1;
    }
    return size;
  } else {
    return info.st_size;
  }
}


x86 assembly (clang, -O0):
<get_bootimg_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11cb <get_bootimg_size+0x5b>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe46(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <get_bootimg_size+0x13a>
cmpq   $0x0,-0x18(%rbp)
jne    12a3 <get_bootimg_size+0x133>
mov    -0x10(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122d <get_bootimg_size+0xbd>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xde4(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <get_bootimg_size+0x13a>
mov    -0x28(%rbp),%rdi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jg     1298 <get_bootimg_size+0x128>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xd9f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <get_bootimg_size+0x13a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12aa <get_bootimg_size+0x13a>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bootimg_size>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11f1 <get_bootimg_size+0x81>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    121e <get_bootimg_size+0xae>
lea    0xe8c(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f1 <get_bootimg_size+0x81>
mov    %rax,%r14
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
test   %ebx,%ebx
jg     121e <get_bootimg_size+0xae>
mov    0x2e00(%rip),%rax
mov    (%rax),%ebx
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe37(%rip),%rsi
jmp    120f <get_bootimg_size+0x9f>
mov    0x2de0(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [36 x i8] c"Failed to get boot image size (%s)!\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.2 = [59 x i8] c"Failed to get boot image size (fseek -> ftell failed, %s)!\00"
define i32 @get_bootimg_size(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @stat(i8* %7, %struct.stat* %4)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %2
br label %50
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %46
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 -1, i32* %2
br label %50
29: ; preds = %19
%30 = load i32*, i32** %6
%31 = load i32, i32* @SEEK_END
%32 = call i32 @fseek(i32* %30, i32 0, i32 %31)
%33 = load i32*, i32** %6
%34 = call i32 @ftell(i32* %33)
store i32 %34, i32* %5
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %5
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %29
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %42)
store i32 -1, i32* %2
br label %50
44: ; preds = %29
%45 = load i32, i32* %5
store i32 %45, i32* %2
br label %50
46: ; preds = %15
%47 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%48 = load i64, i64* %47
%49 = trunc i64 %48 to i32
store i32 %49, i32* %2
br label %50
50: ; preds = %46, %44, %39, %24, %10
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [36 x i8] c"Failed to get boot image size (%s)!\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.2 = [59 x i8] c"Failed to get boot image size (fseek -> ftell failed, %s)!\00"
define i32 @get_bootimg_size(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %36
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i64, i64* %12
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %11
%16 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %36
23: ; preds = %15
%24 = load i32, i32* @SEEK_END
%25 = call i32 @fseek(i32* %16, i32 0, i32 %24)
%26 = call i32 @ftell(i32* %16)
%27 = call i32 @fclose(i32* %16)
%28 = icmp slt i32 %26, 1
br i1 %28, label %29, label %36
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %36
34: ; preds = %11
%35 = trunc i64 %13 to i32
br label %36
36: ; preds = %18, %29, %23, %34, %6
%37 = phi i32 [ -1, %6 ], [ %35, %34 ], [ -1, %29 ], [ -1, %18 ], [ %26, %23 ]
ret i32 %37
}