#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 */

/* Variables and functions */
int parity(int, int);
unsigned char decode_bch_codeword(unsigned int code, unsigned int *error) {
  int i, bit;
  int a1, a2, a3, a4;
  unsigned int input, output;
  input = code;
  for (i = 0, output = 0; i < 15; i++) {
    a1 = parity(0x080D & code, 15);
    a2 = parity(0x2203 & code, 15);
    a3 = parity(0x5101 & code, 15);
    a4 = parity(0x00D1 & code, 15);
    if ((a1 + a2 + a3 + a4) >= 3) {
      bit = (code ^ 1) & 1;
    } else {
      bit = (code ^ 0) & 1;
    }
    code >>= 1;
    code |= bit ? 0x4000 : 0;
    output >>= 1;
    output |= bit ? 0x4000 : 0;
  }
  *error = code ^ input;
  return (unsigned char)(output & 0x7F);
}


x86 assembly (clang, -O0):
<decode_bch_codeword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0xf,-0x14(%rbp)
jge    1213 <decode_bch_codeword+0x103>
mov    -0x4(%rbp),%edi
and    $0x80d,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
and    $0x2203,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
and    $0x5101,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
and    $0xd1,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
cmp    $0x3,%eax
jl     11bb <decode_bch_codeword+0xab>
mov    -0x4(%rbp),%eax
xor    $0x1,%eax
and    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c7 <decode_bch_codeword+0xb7>
mov    -0x4(%rbp),%eax
xor    $0x0,%eax
and    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%edx
xor    %eax,%eax
mov    $0x4000,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
or     -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x30(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edx
xor    %eax,%eax
mov    $0x4000,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1133 <decode_bch_codeword+0x23>
mov    -0x4(%rbp),%ecx
xor    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%eax
and    $0x7f,%eax
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_bch_codeword>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
xor    %r15d,%r15d
mov    $0xf,%r14d
mov    %edi,0xc(%rsp)
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
and    $0x80d,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
mov    %eax,%r12d
mov    %ebp,%edi
and    $0x2203,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
mov    %eax,%ebx
mov    %ebp,%edi
and    $0x5101,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
mov    %eax,%r13d
mov    %ebp,%edi
and    $0xd1,%edi
mov    $0xf,%esi
call   1030 <parity@plt>
add    %r12d,%ebx
add    %r13d,%ebx
add    %eax,%ebx
mov    %r15d,%eax
xor    %r15d,%r15d
cmp    $0x3,%ebx
setge  %r15b
xor    %ebp,%r15d
shr    %ebp
and    $0x1,%r15d
shl    $0xe,%r15d
or     %r15d,%ebp
shr    %eax
or     %eax,%r15d
add    $0xffffffff,%r14d
jne    1140 <decode_bch_codeword+0x30>
xor    0xc(%rsp),%ebp
mov    0x10(%rsp),%rcx
mov    %ebp,(%rcx)
and    $0x7f,%al
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @decode_bch_codeword(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%13 = load i32, i32* %3
store i32 %13, i32* %11
store i32 0, i32* %5
store i32 0, i32* %12
br label %14
14: ; preds = %63, %2
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 15
br i1 %16, label %17, label %66
17: ; preds = %14
%18 = load i32, i32* %3
%19 = and i32 2061, %18
%20 = call i32 @parity(i32 %19, i32 15)
store i32 %20, i32* %7
%21 = load i32, i32* %3
%22 = and i32 8707, %21
%23 = call i32 @parity(i32 %22, i32 15)
store i32 %23, i32* %8
%24 = load i32, i32* %3
%25 = and i32 20737, %24
%26 = call i32 @parity(i32 %25, i32 15)
store i32 %26, i32* %9
%27 = load i32, i32* %3
%28 = and i32 209, %27
%29 = call i32 @parity(i32 %28, i32 15)
store i32 %29, i32* %10
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = add nsw i32 %30, %31
%33 = load i32, i32* %9
%34 = add nsw i32 %32, %33
%35 = load i32, i32* %10
%36 = add nsw i32 %34, %35
%37 = icmp sge i32 %36, 3
br i1 %37, label %38, label %42
38: ; preds = %17
%39 = load i32, i32* %3
%40 = xor i32 %39, 1
%41 = and i32 %40, 1
store i32 %41, i32* %6
br label %46
42: ; preds = %17
%43 = load i32, i32* %3
%44 = xor i32 %43, 0
%45 = and i32 %44, 1
store i32 %45, i32* %6
br label %46
46: ; preds = %42, %38
%47 = load i32, i32* %3
%48 = lshr i32 %47, 1
store i32 %48, i32* %3
%49 = load i32, i32* %6
%50 = icmp ne i32 %49, 0
%51 = zext i1 %50 to i64
%52 = select i1 %50, i32 16384, i32 0
%53 = load i32, i32* %3
%54 = or i32 %53, %52
store i32 %54, i32* %3
%55 = load i32, i32* %12
%56 = lshr i32 %55, 1
store i32 %56, i32* %12
%57 = load i32, i32* %6
%58 = icmp ne i32 %57, 0
%59 = zext i1 %58 to i64
%60 = select i1 %58, i32 16384, i32 0
%61 = load i32, i32* %12
%62 = or i32 %61, %60
store i32 %62, i32* %12
br label %63
63: ; preds = %46
%64 = load i32, i32* %5
%65 = add nsw i32 %64, 1
store i32 %65, i32* %5
br label %14
66: ; preds = %14
%67 = load i32, i32* %3
%68 = load i32, i32* %11
%69 = xor i32 %67, %68
%70 = load i32*, i32** %4
store i32 %69, i32* %70
%71 = load i32, i32* %12
%72 = and i32 %71, 127
%73 = trunc i32 %72 to i8
ret i8 %73
}

LLVM IR (-O2):
define zeroext i8 @decode_bch_codeword(i32 %0, i32* nocapture writeonly %1) {
br label %3
3: ; preds = %2, %3
%4 = phi i32 [ 0, %2 ], [ %26, %3 ]
%5 = phi i32 [ %0, %2 ], [ %24, %3 ]
%6 = phi i32 [ 0, %2 ], [ %27, %3 ]
%7 = and i32 %5, 2061
%8 = tail call i32 @parity(i32 %7, i32 15)
%9 = and i32 %5, 8707
%10 = tail call i32 @parity(i32 %9, i32 15)
%11 = and i32 %5, 20737
%12 = tail call i32 @parity(i32 %11, i32 15)
%13 = and i32 %5, 209
%14 = tail call i32 @parity(i32 %13, i32 15)
%15 = add nsw i32 %10, %8
%16 = add nsw i32 %15, %12
%17 = add nsw i32 %16, %14
%18 = icmp sgt i32 %17, 2
%19 = zext i1 %18 to i32
%20 = xor i32 %5, %19
%21 = lshr i32 %5, 1
%22 = shl i32 %20, 14
%23 = and i32 %22, 16384
%24 = or i32 %23, %21
%25 = lshr i32 %4, 1
%26 = or i32 %23, %25
%27 = add nuw nsw i32 %6, 1
%28 = icmp eq i32 %27, 15
br i1 %28, label %29, label %3
29: ; preds = %3
%30 = xor i32 %24, %0
store i32 %30, i32* %1
%31 = trunc i32 %25 to i8
%32 = and i8 %31, 127
ret i8 %32
}