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

/* Variables and functions */
int /*<<< orphan*/ crc16ccitt_final(unsigned short *);
int /*<<< orphan*/ crc16ccitt_init(unsigned short *);
int /*<<< orphan*/ crc16ccitt_update(unsigned short *, unsigned char *, int);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, unsigned short, long);
unsigned short cal_crc(FILE *in) {
  int size;
  long file_size;
  unsigned short crc = 0;
  unsigned char buffer[80];
  file_size = size = 0;
  crc16ccitt_init(&crc);
  while (1) {
    size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
    file_size += size;
    if (size % 4 == 1) {
      buffer[size] = 0x0;
      buffer[size + 1] = 0x0;
      buffer[size + 2] = 0x0;
      size += 3;
    } else if (size % 4 == 2) {
      buffer[size] = 0x0;
      buffer[size + 1] = 0x0;
      size += 2;
    } else if (size % 4 == 3) {
      buffer[size] = 0x0;
      size += 1;
    }
    crc16ccitt_update(&crc, (unsigned char *)buffer, size);
    if (size < 80)
      goto ERR_DONE;
  }
ERR_DONE:
  crc16ccitt_final(&crc);
  printf(" ==> output crc 0x%04x with file size 0x%04x\n\r", crc, file_size);
  return crc;
}


x86 assembly (clang, -O0):
<cal_crc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movw   $0x0,-0x1a(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x1a(%rbp),%rdi
call   1070 <crc16ccitt_init@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x50,%edx
call   1030 <fread@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    11e5 <cal_crc+0x95>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0x2,%eax
cltq
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0xc(%rbp)
jmp    124d <cal_crc+0xfd>
mov    -0xc(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x2,%edx
jne    121d <cal_crc+0xcd>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
jmp    1248 <cal_crc+0xf8>
mov    -0xc(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x3,%edx
jne    1243 <cal_crc+0xf3>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1248 <cal_crc+0xf8>
jmp    124d <cal_crc+0xfd>
lea    -0x70(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x1a(%rbp),%rdi
call   1060 <crc16ccitt_update@plt>
cmpl   $0x50,-0xc(%rbp)
jge    126c <cal_crc+0x11c>
jmp    1271 <cal_crc+0x121>
jmp    117a <cal_crc+0x2a>
lea    -0x1a(%rbp),%rdi
call   1050 <crc16ccitt_final@plt>
mov    -0x1a(%rbp),%ax
mov    -0x18(%rbp),%rdx
lea    0xd77(%rip),%rdi
movzwl %ax,%esi
call   1040 <printf@plt>
movzwl -0x1a(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cal_crc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r14
movw   $0x0,0xe(%rsp)
lea    0xe(%rsp),%r15
mov    %r15,%rdi
call   1070 <crc16ccitt_init@plt>
xor    %r13d,%r13d
lea    0x10(%rsp),%r12
jmp    11ab <cal_crc+0x5b>
nop
mov    %rax,%rdx
movb   $0x0,0x10(%rsp,%rdx,1)
add    %ebx,%ebp
movslq %ebp,%rdx
mov    %ecx,%ebp
movb   $0x0,0x10(%rsp,%rdx,1)
add    %ebp,%ebx
add    %rax,%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <crc16ccitt_update@plt>
cmp    $0x4f,%ebx
jle    1205 <cal_crc+0xb5>
mov    $0x1,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x50,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %eax,%ebx
lea    0x3(%rbx),%edx
test   %eax,%eax
cmovns %eax,%edx
cltq
and    $0xfffffffc,%edx
mov    %ebx,%ecx
sub    %edx,%ecx
cmp    $0x3,%ecx
je     1200 <cal_crc+0xb0>
cmp    $0x2,%ecx
je     1180 <cal_crc+0x30>
cmp    $0x1,%ecx
jne    1196 <cal_crc+0x46>
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x1(%rbx),%ecx
movslq %ecx,%rdx
mov    $0x3,%ecx
mov    $0x2,%ebp
jmp    1183 <cal_crc+0x33>
xchg   %ax,%ax
mov    %rax,%rdx
jmp    118f <cal_crc+0x3f>
lea    0xe(%rsp),%rdi
call   1050 <crc16ccitt_final@plt>
movzwl 0xe(%rsp),%esi
lea    0xde5(%rip),%rdi
mov    %r13,%rdx
call   1040 <printf@plt>
movzwl 0xe(%rsp),%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c" ==> output crc 0x%04x with file size 0x%04x\0A\0D\00"
define zeroext i16 @cal_crc(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i16
%6 = alloca [80 x i8]
store i32* %0, i32** %2
store i16 0, i16* %5
store i32 0, i32* %3
store i64 0, i64* %4
%7 = call i32 @crc16ccitt_init(i16* %5)
br label %8
8: ; preds = %1, %66
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i32 @fread(i8* %9, i32 1, i32 80, i32* %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = load i64, i64* %4
%15 = add nsw i64 %14, %13
store i64 %15, i64* %4
%16 = load i32, i32* %3
%17 = srem i32 %16, 4
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %33
19: ; preds = %8
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %21
store i8 0, i8* %22
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 3
store i32 %32, i32* %3
br label %59
33: ; preds = %8
%34 = load i32, i32* %3
%35 = srem i32 %34, 4
%36 = icmp eq i32 %35, 2
br i1 %36, label %37, label %47
37: ; preds = %33
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %43
store i8 0, i8* %44
%45 = load i32, i32* %3
%46 = add nsw i32 %45, 2
store i32 %46, i32* %3
br label %58
47: ; preds = %33
%48 = load i32, i32* %3
%49 = srem i32 %48, 4
%50 = icmp eq i32 %49, 3
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = load i32, i32* %3
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
br label %57
57: ; preds = %51, %47
br label %58
58: ; preds = %57, %37
br label %59
59: ; preds = %58, %19
%60 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%61 = load i32, i32* %3
%62 = call i32 @crc16ccitt_update(i16* %5, i8* %60, i32 %61)
%63 = load i32, i32* %3
%64 = icmp slt i32 %63, 80
br i1 %64, label %65, label %66
65: ; preds = %59
br label %67
66: ; preds = %59
br label %8
67: ; preds = %65
%68 = call i32 @crc16ccitt_final(i16* %5)
%69 = load i16, i16* %5
%70 = load i64, i64* %4
%71 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i16 zeroext %69, i64 %70)
%72 = load i16, i16* %5
ret i16 %72
}

LLVM IR (-O2):
@.str = [47 x i8] c" ==> output crc 0x%04x with file size 0x%04x\0A\0D\00"
define zeroext i16 @cal_crc(i32* nocapture %0) {
%2 = alloca i16
%3 = alloca [80 x i8]
%4 = bitcast i16* %2 to i8*
store i16 0, i16* %2
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = call i32 @crc16ccitt_init(i16* %2)
br label %7
7: ; preds = %29, %1
%8 = phi i64 [ 0, %1 ], [ %11, %29 ]
%9 = call i32 @fread(i8* %5, i32 1, i32 80, i32* %0)
%10 = sext i32 %9 to i64
%11 = add nsw i64 %8, %10
%12 = srem i32 %9, 4
switch i32 %12, label %29 [
i32 1, label %13
i32 2, label %17
i32 3, label %24
]
13: ; preds = %7
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %10
store i8 0, i8* %14
%15 = add nsw i32 %9, 1
%16 = sext i32 %15 to i64
br label %17
17: ; preds = %7, %13
%18 = phi i64 [ %16, %13 ], [ %10, %7 ]
%19 = phi i32 [ 2, %13 ], [ 1, %7 ]
%20 = phi i32 [ 3, %13 ], [ %12, %7 ]
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %18
store i8 0, i8* %21
%22 = add nsw i32 %9, %19
%23 = sext i32 %22 to i64
br label %24
24: ; preds = %17, %7
%25 = phi i64 [ %10, %7 ], [ %23, %17 ]
%26 = phi i32 [ 1, %7 ], [ %20, %17 ]
%27 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %25
store i8 0, i8* %27
%28 = add nsw i32 %9, %26
br label %29
29: ; preds = %24, %7
%30 = phi i32 [ %9, %7 ], [ %28, %24 ]
%31 = call i32 @crc16ccitt_update(i16* %2, i8* %5, i32 %30)
%32 = icmp slt i32 %30, 80
br i1 %32, label %33, label %7
33: ; preds = %29
%34 = call i32 @crc16ccitt_final(i16* %2)
%35 = load i16, i16* %2
%36 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i16 zeroext %35, i64 %11)
%37 = load i16, i16* %2
ret i16 %37
}