#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 scalar_t__ i1;

/* Variables and functions */
scalar_t__ EOF;
unsigned short icrc(unsigned short, unsigned char *, unsigned long, short, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ scanf(char *, unsigned long *, unsigned char *);
int main(void) {
  unsigned char lin[256];
  unsigned short i1, i2;
  unsigned long n;
  for (;;) {
    printf("Enter length and string: \n");
    if (scanf("%lu %s", &n, &lin[1]) == EOF)
      break;
    lin[n + 1] = 0;
    printf("%s\n", &lin[1]);
    i1 = icrc(0, lin, n, (short)0, 1);
    lin[n + 1] = ((unsigned char)((i1) >> 8));
    lin[n + 2] = ((unsigned char)((i1)&0xFF));
    i2 = icrc(i1, lin, n + 2, (short)0, 1);
    printf("    XMODEM: String CRC, Packet CRC=    0x%x   0x%x\n", i1, i2);
    i1 = icrc(i2, lin, n, (short)0xff, -1);
    lin[n + 1] = ~((unsigned char)((i1)&0xFF));
    lin[n + 2] = ~((unsigned char)((i1) >> 8));
    i2 = icrc(i1, lin, n + 2, (short)0xff, -1);
    printf("      X.25: String CRC, Packet CRC=    0x%x   0x%x\n", i1, i2);
    i1 = icrc(i2, lin, n, (short)0, -1);
    lin[n + 1] = ((unsigned char)((i1)&0xFF));
    lin[n + 2] = ((unsigned char)((i1) >> 8));
    i2 = icrc(i1, lin, n + 2, (short)0, -1);
    printf(" CRC-CCITT: String CRC, Packet CRC=    0x%x   0x%x\n", i1, i2);
  }
  printf("Normal completion\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x110(%rbp),%rdx
add    $0x1,%rdx
lea    0xeb9(%rip),%rdi
lea    -0x120(%rbp),%rsi
call   1050 <scanf@plt>
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
jne    1183 <main+0x53>
jmp    13c4 <main+0x294>
mov    -0x120(%rbp),%rax
movb   $0x0,-0x10f(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
add    $0x1,%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x110(%rbp),%rsi
mov    -0x120(%rbp),%rdx
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    %ecx,%edi
call   1040 <icrc@plt>
mov    %ax,-0x112(%rbp)
movzwl -0x112(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x120(%rbp),%rax
mov    %cl,-0x10f(%rbp,%rax,1)
movzwl -0x112(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x120(%rbp),%rax
mov    %cl,-0x10e(%rbp,%rax,1)
mov    -0x112(%rbp),%ax
lea    -0x110(%rbp),%rsi
mov    -0x120(%rbp),%rdx
add    $0x2,%rdx
xor    %ecx,%ecx
mov    $0x1,%r8d
movzwl %ax,%edi
call   1040 <icrc@plt>
mov    %ax,-0x114(%rbp)
movzwl -0x112(%rbp),%esi
movzwl -0x114(%rbp),%edx
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x114(%rbp),%ax
lea    -0x110(%rbp),%rsi
mov    -0x120(%rbp),%rdx
mov    $0xff,%ecx
mov    $0xffffffff,%r8d
movzwl %ax,%edi
call   1040 <icrc@plt>
mov    %ax,-0x112(%rbp)
movzwl -0x112(%rbp),%eax
and    $0xff,%eax
movzbl %al,%eax
xor    $0xffffffff,%eax
mov    %al,%cl
mov    -0x120(%rbp),%rax
mov    %cl,-0x10f(%rbp,%rax,1)
movzwl -0x112(%rbp),%eax
sar    $0x8,%eax
movzbl %al,%eax
xor    $0xffffffff,%eax
mov    %al,%cl
mov    -0x120(%rbp),%rax
mov    %cl,-0x10e(%rbp,%rax,1)
mov    -0x112(%rbp),%ax
lea    -0x110(%rbp),%rsi
mov    -0x120(%rbp),%rdx
add    $0x2,%rdx
mov    $0xff,%ecx
mov    $0xffffffff,%r8d
movzwl %ax,%edi
call   1040 <icrc@plt>
mov    %ax,-0x114(%rbp)
movzwl -0x112(%rbp),%esi
movzwl -0x114(%rbp),%edx
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x114(%rbp),%ax
lea    -0x110(%rbp),%rsi
mov    -0x120(%rbp),%rdx
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
movzwl %ax,%edi
call   1040 <icrc@plt>
mov    %ax,-0x112(%rbp)
movzwl -0x112(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x120(%rbp),%rax
mov    %cl,-0x10f(%rbp,%rax,1)
movzwl -0x112(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x120(%rbp),%rax
mov    %cl,-0x10e(%rbp,%rax,1)
mov    -0x112(%rbp),%ax
lea    -0x110(%rbp),%rsi
mov    -0x120(%rbp),%rdx
add    $0x2,%rdx
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
movzwl %ax,%edi
call   1040 <icrc@plt>
mov    %ax,-0x114(%rbp)
movzwl -0x112(%rbp),%esi
movzwl -0x114(%rbp),%edx
lea    0xcd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1142 <main+0x12>
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xf4b(%rip),%rdi
call   1030 <puts@plt>
lea    0x11(%rsp),%r14
lea    0xe97(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %r14,%rdx
call   1060 <scanf@plt>
mov    0x2e6b(%rip),%r12
cmp    (%r12),%rax
je     12d9 <main+0x199>
lea    0x10(%rsp),%r15
nopl   0x0(%rax)
mov    0x8(%rsp),%rax
movb   $0x0,0x1(%rax,%r15,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rdx
xor    %edi,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
mov    $0x1,%r8d
call   1050 <icrc@plt>
movzwl %ax,%r13d
mov    0x8(%rsp),%rdx
mov    %r13d,%eax
rol    $0x8,%ax
mov    %ax,0x1(%rdx,%r15,1)
add    $0x2,%rdx
mov    %r13d,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
mov    $0x1,%r8d
call   1050 <icrc@plt>
movzwl %ax,%ebp
lea    0xe16(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xff,%ecx
mov    $0xffffffff,%r8d
call   1050 <icrc@plt>
movzwl %ax,%ebp
mov    %ebp,%eax
not    %al
mov    0x8(%rsp),%rdx
mov    %al,0x1(%rdx,%r15,1)
mov    %ebp,%eax
shr    $0x8,%eax
not    %al
mov    %al,0x12(%rsp,%rdx,1)
add    $0x2,%rdx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xff,%ecx
mov    $0xffffffff,%r8d
call   1050 <icrc@plt>
movzwl %ax,%ebx
lea    0xde5(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rdx
mov    %ebx,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
call   1050 <icrc@plt>
movzwl %ax,%ebx
mov    0x8(%rsp),%rdx
mov    %bx,0x1(%rdx,%r15,1)
add    $0x2,%rdx
mov    %ebx,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
call   1050 <icrc@plt>
movzwl %ax,%edx
lea    0xdc9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
lea    0xd3e(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %r14,%rdx
call   1060 <scanf@plt>
cmp    (%r12),%rax
jne    1190 <main+0x50>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Enter length and string: \0A\00"
@.str.1 = [7 x i8] c"%lu %s\00"
@EOF = global i64 0
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [52 x i8] c" XMODEM: String CRC, Packet CRC= 0x%x 0x%x\0A\00"
@.str.4 = [52 x i8] c" X.25: String CRC, Packet CRC= 0x%x 0x%x\0A\00"
@.str.5 = [52 x i8] c" CRC-CCITT: String CRC, Packet CRC= 0x%x 0x%x\0A\00"
@.str.6 = [19 x i8] c"Normal completion\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i16
%4 = alloca i16
%5 = alloca i64
store i32 0, i32* %1
br label %6
6: ; preds = %13, %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 1
%9 = call i64 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64* %5, i8* %8)
%10 = load i64, i64* @EOF
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %6
br label %108
13: ; preds = %6
%14 = load i64, i64* %5
%15 = add i64 %14, 1
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %15
store i8 0, i8* %16
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%20 = load i64, i64* %5
%21 = call zeroext i16 @icrc(i16 zeroext 0, i8* %19, i64 %20, i16 signext 0, i32 1)
store i16 %21, i16* %3
%22 = load i16, i16* %3
%23 = zext i16 %22 to i32
%24 = ashr i32 %23, 8
%25 = trunc i32 %24 to i8
%26 = load i64, i64* %5
%27 = add i64 %26, 1
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %27
store i8 %25, i8* %28
%29 = load i16, i16* %3
%30 = zext i16 %29 to i32
%31 = and i32 %30, 255
%32 = trunc i32 %31 to i8
%33 = load i64, i64* %5
%34 = add i64 %33, 2
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %34
store i8 %32, i8* %35
%36 = load i16, i16* %3
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%38 = load i64, i64* %5
%39 = add i64 %38, 2
%40 = call zeroext i16 @icrc(i16 zeroext %36, i8* %37, i64 %39, i16 signext 0, i32 1)
store i16 %40, i16* %4
%41 = load i16, i16* %3
%42 = zext i16 %41 to i32
%43 = load i16, i16* %4
%44 = zext i16 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %42, i32 %44)
%46 = load i16, i16* %4
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%48 = load i64, i64* %5
%49 = call zeroext i16 @icrc(i16 zeroext %46, i8* %47, i64 %48, i16 signext 255, i32 -1)
store i16 %49, i16* %3
%50 = load i16, i16* %3
%51 = zext i16 %50 to i32
%52 = and i32 %51, 255
%53 = trunc i32 %52 to i8
%54 = zext i8 %53 to i32
%55 = xor i32 %54, -1
%56 = trunc i32 %55 to i8
%57 = load i64, i64* %5
%58 = add i64 %57, 1
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %58
store i8 %56, i8* %59
%60 = load i16, i16* %3
%61 = zext i16 %60 to i32
%62 = ashr i32 %61, 8
%63 = trunc i32 %62 to i8
%64 = zext i8 %63 to i32
%65 = xor i32 %64, -1
%66 = trunc i32 %65 to i8
%67 = load i64, i64* %5
%68 = add i64 %67, 2
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %68
store i8 %66, i8* %69
%70 = load i16, i16* %3
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%72 = load i64, i64* %5
%73 = add i64 %72, 2
%74 = call zeroext i16 @icrc(i16 zeroext %70, i8* %71, i64 %73, i16 signext 255, i32 -1)
store i16 %74, i16* %4
%75 = load i16, i16* %3
%76 = zext i16 %75 to i32
%77 = load i16, i16* %4
%78 = zext i16 %77 to i32
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %76, i32 %78)
%80 = load i16, i16* %4
%81 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%82 = load i64, i64* %5
%83 = call zeroext i16 @icrc(i16 zeroext %80, i8* %81, i64 %82, i16 signext 0, i32 -1)
store i16 %83, i16* %3
%84 = load i16, i16* %3
%85 = zext i16 %84 to i32
%86 = and i32 %85, 255
%87 = trunc i32 %86 to i8
%88 = load i64, i64* %5
%89 = add i64 %88, 1
%90 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %89
store i8 %87, i8* %90
%91 = load i16, i16* %3
%92 = zext i16 %91 to i32
%93 = ashr i32 %92, 8
%94 = trunc i32 %93 to i8
%95 = load i64, i64* %5
%96 = add i64 %95, 2
%97 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %96
store i8 %94, i8* %97
%98 = load i16, i16* %3
%99 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%100 = load i64, i64* %5
%101 = add i64 %100, 2
%102 = call zeroext i16 @icrc(i16 zeroext %98, i8* %99, i64 %101, i16 signext 0, i32 -1)
store i16 %102, i16* %4
%103 = load i16, i16* %3
%104 = zext i16 %103 to i32
%105 = load i16, i16* %4
%106 = zext i16 %105 to i32
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %104, i32 %106)
br label %6
108: ; preds = %12
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"%lu %s\00"
@EOF = global i64 0
@.str.3 = [52 x i8] c" XMODEM: String CRC, Packet CRC= 0x%x 0x%x\0A\00"
@.str.4 = [52 x i8] c" X.25: String CRC, Packet CRC= 0x%x 0x%x\0A\00"
@.str.5 = [52 x i8] c" CRC-CCITT: String CRC, Packet CRC= 0x%x 0x%x\0A\00"
@str = [26 x i8] c"Enter length and string: \00"
@str.7 = [18 x i8] c"Normal completion\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast i64* %2 to i8*
%5 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 1
%7 = call i64 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64* %2, i8* %6)
%8 = load i64, i64* @EOF
%9 = icmp eq i64 %7, %8
br i1 %9, label %63, label %10
10: ; preds = %0, %10
%11 = load i64, i64* %2
%12 = add i64 %11, 1
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @puts(i8* %6)
%15 = load i64, i64* %2
%16 = call zeroext i16 @icrc(i16 zeroext 0, i8* %3, i64 %15, i16 signext 0, i32 1)
%17 = zext i16 %16 to i32
%18 = lshr i16 %16, 8
%19 = trunc i16 %18 to i8
%20 = load i64, i64* %2
%21 = add i64 %20, 1
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %21
store i8 %19, i8* %22
%23 = trunc i16 %16 to i8
%24 = add i64 %20, 2
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %24
store i8 %23, i8* %25
%26 = call zeroext i16 @icrc(i16 zeroext %16, i8* %3, i64 %24, i16 signext 0, i32 1)
%27 = zext i16 %26 to i32
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %27)
%29 = load i64, i64* %2
%30 = call zeroext i16 @icrc(i16 zeroext %26, i8* %3, i64 %29, i16 signext 255, i32 -1)
%31 = zext i16 %30 to i32
%32 = trunc i16 %30 to i8
%33 = xor i8 %32, -1
%34 = load i64, i64* %2
%35 = add i64 %34, 1
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %35
store i8 %33, i8* %36
%37 = lshr i16 %30, 8
%38 = trunc i16 %37 to i8
%39 = xor i8 %38, -1
%40 = add i64 %34, 2
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %40
store i8 %39, i8* %41
%42 = call zeroext i16 @icrc(i16 zeroext %30, i8* %3, i64 %40, i16 signext 255, i32 -1)
%43 = zext i16 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %31, i32 %43)
%45 = load i64, i64* %2
%46 = call zeroext i16 @icrc(i16 zeroext %42, i8* %3, i64 %45, i16 signext 0, i32 -1)
%47 = zext i16 %46 to i32
%48 = trunc i16 %46 to i8
%49 = load i64, i64* %2
%50 = add i64 %49, 1
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %50
store i8 %48, i8* %51
%52 = lshr i16 %46, 8
%53 = trunc i16 %52 to i8
%54 = add i64 %49, 2
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %54
store i8 %53, i8* %55
%56 = call zeroext i16 @icrc(i16 zeroext %46, i8* %3, i64 %54, i16 signext 0, i32 -1)
%57 = zext i16 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %47, i32 %57)
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%60 = call i64 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64* %2, i8* %6)
%61 = load i64, i64* @EOF
%62 = icmp eq i64 %60, %61
br i1 %62, label %63, label %10
63: ; preds = %10, %0
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}