unsigned short crc16(char *buf, int len) {
  unsigned short crctab[] = {
      0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48,
      0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108,
      0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb,
      0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399,
      0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e,
      0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e,
      0x54b5, 0x453c, 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd,
      0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
      0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285,
      0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44,
      0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f, 0x4014,
      0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5,
      0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3,
      0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862,
      0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e,
      0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
      0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1,
      0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483,
      0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 0x2942, 0x38cb, 0x0a50,
      0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710,
      0xf3af, 0xe226, 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7,
      0x6e6e, 0x5cf5, 0x4d7c, 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1,
      0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72,
      0x3efb, 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
      0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 0xe70e,
      0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf,
      0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d,
      0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c,
      0x3de3, 0x2c6a, 0x1ef1, 0x0f78};
  int i;
  unsigned short crc = 0xffff;
  for (i = 0; i < len; i++)
    crc = crctab[(buf[i] ^ crc) & 0xff] ^ ((crc >> 8) & 0xff);
  return (~crc) & 0xffff;
}


x86 assembly (clang, -O0):
<crc16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x210(%rbp),%rdi
lea    0xed0(%rip),%rsi
mov    $0x200,%edx
call   1030 <memcpy@plt>
movw   $0xffff,-0x216(%rbp)
ff ff
movl   $0x0,-0x214(%rbp)
mov    -0x214(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b0 <crc16+0xa0>
mov    -0x8(%rbp),%rax
movslq -0x214(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movzwl -0x216(%rbp),%ecx
xor    %ecx,%eax
and    $0xff,%eax
cltq
movzwl -0x210(%rbp,%rax,2),%eax
ff
movzwl -0x216(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
xor    %ecx,%eax
mov    %ax,-0x216(%rbp)
mov    -0x214(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x214(%rbp)
jmp    114d <crc16+0x3d>
movzwl -0x216(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xffff,%eax
movzwl %ax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crc16>:
test   %esi,%esi
jle    1115 <crc16+0x15>
mov    %esi,%r8d
cmp    $0x1,%esi
jne    1118 <crc16+0x18>
mov    $0xffff,%eax
xor    %edx,%edx
jmp    1164 <crc16+0x64>
xor    %eax,%eax
ret
mov    %r8d,%r9d
and    $0xfffffffe,%r9d
mov    $0xffff,%esi
xor    %edx,%edx
lea    0xed3(%rip),%r10
nopl   (%rax)
movzbl (%rdi,%rdx,1),%eax
movzbl %sil,%ecx
xor    %eax,%ecx
movzwl (%r10,%rcx,2),%eax
shr    $0x8,%esi
xor    %eax,%esi
movzbl 0x1(%rdi,%rdx,1),%ecx
movzbl %sil,%esi
xor    %ecx,%esi
movzwl (%r10,%rsi,2),%ecx
shr    $0x8,%eax
xor    %ecx,%eax
add    $0x2,%rdx
mov    %eax,%esi
cmp    %rdx,%r9
jne    1130 <crc16+0x30>
test   $0x1,%r8b
je     1183 <crc16+0x83>
movzbl (%rdi,%rdx,1),%ecx
movzbl %al,%edx
xor    %ecx,%edx
lea    0xe86(%rip),%rcx
movzwl (%rcx,%rdx,2),%ecx
shr    $0x8,%eax
xor    %ecx,%eax
not    %eax
ret

LLVM IR (-O0):
@__const.crc16.crctab = [256 x i16] [i16 0, i16 4489, i16 8978, i16 12955, i16 17956, i16 22445, i16 25910, i16 29887, i16 -29624, i16 -25151, i16 -20646, i16 -16685, i16 -13716, i16 -9243, i16 -5762, i16 -1801, i16 4225, i16 264, i16 13203, i16 8730, i16 22181, i16 18220, i16 30135, i16 25662, i16 -25399, i16 -29376, i16 -16421, i16 -20910, i16 -9491, i16 -13468, i16 -1537, i16 -6026, i16 8450, i16 12427, i16 528, i16 5017, i16 26406, i16 30383, i16 17460, i16 21949, i16 -21174, i16 -17213, i16 -29096, i16 -24623, i16 -5266, i16 -1305, i16 -14212, i16 -9739, i16 12675, i16 8202, i16 4753, i16 792, i16 30631, i16 26158, i16 21685, i16 17724, i16 -16949, i16 -21438, i16 -24871, i16 -28848, i16 -1041, i16 -5530, i16 -9987, i16 -13964, i16 16900, i16 21389, i16 24854, i16 28831, i16 1056, i16 5545, i16 10034, i16 14011, i16 -12724, i16 -8251, i16 -4770, i16 -809, i16 -30616, i16 -26143, i16 -21638, i16 -17677, i16 21125, i16 17164, i16 29079, i16 24606, i16 5281, i16 1320, i16 14259, i16 9786, i16 -8499, i16 -12476, i16 -545, i16 -5034, i16 -26391, i16 -30368, i16 -17413, i16 -21902, i16 25350, i16 29327, i16 16404, i16 20893, i16 9506, i16 13483, i16 1584, i16 6073, i16 -4274, i16 -313, i16 -13220, i16 -8747, i16 -22166, i16 -18205, i16 -30088, i16 -25615, i16 29575, i16 25102, i16 20629, i16 16668, i16 13731, i16 9258, i16 5809, i16 1848, i16 -49, i16 -4538, i16 -8995, i16 -12972, i16 -17941, i16 -22430, i16 -25863, i16 -29840, i16 -31736, i16 -27263, i16 -22758, i16 -18797, i16 -15828, i16 -11355, i16 -7874, i16 -3913, i16 2112, i16 6601, i16 11090, i16 15067, i16 20068, i16 24557, i16 28022, i16 31999, i16 -27511, i16 -31488, i16 -18533, i16 -23022, i16 -11603, i16 -15580, i16 -3649, i16 -8138, i16 6337, i16 2376, i16 15315, i16 10842, i16 24293, i16 20332, i16 32247, i16 27774, i16 -23286, i16 -19325, i16 -31208, i16 -26735, i16 -7378, i16 -3417, i16 -16324, i16 -11851, i16 10562, i16 14539, i16 2640, i16 7129, i16 28518, i16 32495, i16 19572, i16 24061, i16 -19061, i16 -23550, i16 -26983, i16 -30960, i16 -3153, i16 -7642, i16 -12099, i16 -16076, i16 14787, i16 10314, i16 6865, i16 2904, i16 32743, i16 28270, i16 23797, i16 19836, i16 -14836, i16 -10363, i16 -6882, i16 -2921, i16 -32728, i16 -28255, i16 -23750, i16 -19789, i16 19012, i16 23501, i16 26966, i16 30943, i16 3168, i16 7657, i16 12146, i16 16123, i16 -10611, i16 -14588, i16 -2657, i16 -7146, i16 -28503, i16 -32480, i16 -19525, i16 -24014, i16 23237, i16 19276, i16 31191, i16 26718, i16 7393, i16 3432, i16 16371, i16 11898, i16 -6386, i16 -2425, i16 -15332, i16 -10859, i16 -24278, i16 -20317, i16 -32200, i16 -27727, i16 27462, i16 31439, i16 18516, i16 23005, i16 11618, i16 15595, i16 3696, i16 8185, i16 -2161, i16 -6650, i16 -11107, i16 -15084, i16 -20053, i16 -24542, i16 -27975, i16 -31952, i16 31687, i16 27214, i16 22741, i16 18780, i16 15843, i16 11370, i16 7921, i16 3960]
define zeroext i16 @crc16(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [256 x i16]
%6 = alloca i32
%7 = alloca i16
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = bitcast [256 x i16]* %5 to i8*
store i16 -1, i16* %7
store i32 0, i32* %6
br label %9
9: ; preds = %34, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = load i16, i16* %7
%21 = zext i16 %20 to i32
%22 = xor i32 %19, %21
%23 = and i32 %22, 255
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [256 x i16], [256 x i16]* %5, i64 0, i64 %24
%26 = load i16, i16* %25
%27 = zext i16 %26 to i32
%28 = load i16, i16* %7
%29 = zext i16 %28 to i32
%30 = ashr i32 %29, 8
%31 = and i32 %30, 255
%32 = xor i32 %27, %31
%33 = trunc i32 %32 to i16
store i16 %33, i16* %7
br label %34
34: ; preds = %13
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %9
37: ; preds = %9
%38 = load i16, i16* %7
%39 = zext i16 %38 to i32
%40 = xor i32 %39, -1
%41 = and i32 %40, 65535
%42 = trunc i32 %41 to i16
ret i16 %42
}

LLVM IR (-O2):
@__const.crc16.crctab = [256 x i16] [i16 0, i16 4489, i16 8978, i16 12955, i16 17956, i16 22445, i16 25910, i16 29887, i16 -29624, i16 -25151, i16 -20646, i16 -16685, i16 -13716, i16 -9243, i16 -5762, i16 -1801, i16 4225, i16 264, i16 13203, i16 8730, i16 22181, i16 18220, i16 30135, i16 25662, i16 -25399, i16 -29376, i16 -16421, i16 -20910, i16 -9491, i16 -13468, i16 -1537, i16 -6026, i16 8450, i16 12427, i16 528, i16 5017, i16 26406, i16 30383, i16 17460, i16 21949, i16 -21174, i16 -17213, i16 -29096, i16 -24623, i16 -5266, i16 -1305, i16 -14212, i16 -9739, i16 12675, i16 8202, i16 4753, i16 792, i16 30631, i16 26158, i16 21685, i16 17724, i16 -16949, i16 -21438, i16 -24871, i16 -28848, i16 -1041, i16 -5530, i16 -9987, i16 -13964, i16 16900, i16 21389, i16 24854, i16 28831, i16 1056, i16 5545, i16 10034, i16 14011, i16 -12724, i16 -8251, i16 -4770, i16 -809, i16 -30616, i16 -26143, i16 -21638, i16 -17677, i16 21125, i16 17164, i16 29079, i16 24606, i16 5281, i16 1320, i16 14259, i16 9786, i16 -8499, i16 -12476, i16 -545, i16 -5034, i16 -26391, i16 -30368, i16 -17413, i16 -21902, i16 25350, i16 29327, i16 16404, i16 20893, i16 9506, i16 13483, i16 1584, i16 6073, i16 -4274, i16 -313, i16 -13220, i16 -8747, i16 -22166, i16 -18205, i16 -30088, i16 -25615, i16 29575, i16 25102, i16 20629, i16 16668, i16 13731, i16 9258, i16 5809, i16 1848, i16 -49, i16 -4538, i16 -8995, i16 -12972, i16 -17941, i16 -22430, i16 -25863, i16 -29840, i16 -31736, i16 -27263, i16 -22758, i16 -18797, i16 -15828, i16 -11355, i16 -7874, i16 -3913, i16 2112, i16 6601, i16 11090, i16 15067, i16 20068, i16 24557, i16 28022, i16 31999, i16 -27511, i16 -31488, i16 -18533, i16 -23022, i16 -11603, i16 -15580, i16 -3649, i16 -8138, i16 6337, i16 2376, i16 15315, i16 10842, i16 24293, i16 20332, i16 32247, i16 27774, i16 -23286, i16 -19325, i16 -31208, i16 -26735, i16 -7378, i16 -3417, i16 -16324, i16 -11851, i16 10562, i16 14539, i16 2640, i16 7129, i16 28518, i16 32495, i16 19572, i16 24061, i16 -19061, i16 -23550, i16 -26983, i16 -30960, i16 -3153, i16 -7642, i16 -12099, i16 -16076, i16 14787, i16 10314, i16 6865, i16 2904, i16 32743, i16 28270, i16 23797, i16 19836, i16 -14836, i16 -10363, i16 -6882, i16 -2921, i16 -32728, i16 -28255, i16 -23750, i16 -19789, i16 19012, i16 23501, i16 26966, i16 30943, i16 3168, i16 7657, i16 12146, i16 16123, i16 -10611, i16 -14588, i16 -2657, i16 -7146, i16 -28503, i16 -32480, i16 -19525, i16 -24014, i16 23237, i16 19276, i16 31191, i16 26718, i16 7393, i16 3432, i16 16371, i16 11898, i16 -6386, i16 -2425, i16 -15332, i16 -10859, i16 -24278, i16 -20317, i16 -32200, i16 -27727, i16 27462, i16 31439, i16 18516, i16 23005, i16 11618, i16 15595, i16 3696, i16 8185, i16 -2161, i16 -6650, i16 -11107, i16 -15084, i16 -20053, i16 -24542, i16 -27975, i16 -31952, i16 31687, i16 27214, i16 22741, i16 18780, i16 15843, i16 11370, i16 7921, i16 3960]
define zeroext i16 @crc16(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %62
4: ; preds = %2
%5 = zext i32 %1 to i64
%6 = and i64 %5, 1
%7 = icmp eq i32 %1, 1
br i1 %7, label %40, label %8
8: ; preds = %4
%9 = and i64 %5, 4294967294
br label %10
10: ; preds = %10, %8
%11 = phi i64 [ 0, %8 ], [ %37, %10 ]
%12 = phi i32 [ 65535, %8 ], [ %36, %10 ]
%13 = phi i64 [ 0, %8 ], [ %38, %10 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %11
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = and i32 %12, 255
%18 = xor i32 %17, %16
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [256 x i16], [256 x i16]* @__const.crc16.crctab, i64 0, i64 %19
%21 = load i16, i16* %20
%22 = zext i16 %21 to i32
%23 = lshr i32 %12, 8
%24 = xor i32 %23, %22
%25 = or i64 %11, 1
%26 = getelementptr inbounds i8, i8* %0, i64 %25
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = and i32 %24, 255
%30 = xor i32 %29, %28
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [256 x i16], [256 x i16]* @__const.crc16.crctab, i64 0, i64 %31
%33 = load i16, i16* %32
%34 = zext i16 %33 to i32
%35 = lshr i32 %22, 8
%36 = xor i32 %35, %34
%37 = add nuw nsw i64 %11, 2
%38 = add i64 %13, 2
%39 = icmp eq i64 %38, %9
br i1 %39, label %40, label %10
40: ; preds = %10, %4
%41 = phi i32 [ undef, %4 ], [ %36, %10 ]
%42 = phi i64 [ 0, %4 ], [ %37, %10 ]
%43 = phi i32 [ 65535, %4 ], [ %36, %10 ]
%44 = icmp eq i64 %6, 0
br i1 %44, label %58, label %45
45: ; preds = %40
%46 = getelementptr inbounds i8, i8* %0, i64 %42
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = and i32 %43, 255
%50 = xor i32 %49, %48
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds [256 x i16], [256 x i16]* @__const.crc16.crctab, i64 0, i64 %51
%53 = load i16, i16* %52
%54 = zext i16 %53 to i32
%55 = lshr i32 %43, 8
%56 = and i32 %55, 255
%57 = xor i32 %56, %54
br label %58
58: ; preds = %40, %45
%59 = phi i32 [ %41, %40 ], [ %57, %45 ]
%60 = trunc i32 %59 to i16
%61 = xor i16 %60, -1
br label %62
62: ; preds = %58, %2
%63 = phi i16 [ 0, %2 ], [ %61, %58 ]
ret i16 %63
}