#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ ui_printerr(char *, char const *, ...);
unsigned int get_file_size(const char *fn) {
  FILE *file;
  unsigned long fsize;
  if (!fn)
    return (unsigned int)-1;
  file = fopen(fn, "rb");
  if (!file) {
    ui_printerr("in fopen(%s) - %s\n\n", fn, strerror(errno));
    return (unsigned int)-1;
  }
  if (fseek(file, 0, SEEK_END) < 0)
    return (unsigned int)-1;
  fsize = ftell(file);
  if (fsize == (unsigned long)-1)
    return (unsigned int)-1;
  fclose(file);
  if (fsize > (unsigned long)((unsigned int)-1)) {
    ui_printerr("the file %s is too big. The donkey can only deal with files "
                "smaller than 4GB!\n\n",
                fn);
    return ((unsigned int)-1);
  }
  return (unsigned int)fsize;
}


x86 assembly (clang, -O0):
<get_file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <get_file_size+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <get_file_size+0x103>
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <get_file_size+0x78>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <ui_printerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <get_file_size+0x103>
mov    -0x18(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
jge    1202 <get_file_size+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <get_file_size+0x103>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1226 <get_file_size+0xc6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <get_file_size+0x103>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%eax
cmp    %rax,-0x20(%rbp)
jbe    125c <get_file_size+0xfc>
mov    -0x10(%rbp),%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <ui_printerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <get_file_size+0x103>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_size>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1200 <get_file_size+0xa0>
mov    %rdi,%rbx
lea    0xe80(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11da <get_file_size+0x7a>
mov    %rax,%rbp
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
js     1200 <get_file_size+0xa0>
mov    %rbp,%rdi
call   1050 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1200 <get_file_size+0xa0>
mov    %rax,%r15
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %r15,%rax
shr    $0x20,%rax
je     11fd <get_file_size+0x9d>
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <ui_printerr@plt>
jmp    1200 <get_file_size+0xa0>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe14(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <ui_printerr@plt>
jmp    1200 <get_file_size+0xa0>
mov    %r15d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"in fopen(%s) - %s\0A\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [80 x i8] c"the file %s is too big. The donkey can only deal with files smaller than 4GB!\0A\0A\00"
define i32 @get_file_size(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i32 -1, i32* %2
br label %42
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @ui_printerr(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %17)
store i32 -1, i32* %2
br label %42
19: ; preds = %9
%20 = load i32*, i32** %4
%21 = load i32, i32* @SEEK_END
%22 = call i64 @fseek(i32* %20, i32 0, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %2
br label %42
25: ; preds = %19
%26 = load i32*, i32** %4
%27 = call i64 @ftell(i32* %26)
store i64 %27, i64* %5
%28 = load i64, i64* %5
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -1, i32* %2
br label %42
31: ; preds = %25
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
%34 = load i64, i64* %5
%35 = icmp ugt i64 %34, 4294967295
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i8*, i8** %3
%38 = call i32 (i8*, i8*, ...) @ui_printerr(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.2, i64 0, i64 0), i8* %37)
store i32 -1, i32* %2
br label %42
39: ; preds = %31
%40 = load i64, i64* %5
%41 = trunc i64 %40 to i32
store i32 %41, i32* %2
br label %42
42: ; preds = %39, %36, %30, %24, %14, %8
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"in fopen(%s) - %s\0A\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [80 x i8] c"the file %s is too big. The donkey can only deal with files smaller than 4GB!\0A\0A\00"
define i32 @get_file_size(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %24, label %3
3: ; preds = %1
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 (i8*, i8*, ...) @ui_printerr(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %24
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i64 @fseek(i32* %4, i32 0, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %10
%15 = tail call i64 @ftell(i32* %4)
%16 = icmp eq i64 %15, -1
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = tail call i32 @fclose(i32* %4)
%19 = icmp ugt i64 %15, 4294967295
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 (i8*, i8*, ...) @ui_printerr(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %24
22: ; preds = %17
%23 = trunc i64 %15 to i32
br label %24
24: ; preds = %14, %10, %1, %22, %20, %6
%25 = phi i32 [ -1, %20 ], [ %23, %22 ], [ -1, %6 ], [ -1, %1 ], [ -1, %10 ], [ -1, %14 ]
ret i32 %25
}