#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() {
  FILE *file;
  unsigned char buf[0x100] = {0};
  size_t read;
  uint32_t temp;
  file = fopen("test.txt", "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    $0x120,%rsp
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xe8b(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ce <test+0x17e>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1030 <fread@plt>
mov    %rax,-0x118(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0xe,-0x118(%rbp)
0e
jae    11d4 <test+0x84>
movl   $0xffff,-0x4(%rbp)
jmp    12d5 <test+0x185>
movzbl -0x110(%rbp),%eax
cmp    $0x4d,%eax
je     11f0 <test+0xa0>
movl   $0xffff,-0x4(%rbp)
jmp    12d5 <test+0x185>
movzbl -0x109(%rbp),%eax
shl    $0x1,%eax
movzbl %al,%eax
cmp    $0xe,%eax
jne    1216 <test+0xc6>
movzbl -0x109(%rbp),%eax
cmp    $0x7,%eax
jne    1222 <test+0xd2>
movl   $0xffff,-0x4(%rbp)
jmp    12d5 <test+0x185>
movzbl -0x10f(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x11c(%rbp)
movzbl -0x10e(%rbp),%eax
or     -0x11c(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x11c(%rbp)
movzbl -0x10d(%rbp),%eax
or     -0x11c(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x11c(%rbp)
movzbl -0x10c(%rbp),%eax
or     -0x11c(%rbp),%eax
mov    %eax,-0x11c(%rbp)
cmpl   $0x2140244d,-0x11c(%rbp)
24 40 21
je     12b1 <test+0x161>
movl   $0xffff,-0x4(%rbp)
jmp    12d5 <test+0x185>
lea    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%rdx
xor    %edi,%edi
call   1070 <crc16@plt>
mov    %eax,-0x4(%rbp)
jmp    12d5 <test+0x185>
movl   $0xffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%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),%rdi
lea    0xe46(%rip),%rsi
call   1050 <fopen@plt>
mov    $0xffff,%ebp
test   %rax,%rax
je     122b <test+0xeb>
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     122b <test+0xeb>
cmpb   $0x4d,(%rsp)
jne    122b <test+0xeb>
mov    0x7(%rsp),%al
cmp    $0x7,%al
je     122b <test+0xeb>
and    $0x7f,%al
cmp    $0x7,%al
jne    122b <test+0xeb>
mov    0x1(%rsp),%eax
bswap  %eax
cmp    $0x2140244d,%eax
jne    122b <test+0xeb>
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 = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @test() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca i32
%6 = bitcast [256 x i8]* %3 to i8*
%7 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %71
10: ; preds = %0
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = load i32*, i32** %2
%13 = call i64 @fread(i8* %11, i32 1, i32 256, i32* %12)
store i64 %13, i64* %4
%14 = load i32*, i32** %2
%15 = call i32 @fclose(i32* %14)
%16 = load i64, i64* %4
%17 = icmp ult i64 %16, 14
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 65535, i32* %1
br label %72
19: ; preds = %10
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp ne i32 %22, 77
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 65535, i32* %1
br label %72
25: ; preds = %19
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 7
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = mul nsw i32 2, %28
%30 = trunc i32 %29 to i8
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %31, 14
br i1 %32, label %33, label %38
33: ; preds = %25
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 7
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = icmp eq i32 %36, 7
br i1 %37, label %38, label %39
38: ; preds = %33, %25
store i32 65535, i32* %1
br label %72
39: ; preds = %33
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 1
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = shl i32 %43, 8
store i32 %44, i32* %5
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 2
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = load i32, i32* %5
%49 = or i32 %48, %47
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = shl i32 %50, 8
store i32 %51, i32* %5
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 3
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = load i32, i32* %5
%56 = or i32 %55, %54
store i32 %56, i32* %5
%57 = load i32, i32* %5
%58 = shl i32 %57, 8
store i32 %58, i32* %5
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 4
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = load i32, i32* %5
%63 = or i32 %62, %61
store i32 %63, i32* %5
%64 = load i32, i32* %5
%65 = icmp ne i32 %64, 557851725
br i1 %65, label %66, label %67
66: ; preds = %39
store i32 65535, i32* %1
br label %72
67: ; preds = %39
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%69 = load i64, i64* %4
%70 = call i32 @crc16(i32 0, i8* %68, i64 %69)
store i32 %70, i32* %1
br label %72
71: ; preds = %0
store i32 65535, i32* %1
br label %72
72: ; preds = %71, %67, %66, %38, %24, %18
%73 = load i32, i32* %1
ret i32 %73
}

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