#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ assert(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
ssize_t files_size(char *path) {
  assert(path);
  FILE *file = fopen(path, "r");
  if (!file)
    return -1;
  if (fseek(file, 0L, SEEK_END)) {
    fclose(file);
    return -1;
  }
  long ret = ftell(file);
  fclose(file);
  return ret;
}


x86 assembly (clang, -O0):
<files_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe90(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <files_size+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <files_size+0x92>
mov    -0x18(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11c5 <files_size+0x75>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <files_size+0x92>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<files_size>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <assert@plt>
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11a4 <files_size+0x54>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %rax,%rax
jne    119c <files_size+0x4c>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define i32 @files_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 = call i32 @assert(i8* %6)
%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 ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i32 -1, i32* %2
br label %28
13: ; preds = %1
%14 = load i32*, i32** %4
%15 = load i32, i32* @SEEK_END
%16 = call i64 @fseek(i32* %14, i64 0, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
store i32 -1, i32* %2
br label %28
21: ; preds = %13
%22 = load i32*, i32** %4
%23 = call i64 @ftell(i32* %22)
store i64 %23, i64* %5
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
%26 = load i64, i64* %5
%27 = trunc i64 %26 to i32
store i32 %27, i32* %2
br label %28
28: ; preds = %21, %18, %12
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define i32 @files_size(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @fseek(i32* %3, i64 0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @fclose(i32* %3)
br label %15
11: ; preds = %5
%12 = tail call i64 @ftell(i32* %3)
%13 = tail call i32 @fclose(i32* %3)
%14 = trunc i64 %12 to i32
br label %15
15: ; preds = %1, %11, %9
%16 = phi i32 [ -1, %9 ], [ %14, %11 ], [ -1, %1 ]
ret i32 %16
}