#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
long lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
long file_size(char *file) {
  int fd;
  void err_msg(char *msg, char *file);
  long size;
  if ((fd = open(file, O_RDONLY)) == -1) {
    err_msg("open", file);
    return (-1);
  }
  if ((size = lseek(fd, 0, SEEK_END)) == -1) {
    err_msg("lseek", file);
    return (-1);
  }
  if (close(fd) == -1) {
    err_msg("close", file);
    exit(errno);
  }
  return (size);
}


x86 assembly (clang, -O0):
<file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1197 <file_size+0x47>
mov    -0x10(%rbp),%rsi
lea    0xe7b(%rip),%rdi
call   1060 <err_msg@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    120c <file_size+0xbc>
mov    -0x14(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11d5 <file_size+0x85>
mov    -0x10(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1060 <err_msg@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    120c <file_size+0xbc>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1204 <file_size+0xb4>
mov    -0x10(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1060 <err_msg@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_size>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b0 <file_size+0x60>
mov    %eax,%ebp
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     11b9 <file_size+0x69>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11cf <file_size+0x7f>
lea    0xe71(%rip),%rdi
mov    %r14,%rsi
call   1060 <err_msg@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe49(%rip),%rdi
jmp    11c0 <file_size+0x70>
lea    0xe45(%rip),%rdi
mov    %r14,%rsi
call   1060 <err_msg@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@SEEK_END = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@.str.2 = [6 x i8] c"close\00"
@errno = global i32 0
define i64 @file_size(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** %3
store i64 -1, i64* %2
br label %29
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @SEEK_END
%15 = call i64 @lseek(i32 %13, i32 0, i32 %14)
store i64 %15, i64* %5
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i8*, i8** %3
store i64 -1, i64* %2
br label %29
19: ; preds = %12
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i8*, i8** %3
%25 = load i32, i32* @errno
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = load i64, i64* %5
store i64 %28, i64* %2
br label %29
29: ; preds = %27, %17, %10
%30 = load i64, i64* %2
ret i64 %30
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@SEEK_END = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@.str.2 = [6 x i8] c"close\00"
@errno = global i32 0
define i64 @file_size(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @lseek(i32 %3, i32 0, i32 %6)
%8 = icmp eq i64 %7, -1
br i1 %8, label %15, label %9
9: ; preds = %5
%10 = tail call i32 @close(i32 %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %9
tail call void @err_msg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%13 = load i32, i32* @errno
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %5, %1
%16 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %5 ]
tail call void @err_msg(i8* %16, i8* %0)
br label %17
17: ; preds = %15, %9
%18 = phi i64 [ %7, %9 ], [ -1, %15 ]
ret i64 %18
}