#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char get_stream_encoding(int /*<<< orphan*/ *);
scalar_t__ getc(int /*<<< orphan*/ *);
char get_file_encoding(const char *filename) {
  FILE *file = fopen(filename, "rb");
  if (file) {
    char rv = 'F';
    if (getc(file) == EOF) {
      rv = 'Z';
    } else {
      rv = get_stream_encoding(file);
    }
    fclose(file);
    return rv;
  } else {
    return 'F';
  }
}


x86 assembly (clang, -O0):
<get_file_encoding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b1 <get_file_encoding+0x71>
movb   $0x46,-0x19(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <getc@plt>
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%rax
jne    1191 <get_file_encoding+0x51>
movb   $0x5a,-0x19(%rbp)
jmp    119d <get_file_encoding+0x5d>
mov    -0x18(%rbp),%rdi
call   1040 <get_stream_encoding@plt>
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x19(%rbp),%al
mov    %al,-0x1(%rbp)
jmp    11b5 <get_file_encoding+0x75>
movb   $0x46,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_encoding>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1182 <get_file_encoding+0x42>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <getc@plt>
mov    0x2e82(%rip),%rcx
mov    $0x5a,%bpl
cmp    (%rcx),%rax
je     1178 <get_file_encoding+0x38>
mov    %rbx,%rdi
call   1040 <get_stream_encoding@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1185 <get_file_encoding+0x45>
mov    $0x46,%bpl
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@EOF = global i64 0
define signext i8 @get_file_encoding(i8* %0) {
%2 = alloca i8
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %23
10: ; preds = %1
store i8 70, i8* %5
%11 = load i32*, i32** %4
%12 = call i64 @getc(i32* %11)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %10
store i8 90, i8* %5
br label %19
16: ; preds = %10
%17 = load i32*, i32** %4
%18 = call signext i8 @get_stream_encoding(i32* %17)
store i8 %18, i8* %5
br label %19
19: ; preds = %16, %15
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
%22 = load i8, i8* %5
store i8 %22, i8* %2
br label %24
23: ; preds = %1
store i8 70, i8* %2
br label %24
24: ; preds = %23, %19
%25 = load i8, i8* %2
ret i8 %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@EOF = global i64 0
define signext i8 @get_file_encoding(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = tail call i64 @getc(i32* %2)
%6 = load i64, i64* @EOF
%7 = icmp eq i64 %5, %6
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call signext i8 @get_stream_encoding(i32* %2)
br label %10
10: ; preds = %4, %8
%11 = phi i8 [ %9, %8 ], [ 90, %4 ]
%12 = tail call i32 @fclose(i32* %2)
br label %13
13: ; preds = %1, %10
%14 = phi i8 [ %11, %10 ], [ 70, %1 ]
ret i8 %14
}