#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 char uint8_t;
typedef int uint16_t;

/* Variables and functions */
int crc_calculate_algorithm(char *, size_t);
int crc_calculate_table(char *, size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
size_t strlen(char *);
int main(void) {
  int i;
  size_t datalen;
  uint16_t crc_algorithm;
  uint16_t crc_table;
  uint16_t crc_check_algorithm;
  uint16_t crc_check_table;
  char *testdata[] = {"", "A", "123456789",
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                      NULL};
  uint8_t temp_buffer[300];
  for (i = 0; testdata[i]; i++) {
    printf("Data: '%s'\n", testdata[i]);
    datalen = strlen(testdata[i]);
    crc_algorithm = crc_calculate_algorithm(testdata[i], datalen);
    printf("Algorithm-based CRC: 0x%04hX\n", crc_algorithm);
    crc_table = crc_calculate_table(testdata[i], datalen);
    printf("Table-based CRC:     0x%04hX\n", crc_table);
    memcpy(temp_buffer, testdata[i], datalen);
    temp_buffer[datalen] = crc_algorithm >> 8;
    temp_buffer[datalen + 1] = crc_algorithm & 0xFF;
    crc_check_algorithm = crc_calculate_algorithm(temp_buffer, datalen + 2);
    printf("Algorithm-based CRC check: 0x%04hX\n", crc_check_algorithm);
    temp_buffer[datalen] = crc_table >> 8;
    temp_buffer[datalen + 1] = crc_table & 0xFF;
    crc_check_table = crc_calculate_table(temp_buffer, datalen + 2);
    printf("Table-based CRC check:     0x%04hX\n", crc_check_table);
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    0xf8d(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xf75(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe61(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe60(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x8(%rbp)
movslq -0x8(%rbp),%rax
cmpq   $0x0,-0x50(%rbp,%rax,8)
je     130f <main+0x1af>
movslq -0x8(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rsi
lea    0xf36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x8(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
movslq -0x8(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <crc_calculate_algorithm@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xf0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x8(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <crc_calculate_table@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xf02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x180(%rbp),%rdi
movslq -0x8(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rsi
mov    -0x10(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,-0x180(%rbp,%rax,1)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,-0x17f(%rbp,%rax,1)
lea    -0x180(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   1060 <crc_calculate_algorithm@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,-0x180(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,-0x17f(%rbp,%rax,1)
lea    -0x180(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   1080 <crc_calculate_table@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b5 <main+0x55>
xor    %eax,%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x168,%rsp
movq   $0x0,0x20(%rsp)
lea    0xf95(%rip),%r14
mov    %r14,(%rsp)
lea    0x8(%rsp),%rbx
lea    0xf78(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xe63(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe61(%rip),%rax
mov    %rax,0x18(%rsp)
xchg   %ax,%ax
lea    0xf54(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <crc_calculate_algorithm@plt>
mov    %eax,%r13d
lea    0xf36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <crc_calculate_table@plt>
mov    %eax,%r12d
lea    0xf36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x30(%rsp),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <memcpy@plt>
rol    $0x8,%r13w
mov    %r13w,0x30(%rsp,%r15,1)
lea    0x2(%r15),%rbp
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <crc_calculate_algorithm@plt>
lea    0xf15(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
rol    $0x8,%r12w
mov    %r12w,0x30(%rsp,%r15,1)
mov    %r14,%rdi
mov    %rbp,%rsi
call   1080 <crc_calculate_table@plt>
lea    0xf13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%rbx),%r14
add    $0x8,%rbx
test   %r14,%r14
jne    11b0 <main+0x50>
xor    %eax,%eax
add    $0x168,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"A\00"
@.str.2 = [10 x i8] c"123456789\00"
@.str.3 = [257 x i8] c"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\00"
@.str.4 = [12 x i8] c"Data: '%s'\0A\00"
@.str.5 = [30 x i8] c"Algorithm-based CRC: 0x%04hX\0A\00"
@.str.6 = [30 x i8] c"Table-based CRC: 0x%04hX\0A\00"
@.str.7 = [36 x i8] c"Algorithm-based CRC check: 0x%04hX\0A\00"
@.str.8 = [36 x i8] c"Table-based CRC check: 0x%04hX\0A\00"
@.str.9 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [5 x i8*]
%9 = alloca [300 x i8]
store i32 0, i32* %1
%10 = bitcast [5 x i8*]* %8 to i8*
%11 = bitcast i8* %10 to [5 x i8*]*
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 0
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 3
store i8* getelementptr inbounds ([257 x i8], [257 x i8]* @.str.3, i32 0, i32 0), i8** %15
store i32 0, i32* %2
br label %16
16: ; preds = %91, %0
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 %18
%20 = load i8*, i8** %19
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %94
22: ; preds = %16
%23 = load i32, i32* %2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 %24
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 %29
%31 = load i8*, i8** %30
%32 = call i64 @strlen(i8* %31)
store i64 %32, i64* %3
%33 = load i32, i32* %2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = load i64, i64* %3
%38 = call i32 @crc_calculate_algorithm(i8* %36, i64 %37)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 %42
%44 = load i8*, i8** %43
%45 = load i64, i64* %3
%46 = call i32 @crc_calculate_table(i8* %44, i64 %45)
store i32 %46, i32* %5
%47 = load i32, i32* %5
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %47)
%49 = getelementptr inbounds [300 x i8], [300 x i8]* %9, i64 0, i64 0
%50 = load i32, i32* %2
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i64 0, i64 %51
%53 = load i8*, i8** %52
%54 = load i64, i64* %3
%55 = call i32 @memcpy(i8* %49, i8* %53, i64 %54)
%56 = load i32, i32* %4
%57 = ashr i32 %56, 8
%58 = trunc i32 %57 to i8
%59 = load i64, i64* %3
%60 = getelementptr inbounds [300 x i8], [300 x i8]* %9, i64 0, i64 %59
store i8 %58, i8* %60
%61 = load i32, i32* %4
%62 = and i32 %61, 255
%63 = trunc i32 %62 to i8
%64 = load i64, i64* %3
%65 = add i64 %64, 1
%66 = getelementptr inbounds [300 x i8], [300 x i8]* %9, i64 0, i64 %65
store i8 %63, i8* %66
%67 = getelementptr inbounds [300 x i8], [300 x i8]* %9, i64 0, i64 0
%68 = load i64, i64* %3
%69 = add i64 %68, 2
%70 = call i32 @crc_calculate_algorithm(i8* %67, i64 %69)
store i32 %70, i32* %6
%71 = load i32, i32* %6
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %5
%74 = ashr i32 %73, 8
%75 = trunc i32 %74 to i8
%76 = load i64, i64* %3
%77 = getelementptr inbounds [300 x i8], [300 x i8]* %9, i64 0, i64 %76
store i8 %75, i8* %77
%78 = load i32, i32* %5
%79 = and i32 %78, 255
%80 = trunc i32 %79 to i8
%81 = load i64, i64* %3
%82 = add i64 %81, 1
%83 = getelementptr inbounds [300 x i8], [300 x i8]* %9, i64 0, i64 %82
store i8 %80, i8* %83
%84 = getelementptr inbounds [300 x i8], [300 x i8]* %9, i64 0, i64 0
%85 = load i64, i64* %3
%86 = add i64 %85, 2
%87 = call i32 @crc_calculate_table(i8* %84, i64 %86)
store i32 %87, i32* %7
%88 = load i32, i32* %7
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i32 %88)
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
br label %91
91: ; preds = %22
%92 = load i32, i32* %2
%93 = add nsw i32 %92, 1
store i32 %93, i32* %2
br label %16
94: ; preds = %16
ret i32 0
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"A\00"
@.str.2 = [10 x i8] c"123456789\00"
@.str.3 = [257 x i8] c"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\00"
@.str.4 = [12 x i8] c"Data: '%s'\0A\00"
@.str.5 = [30 x i8] c"Algorithm-based CRC: 0x%04hX\0A\00"
@.str.6 = [30 x i8] c"Table-based CRC: 0x%04hX\0A\00"
@.str.7 = [36 x i8] c"Algorithm-based CRC check: 0x%04hX\0A\00"
@.str.8 = [36 x i8] c"Table-based CRC check: 0x%04hX\0A\00"
define i32 @main() {
%1 = alloca [5 x i8*]
%2 = alloca [300 x i8]
%3 = bitcast [5 x i8*]* %1 to i8*
%4 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 4
%5 = bitcast i8** %4 to i64*
store i64 0, i64* %5
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 0
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 3
store i8* getelementptr inbounds ([257 x i8], [257 x i8]* @.str.3, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 0
br label %11
11: ; preds = %0, %11
%12 = phi i64 [ 0, %0 ], [ %36, %11 ]
%13 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %0 ], [ %38, %11 ]
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%15 = call i64 @strlen(i8* dereferenceable(1) %13)
%16 = call i32 @crc_calculate_algorithm(i8* %13, i64 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%18 = call i32 @crc_calculate_table(i8* %13, i64 %15)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %18)
%20 = call i32 @memcpy(i8* %10, i8* %13, i64 %15)
%21 = lshr i32 %16, 8
%22 = trunc i32 %21 to i8
%23 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 %15
store i8 %22, i8* %23
%24 = trunc i32 %16 to i8
%25 = add i64 %15, 1
%26 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 %25
store i8 %24, i8* %26
%27 = add i64 %15, 2
%28 = call i32 @crc_calculate_algorithm(i8* %10, i64 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %28)
%30 = lshr i32 %18, 8
%31 = trunc i32 %30 to i8
store i8 %31, i8* %23
%32 = trunc i32 %18 to i8
store i8 %32, i8* %26
%33 = call i32 @crc_calculate_table(i8* %10, i64 %27)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i32 %33)
%35 = call i32 @putchar(i32 10)
%36 = add nuw i64 %12, 1
%37 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 %36
%38 = load i8*, i8** %37
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %11
40: ; preds = %11
ret i32 0
}