#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ testExt(char *, char *);
int getFileType(char *path) {
  FILE *file = fopen(path, "rb");
  char buf[9];
  int res = 0;
  if (testExt(path, "tap")) {
    res = 3;
  } else if (testExt(path, "scl") && file) {
    fread(buf, 9, 1, file);
    if ((strncmp(buf, "SINCLAIR", 8) == 0) && ((unsigned)buf[8] < 129))
      res = 1;
  } else if (testExt(path, "trd") && file) {
    fseek(file, 0, SEEK_END);
    size_t size = ftell(file);
    fseek(file, 0x8e7, SEEK_SET);
    int type = fgetc(file);
    if ((size > 0x8ff) && (size < 0xa0001) && (type == 0x10))
      res = 2;
  }
  if (file)
    fclose(file);
  return res;
}


x86 assembly (clang, -O0):
<getFileType>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10a0 <testExt@plt>
cmp    $0x0,%rax
je     11cd <getFileType+0x4d>
movl   $0x3,-0x20(%rbp)
jmp    12e8 <getFileType+0x168>
mov    -0x8(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   10a0 <testExt@plt>
cmp    $0x0,%rax
je     1243 <getFileType+0xc3>
cmpq   $0x0,-0x10(%rbp)
je     1243 <getFileType+0xc3>
lea    -0x19(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x9,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
lea    -0x19(%rbp),%rdi
lea    0xdf7(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    123e <getFileType+0xbe>
movsbl -0x11(%rbp),%eax
cmp    $0x81,%eax
jae    123e <getFileType+0xbe>
movl   $0x1,-0x20(%rbp)
jmp    12e3 <getFileType+0x163>
mov    -0x8(%rbp),%rdi
lea    0xdc6(%rip),%rsi
call   10a0 <testExt@plt>
cmp    $0x0,%rax
je     12de <getFileType+0x15e>
cmpq   $0x0,-0x10(%rbp)
je     12de <getFileType+0x15e>
mov    -0x10(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
mov    $0x8e7,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x8ff,-0x28(%rbp)
jbe    12d9 <getFileType+0x159>
cmpq   $0xa0001,-0x28(%rbp)
jae    12d9 <getFileType+0x159>
cmpl   $0x10,-0x2c(%rbp)
jne    12d9 <getFileType+0x159>
movl   $0x2,-0x20(%rbp)
jmp    12de <getFileType+0x15e>
jmp    12e3 <getFileType+0x163>
jmp    12e8 <getFileType+0x168>
cmpq   $0x0,-0x10(%rbp)
je     12fc <getFileType+0x17c>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFileType>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r14
lea    0xe62(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <testExt@plt>
mov    $0x3,%ebp
test   %rax,%rax
je     11c1 <getFileType+0x41>
test   %r14,%r14
jne    1296 <getFileType+0x116>
jmp    129e <getFileType+0x11e>
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <testExt@plt>
test   %r14,%r14
je     121d <getFileType+0x9d>
test   %rax,%rax
je     121d <getFileType+0x9d>
lea    0x7(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x9,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
lea    0xe10(%rip),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
sete   %al
cmpb   $0x0,0xf(%rsp)
setns  %cl
and    %al,%cl
movzbl %cl,%ebp
jmp    1296 <getFileType+0x116>
lea    0xdf0(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <testExt@plt>
xor    %ebp,%ebp
test   %r14,%r14
je     11b3 <getFileType+0x33>
test   %rax,%rax
je     11b3 <getFileType+0x33>
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbx
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x8e7,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1060 <fgetc@plt>
add    $0xfffffffffffff700,%rbx
cmp    $0x9f701,%rbx
setb   %cl
cmp    $0x10,%eax
sete   %al
and    %cl,%al
movzbl %al,%ebp
add    %ebp,%ebp
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [4 x i8] c"tap\00"
@.str.2 = [4 x i8] c"scl\00"
@.str.3 = [9 x i8] c"SINCLAIR\00"
@.str.4 = [4 x i8] c"trd\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @getFileType(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [9 x i8]
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
store i32 0, i32* %5
%10 = load i8*, i8** %2
%11 = call i64 @testExt(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 3, i32* %5
br label %65
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i64 @testExt(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %14
%19 = load i32*, i32** %3
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %35
21: ; preds = %18
%22 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%23 = load i32*, i32** %3
%24 = call i32 @fread(i8* %22, i32 9, i32 1, i32* %23)
%25 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%26 = call i64 @strncmp(i8* %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %21
%29 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 8
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ult i32 %31, 129
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 1, i32* %5
br label %34
34: ; preds = %33, %28, %21
br label %64
35: ; preds = %18, %14
%36 = load i8*, i8** %2
%37 = call i64 @testExt(i8* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %63
39: ; preds = %35
%40 = load i32*, i32** %3
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %63
42: ; preds = %39
%43 = load i32*, i32** %3
%44 = load i32, i32* @SEEK_END
%45 = call i32 @fseek(i32* %43, i32 0, i32 %44)
%46 = load i32*, i32** %3
%47 = call i64 @ftell(i32* %46)
store i64 %47, i64* %6
%48 = load i32*, i32** %3
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @fseek(i32* %48, i32 2279, i32 %49)
%51 = load i32*, i32** %3
%52 = call i32 @fgetc(i32* %51)
store i32 %52, i32* %7
%53 = load i64, i64* %6
%54 = icmp ugt i64 %53, 2303
br i1 %54, label %55, label %62
55: ; preds = %42
%56 = load i64, i64* %6
%57 = icmp ult i64 %56, 655361
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = load i32, i32* %7
%60 = icmp eq i32 %59, 16
br i1 %60, label %61, label %62
61: ; preds = %58
store i32 2, i32* %5
br label %62
62: ; preds = %61, %58, %55, %42
br label %63
63: ; preds = %62, %39, %35
br label %64
64: ; preds = %63, %34
br label %65
65: ; preds = %64, %13
%66 = load i32*, i32** %3
%67 = icmp ne i32* %66, null
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32*, i32** %3
%70 = call i32 @fclose(i32* %69)
br label %71
71: ; preds = %68, %65
%72 = load i32, i32* %5
ret i32 %72
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [4 x i8] c"tap\00"
@.str.2 = [4 x i8] c"scl\00"
@.str.3 = [9 x i8] c"SINCLAIR\00"
@.str.4 = [4 x i8] c"trd\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @getFileType(i8* %0) {
%2 = alloca [9 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%5 = tail call i64 @testExt(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %37
7: ; preds = %1
%8 = tail call i64 @testExt(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp ne i64 %8, 0
%10 = icmp ne i32* %3, null
%11 = and i1 %10, %9
br i1 %11, label %12, label %21
12: ; preds = %7
%13 = call i32 @fread(i8* %4, i32 9, i32 1, i32* %3)
%14 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%15 = icmp eq i64 %14, 0
%16 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 8
%17 = load i8, i8* %16
%18 = icmp sgt i8 %17, -1
%19 = select i1 %15, i1 %18, i1 false
%20 = zext i1 %19 to i32
br label %40
21: ; preds = %7
%22 = tail call i64 @testExt(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp ne i64 %22, 0
%24 = and i1 %10, %23
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = load i32, i32* @SEEK_END
%27 = tail call i32 @fseek(i32* %3, i32 0, i32 %26)
%28 = tail call i64 @ftell(i32* %3)
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @fseek(i32* %3, i32 2279, i32 %29)
%31 = tail call i32 @fgetc(i32* %3)
%32 = add i64 %28, -2304
%33 = icmp ult i64 %32, 653057
%34 = icmp eq i32 %31, 16
%35 = and i1 %33, %34
%36 = select i1 %35, i32 2, i32 0
br label %40
37: ; preds = %1, %21
%38 = phi i32 [ 0, %21 ], [ 3, %1 ]
%39 = icmp eq i32* %3, null
br i1 %39, label %43, label %40
40: ; preds = %25, %12, %37
%41 = phi i32 [ %38, %37 ], [ %20, %12 ], [ %36, %25 ]
%42 = call i32 @fclose(i32* %3)
br label %43
43: ; preds = %40, %37
%44 = phi i32 [ %41, %40 ], [ %38, %37 ]
ret i32 %44
}