#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 EOF;
int /*<<< orphan*/ SEEK_END;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
long int countBytesInFile(char *file) {
  FILE *fp = fopen(file, "r");
  if (fp == NULL) {
    perror("fopen");
    fprintf(stderr, "Could not open file: %s\n", file);
    return -1;
  }
  int seek = fseek(fp, 0L, SEEK_END);
  if (seek != 0) {
    perror("fseek");
    return -1;
  }
  long int size = ftell(fp);
  if (size == -1) {
    perror("ftell");
    return -1;
  }
  int closed = fclose(fp);
  if (closed == EOF) {
    perror("fclose");
    return -1;
  }
  return size;
}


x86 assembly (clang, -O0):
<countBytesInFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bd <countBytesInFile+0x5d>
lea    0xe70(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5d(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1269 <countBytesInFile+0x109>
mov    -0x18(%rbp),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <fseek@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11f9 <countBytesInFile+0x99>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1269 <countBytesInFile+0x109>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    122a <countBytesInFile+0xca>
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1269 <countBytesInFile+0x109>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%eax
jne    1261 <countBytesInFile+0x101>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1269 <countBytesInFile+0x109>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countBytesInFile>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bf <countBytesInFile+0x5f>
mov    %rax,%rbx
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %eax,%eax
jne    11e5 <countBytesInFile+0x85>
mov    %rbx,%rdi
call   1050 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     11ee <countBytesInFile+0x8e>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%eax
jne    1201 <countBytesInFile+0xa1>
lea    0xe70(%rip),%rdi
jmp    11f5 <countBytesInFile+0x95>
lea    0xe3c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
jmp    11fa <countBytesInFile+0x9a>
lea    0xe35(%rip),%rdi
jmp    11f5 <countBytesInFile+0x95>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not open file: %s\0A\00"
@SEEK_END = global i32 0
@.str.3 = [6 x i8] c"fseek\00"
@.str.4 = [6 x i8] c"ftell\00"
@EOF = global i32 0
@.str.5 = [7 x i8] c"fclose\00"
define i64 @countBytesInFile(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %15)
store i64 -1, i64* %2
br label %42
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i64 -1, i64* %2
br label %42
25: ; preds = %17
%26 = load i32*, i32** %4
%27 = call i64 @ftell(i32* %26)
store i64 %27, i64* %6
%28 = load i64, i64* %6
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i64 -1, i64* %2
br label %42
32: ; preds = %25
%33 = load i32*, i32** %4
%34 = call i32 @fclose(i32* %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = load i32, i32* @EOF
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i64 -1, i64* %2
br label %42
40: ; preds = %32
%41 = load i64, i64* %6
store i64 %41, i64* %2
br label %42
42: ; preds = %40, %38, %30, %23, %12
%43 = load i64, i64* %2
ret i64 %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not open file: %s\0A\00"
@SEEK_END = global i32 0
@.str.3 = [6 x i8] c"fseek\00"
@.str.4 = [6 x i8] c"ftell\00"
@EOF = global i32 0
@.str.5 = [7 x i8] c"fclose\00"
define i64 @countBytesInFile(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %25
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i64 0, i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %25
14: ; preds = %8
%15 = tail call i64 @ftell(i32* %2)
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %25
19: ; preds = %14
%20 = tail call i32 @fclose(i32* %2)
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %12, %23, %19, %17, %4
%26 = phi i64 [ -1, %4 ], [ -1, %12 ], [ -1, %17 ], [ -1, %23 ], [ %15, %19 ]
ret i64 %26
}