#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int filesize(char *ifile) {
  FILE *fp;
  int ret;
  if ((fp = fopen(ifile, "rb")) == (FILE *)NULL) {
    fprintf(stderr, "ERROR : filesize : file %s could not be opened\n", ifile);
    return (-2);
  }
  if (fseek(fp, 0, SEEK_END)) {
    fprintf(stderr, "ERROR : filesize : seeking to EOF of file %s failed\n",
            ifile);
    return (-3);
  }
  if ((ret = ftell(fp)) < 0) {
    fprintf(stderr, "ERROR : filesize : ftell at EOF of file %s failed\n",
            ifile);
    return (-4);
  }
  fclose(fp);
  return (ret);
}


x86 assembly (clang, -O0):
<filesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119f <filesize+0x4f>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe75(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    122b <filesize+0xdb>
mov    -0x18(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11e2 <filesize+0x92>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe62(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    122b <filesize+0xdb>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    121c <filesize+0xcc>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    122b <filesize+0xdb>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filesize>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a2 <filesize+0x52>
mov    %rax,%rbx
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
je     11c1 <filesize+0x71>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0xfffffffd,%ebp
jmp    11f6 <filesize+0xa6>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0xfffffffe,%ebp
jmp    11f6 <filesize+0xa6>
mov    %rbx,%rdi
call   1050 <ftell@plt>
test   %eax,%eax
js     11d9 <filesize+0x89>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11f6 <filesize+0xa6>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0xfffffffc,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [48 x i8] c"ERROR : filesize : file %s could not be opened\0A\00"
@SEEK_END = global i32 0
@.str.2 = [53 x i8] c"ERROR : filesize : seeking to EOF of file %s failed\0A\00"
@.str.3 = [51 x i8] c"ERROR : filesize : ftell at EOF of file %s failed\0A\00"
define i32 @filesize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %3
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32 -2, i32* %2
br label %34
13: ; preds = %1
%14 = load i32*, i32** %4
%15 = load i32, i32* @SEEK_END
%16 = call i64 @fseek(i32* %14, i32 0, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %20)
store i32 -3, i32* %2
br label %34
22: ; preds = %13
%23 = load i32*, i32** %4
%24 = call i32 @ftell(i32* %23)
store i32 %24, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %3
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %28)
store i32 -4, i32* %2
br label %34
30: ; preds = %22
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* %5
store i32 %33, i32* %2
br label %34
34: ; preds = %30, %26, %18, %9
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [48 x i8] c"ERROR : filesize : file %s could not be opened\0A\00"
@SEEK_END = global i32 0
@.str.2 = [53 x i8] c"ERROR : filesize : seeking to EOF of file %s failed\0A\00"
@.str.3 = [51 x i8] c"ERROR : filesize : ftell at EOF of file %s failed\0A\00"
define i32 @filesize(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i64 @fseek(i32* %2, i32 0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %22
14: ; preds = %7
%15 = tail call i32 @ftell(i32* %2)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %22
20: ; preds = %14
%21 = tail call i32 @fclose(i32* %2)
br label %22
22: ; preds = %20, %17, %11, %4
%23 = phi i32 [ -2, %4 ], [ -3, %11 ], [ -4, %17 ], [ %15, %20 ]
ret i32 %23
}