#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 uint32_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int crc16(int /*<<< orphan*/, unsigned char *, size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
uint16_t test(char *fname) {
  FILE *file;
  unsigned char buf[0x100] = {0};
  size_t read;
  uint32_t temp;
  file = fopen(fname, "r");
  if (file) {
    read = fread(buf, sizeof(char), 0x100, file);
    fclose(file);
    if (read < 14) {
      return 0xFFFF;
    }
    if (buf[0] != 'M') {
      return 0xFFFF;
    }
    if (!((unsigned char)(2 * buf[7]) == 14) || (buf[7] == 7)) {
      return 0xFFFF;
    }
    temp = buf[1];
    temp <<= 8;
    temp |= buf[2];
    temp <<= 8;
    temp |= buf[3];
    temp <<= 8;
    temp |= buf[4];
    if (temp != 0x2140244d) {
      return 0xFFFF;
    }
    return crc16(0, buf, read);
  } else {
    return 0xFFFF;
  }
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12cf <test+0x17f>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1030 <fread@plt>
mov    %rax,-0x128(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0xe,-0x128(%rbp)
0e
jae    11d5 <test+0x85>
movl   $0xffff,-0x4(%rbp)
jmp    12d6 <test+0x186>
movzbl -0x120(%rbp),%eax
cmp    $0x4d,%eax
je     11f1 <test+0xa1>
movl   $0xffff,-0x4(%rbp)
jmp    12d6 <test+0x186>
movzbl -0x119(%rbp),%eax
shl    $0x1,%eax
movzbl %al,%eax
cmp    $0xe,%eax
jne    1217 <test+0xc7>
movzbl -0x119(%rbp),%eax
cmp    $0x7,%eax
jne    1223 <test+0xd3>
movl   $0xffff,-0x4(%rbp)
jmp    12d6 <test+0x186>
movzbl -0x11f(%rbp),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x12c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x12c(%rbp)
movzbl -0x11e(%rbp),%eax
or     -0x12c(%rbp),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x12c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x12c(%rbp)
movzbl -0x11d(%rbp),%eax
or     -0x12c(%rbp),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x12c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x12c(%rbp)
movzbl -0x11c(%rbp),%eax
or     -0x12c(%rbp),%eax
mov    %eax,-0x12c(%rbp)
cmpl   $0x2140244d,-0x12c(%rbp)
24 40 21
je     12b2 <test+0x162>
movl   $0xffff,-0x4(%rbp)
jmp    12d6 <test+0x186>
lea    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%rdx
xor    %edi,%edi
call   1070 <crc16@plt>
mov    %eax,-0x4(%rbp)
jmp    12d6 <test+0x186>
movl   $0xffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe44(%rip),%rsi
call   1050 <fopen@plt>
mov    $0xffff,%ebp
test   %rax,%rax
je     1224 <test+0xe4>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0xe,%r14
jb     1224 <test+0xe4>
cmpb   $0x4d,(%rsp)
jne    1224 <test+0xe4>
mov    0x7(%rsp),%al
cmp    $0x7,%al
je     1224 <test+0xe4>
and    $0x7f,%al
cmp    $0x7,%al
jne    1224 <test+0xe4>
mov    0x1(%rsp),%eax
bswap  %eax
cmp    $0x2140244d,%eax
jne    1224 <test+0xe4>
mov    %rsp,%rsi
xor    %edi,%edi
mov    %r14,%rdx
call   1060 <crc16@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @test(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [256 x i8]
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = bitcast [256 x i8]* %5 to i8*
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %74
13: ; preds = %1
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i64 @fread(i8* %14, i32 1, i32 256, i32* %15)
store i64 %16, i64* %6
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
%19 = load i64, i64* %6
%20 = icmp ult i64 %19, 14
br i1 %20, label %21, label %22
21: ; preds = %13
store i32 65535, i32* %2
br label %75
22: ; preds = %13
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = icmp ne i32 %25, 77
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 65535, i32* %2
br label %75
28: ; preds = %22
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 7
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = mul nsw i32 2, %31
%33 = trunc i32 %32 to i8
%34 = zext i8 %33 to i32
%35 = icmp eq i32 %34, 14
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 7
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = icmp eq i32 %39, 7
br i1 %40, label %41, label %42
41: ; preds = %36, %28
store i32 65535, i32* %2
br label %75
42: ; preds = %36
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 1
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = shl i32 %46, 8
store i32 %47, i32* %7
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 2
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = load i32, i32* %7
%52 = or i32 %51, %50
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = shl i32 %53, 8
store i32 %54, i32* %7
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 3
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = load i32, i32* %7
%59 = or i32 %58, %57
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = shl i32 %60, 8
store i32 %61, i32* %7
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 4
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = load i32, i32* %7
%66 = or i32 %65, %64
store i32 %66, i32* %7
%67 = load i32, i32* %7
%68 = icmp ne i32 %67, 557851725
br i1 %68, label %69, label %70
69: ; preds = %42
store i32 65535, i32* %2
br label %75
70: ; preds = %42
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%72 = load i64, i64* %6
%73 = call i32 @crc16(i32 0, i8* %71, i64 %72)
store i32 %73, i32* %2
br label %75
74: ; preds = %1
store i32 65535, i32* %2
br label %75
75: ; preds = %74, %70, %69, %41, %27, %21
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @test(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 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 %42, label %6
6: ; preds = %1
%7 = call i64 @fread(i8* %3, i32 1, i32 256, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = icmp ugt i64 %7, 13
%10 = load i8, i8* %3
%11 = icmp eq i8 %10, 77
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %42
13: ; preds = %6
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 7
%15 = load i8, i8* %14
%16 = and i8 %15, 127
%17 = icmp ne i8 %16, 7
%18 = icmp eq i8 %15, 7
%19 = or i1 %18, %17
br i1 %19, label %42, label %20
20: ; preds = %13
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 2
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = shl nuw nsw i32 %23, 16
%28 = shl nuw nsw i32 %26, 8
%29 = or i32 %28, %27
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 3
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = or i32 %29, %32
%34 = shl nuw i32 %33, 8
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 4
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = or i32 %34, %37
%39 = icmp eq i32 %38, 557851725
br i1 %39, label %40, label %42
40: ; preds = %20
%41 = call i32 @crc16(i32 0, i8* %3, i64 %7)
br label %42
42: ; preds = %1, %20, %13, %6, %40
%43 = phi i32 [ %41, %40 ], [ 65535, %6 ], [ 65535, %13 ], [ 65535, %20 ], [ 65535, %1 ]
ret i32 %43
}