#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 */
scalar_t__ EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fatal(char *);
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 lenchars_file(char *fn) {
  FILE *fd = fopen(fn, "r");
  if (!fd) {
    fprintf(stderr, "lenchars_file: fn = %s\n", fn);
    fatal("open failed");
  }
  if (fseek(fd, 0, SEEK_END) == EOF)
    fatal("seek failed");
  int len = ftell(fd);
  fclose(fd);
  return len;
}


x86 assembly (clang, -O0):
<lenchars_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b0 <lenchars_file+0x50>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe63(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe6f(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <lenchars_file+0x80>
lea    0xe4b(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lenchars_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119f <lenchars_file+0x3f>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
lea    0xe80(%rip),%rdi
call   1080 <fatal@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    11ca <lenchars_file+0x6a>
lea    0xe61(%rip),%rdi
call   1080 <fatal@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"lenchars_file: fn = %s\0A\00"
@.str.2 = [12 x i8] c"open failed\00"
@SEEK_END = global i32 0
@EOF = global i64 0
@.str.3 = [12 x i8] c"seek failed\00"
define i32 @lenchars_file(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 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %4, %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i64 @fseek(i32* %2, i32 0, i32 %9)
%11 = load i64, i64* @EOF
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = tail call i32 @ftell(i32* %2)
%17 = tail call i32 @fclose(i32* %2)
ret i32 %16
}