#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*/ exit(int);
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*/ perror(char *);
int /*<<< orphan*/ stderr;
int lenchars_file(char *fn) {
  FILE *fd = fopen(fn, "r");
  if (fd == NULL) {
    fprintf(stderr, "Unable to open file <%s>: ", fn);
    perror("");
    exit(1);
  }
  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    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11da <lenchars_file+0x5a>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <lenchars_file+0x8a>
lea    0xe18(%rip),%rdi
call   10a0 <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    0xe72(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11df <lenchars_file+0x5f>
mov    %rax,%rbx
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <lenchars_file+0x46>
lea    0xe5c(%rip),%rdi
call   10a0 <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
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Unable to open file <%s>: \00"
@.str.2 = [1 x i8] zeroinitializer
@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 eq i32* %7, null
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i32*, i32** %3
%17 = load i32, i32* @SEEK_END
%18 = call i64 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i64, i64* @EOF
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i32*, i32** %3
%25 = call i32 @ftell(i32* %24)
store i32 %25, i32* %4
%26 = load i32*, i32** %3
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %4
ret i32 %28
}

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