#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int fLen(char *fname) {
  FILE *fp;
  int len = 0;
  char ch = '\0';
  fp = fopen(fname, "r");
  if (fp == NULL) {
    perror("Error opening file");
    return (-1);
  }
  while (1) {
    ch = fgetc(fp);
    if (ch == EOF) {
      break;
    }
    len++;
  }
  fclose(fp);
  return len;
}


x86 assembly (clang, -O0):
<fLen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x0,-0x1d(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118e <fLen+0x4e>
lea    0xe85(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <fLen+0x97>
jmp    1193 <fLen+0x53>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
mov    0x2e3e(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11ba <fLen+0x7a>
jmp    11c8 <fLen+0x88>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1193 <fLen+0x53>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fLen>:
push   %r15
push   %r14
push   %rbx
lea    0xeb4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1190 <fLen+0x50>
mov    %rax,%r14
mov    $0xffffffff,%ebx
mov    0x2e83(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1040 <fgetc@plt>
add    $0x1,%ebx
cmp    (%r15),%al
jne    1170 <fLen+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1188 <fLen+0x48>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Error opening file\00"
@EOF = global i8 0
define i32 @fLen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 0, i32* %5
store i8 0, i8* %6
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
13: ; preds = %1
br label %14
14: ; preds = %13, %23
%15 = load i32*, i32** %4
%16 = call signext i8 @fgetc(i32* %15)
store i8 %16, i8* %6
%17 = load i8, i8* %6
%18 = sext i8 %17 to i32
%19 = load i8, i8* @EOF
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %18, %20
br i1 %21, label %22, label %23
22: ; preds = %14
br label %26
23: ; preds = %14
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %14
26: ; preds = %22
%27 = load i32*, i32** %4
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %5
store i32 %29, i32* %2
br label %30
30: ; preds = %26, %11
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Error opening file\00"
@EOF = global i8 0
define i32 @fLen(i8* nocapture %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 %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %14
6: ; preds = %1, %6
%7 = phi i32 [ %11, %6 ], [ 0, %1 ]
%8 = tail call signext i8 @fgetc(i32* %2)
%9 = load i8, i8* @EOF
%10 = icmp eq i8 %8, %9
%11 = add nuw nsw i32 %7, 1
br i1 %10, label %12, label %6
12: ; preds = %6
%13 = tail call i32 @fclose(i32* %2)
br label %14
14: ; preds = %12, %4
%15 = phi i32 [ -1, %4 ], [ %7, %12 ]
ret i32 %15
}