#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 berlekamp(int, unsigned int *, unsigned int *);
int chien(int, int, unsigned int *, unsigned int *);
int /*<<< orphan*/ syndrome(int, unsigned char *, unsigned int *);
int decode_bch(int select_4_8, unsigned char *ecc, unsigned int *err_loc) {
  int no_of_err;
  unsigned int syn[16] = {
      0,
  };
  unsigned int err_poly[8] = {
      0,
  };
  syndrome(select_4_8, ecc, syn);
  no_of_err = berlekamp(select_4_8, syn, err_poly);
  if (no_of_err <= (4 << select_4_8))
    no_of_err = chien(select_4_8, no_of_err, err_poly, err_loc);
  return no_of_err;
}


x86 assembly (clang, -O0):
<decode_bch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1060 <syndrome@plt>
mov    -0x4(%rbp),%edi
lea    -0x60(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1030 <berlekamp@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x4(%rbp),%ecx
mov    $0x4,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jg     11c6 <decode_bch+0x86>
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%esi
lea    -0x80(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1050 <chien@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_bch>:
push   %rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %rdx,%r14
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdx
call   1050 <syndrome@plt>
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <berlekamp@plt>
mov    $0x4,%edx
mov    %ebp,%ecx
shl    %cl,%edx
cmp    %edx,%eax
jg     1193 <decode_bch+0x63>
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %eax,%esi
mov    %r14,%rcx
call   1040 <chien@plt>
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @decode_bch(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [16 x i32]
%9 = alloca [8 x i32]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%10 = bitcast [16 x i32]* %8 to i8*
%11 = bitcast [8 x i32]* %9 to i8*
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = getelementptr inbounds [16 x i32], [16 x i32]* %8, i64 0, i64 0
%15 = call i32 @syndrome(i32 %12, i8* %13, i32* %14)
%16 = load i32, i32* %4
%17 = getelementptr inbounds [16 x i32], [16 x i32]* %8, i64 0, i64 0
%18 = getelementptr inbounds [8 x i32], [8 x i32]* %9, i64 0, i64 0
%19 = call i32 @berlekamp(i32 %16, i32* %17, i32* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = load i32, i32* %4
%22 = shl i32 4, %21
%23 = icmp sle i32 %20, %22
br i1 %23, label %24, label %30
24: ; preds = %3
%25 = load i32, i32* %4
%26 = load i32, i32* %7
%27 = getelementptr inbounds [8 x i32], [8 x i32]* %9, i64 0, i64 0
%28 = load i32*, i32** %6
%29 = call i32 @chien(i32 %25, i32 %26, i32* %27, i32* %28)
store i32 %29, i32* %7
br label %30
30: ; preds = %24, %3
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
define i32 @decode_bch(i32 %0, i8* %1, i32* %2) {
%4 = alloca [16 x i32]
%5 = alloca [8 x i32]
%6 = bitcast [16 x i32]* %4 to i8*
%7 = bitcast [8 x i32]* %5 to i8*
%8 = getelementptr inbounds [16 x i32], [16 x i32]* %4, i64 0, i64 0
%9 = call i32 @syndrome(i32 %0, i8* %1, i32* %8)
%10 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%11 = call i32 @berlekamp(i32 %0, i32* %8, i32* %10)
%12 = shl i32 4, %0
%13 = icmp sgt i32 %11, %12
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call i32 @chien(i32 %0, i32 %11, i32* %10, i32* %2)
br label %16
16: ; preds = %14, %3
%17 = phi i32 [ %15, %14 ], [ %11, %3 ]
ret i32 %17
}