#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 */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
bool checkSignature(char *filename) {
  FILE *fp;
  char signature[2];
  fp = fopen(filename, "r");
  if (fp == NULL) {
    printf("File non trovato!\n");
    return false;
  }
  fseek(fp, 0, 0);
  fread(signature, sizeof(char), 2, fp);
  if (signature[0] == 'B' && signature[1] == 'M')
    return true;
  else {
    printf("Codifica file errata!\n");
    return false;
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<checkSignature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1183 <checkSignature+0x43>
lea    0xe90(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <checkSignature+0xa0>
mov    -0x18(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <fseek@plt>
lea    -0x1a(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
movsbl -0x1a(%rbp),%eax
cmp    $0x42,%eax
jne    11cd <checkSignature+0x8d>
movsbl -0x19(%rbp),%eax
cmp    $0x4d,%eax
jne    11cd <checkSignature+0x8d>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <checkSignature+0xa0>
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkSignature>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1196 <checkSignature+0x56>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <fseek@plt>
lea    0x6(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmpb   $0x42,0x6(%rsp)
lea    0xe77(%rip),%rdi
jne    119d <checkSignature+0x5d>
cmpb   $0x4d,0x7(%rsp)
je     11a4 <checkSignature+0x64>
jmp    119d <checkSignature+0x5d>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"File non trovato!\0A\00"
@.str.2 = [23 x i8] c"Codifica file errata!\0A\00"
define i32 @checkSignature(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [2 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %30
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = call i32 @fseek(i32* %13, i32 0, i32 0)
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%16 = load i32*, i32** %4
%17 = call i32 @fread(i8* %15, i32 1, i32 2, i32* %16)
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 66
br i1 %21, label %22, label %28
22: ; preds = %12
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 77
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 1, i32* %2
br label %30
28: ; preds = %22, %12
%29 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %30
30: ; preds = %28, %27, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@str = [22 x i8] c"Codifica file errata!\00"
@str.3 = [18 x i8] c"File non trovato!\00"
define i32 @checkSignature(i8* nocapture %0) {
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %1
%7 = tail call i32 @fseek(i32* %4, i32 0, i32 0)
%8 = call i32 @fread(i8* %3, i32 1, i32 2, i32* %4)
%9 = load i8, i8* %3
%10 = icmp eq i8 %9, 66
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 77
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %18, label %15
15: ; preds = %6, %1
%16 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @str.3, i64 0, i64 0), %1 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0), %6 ]
%17 = tail call i32 @puts(i8* dereferenceable(1) %16)
br label %18
18: ; preds = %15, %6
%19 = phi i32 [ 1, %6 ], [ 0, %15 ]
ret i32 %19
}