// File: /workspace/github_c_makefile_star2_O2/dlbeer[P]dhara/ecc/bch.c
// Function: bch_repair
void bch_repair(const struct bch_def *bch,
		uint8_t *chunk, size_t len, uint8_t *ecc)
{
	const bch_poly_t remainder = unpack_poly(bch, ecc);
	const int chunk_bits = len << 3;
	gf13_elem_t syns[BCH_MAX_SYNS];
	gf13_elem_t sigma[MAX_POLY];
	gf13_elem_t x;
	int i;

	/* Compute syndrome vector */
	x = 2;
	for (i = 0; i < bch->syns; i++) {
		syns[i] = syndrome(bch, chunk, len, remainder, x);
		x = gf13_mulx(x);
	}

	/* Compute sigma */
	berlekamp_massey(syns, bch->syns, sigma);

	/* Each root of sigma corresponds to an error location. Correct
	 * errors in the chunk data first.
	 */
	x = 1;
	for (i = 0; i < chunk_bits; i++) {
		if (!poly_eval(sigma, x))
			chunk[i >> 3] ^= 1 << (i & 7);
		x = gf13_divx(x);
	}

	/* Then correct errors in the ECC data */
	for (i = 0; i < bch->degree; i++) {
		if (!poly_eval(sigma, x))
			ecc[i >> 3] ^= 1 << (i & 7);
		x = gf13_divx(x);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
movq %rcx, 0x38(%rsp)
movq %rcx, %rsi
callq 0x1239
movq %rax, 0x8(%rsp)
movq %rbx, 0x18(%rsp)
movl (%rbx), %eax
xorl %r14d, %r14d
testl %eax, %eax
cmovlel %r14d, %eax
movq %rax, 0x20(%rsp)
movw $0x2, (%rsp)
xorl %edx, %edx
cmpq 0x20(%rsp), %rdx
je 0x138a
movq %rdx, 0x10(%rsp)
movw $0x1, %ax
xorl %r12d, %r12d
xorl %r13d, %r13d
movzwl (%rsp), %ebx
cmpq 0x30(%rsp), %r12
je 0x1313
movq 0x28(%rsp), %rcx
movb (%rcx,%r12), %bpl
notb %bpl
pushq $0x8
popq %r15
subl $0x1, %r15d
jb 0x130e
testb $0x1, %bpl
movl %eax, %ecx
cmovel %r14d, %ecx
xorl %ecx, %r13d
shrb %bpl
movzwl %ax, %edi
movl %ebx, %esi
callq 0x1657
jmp 0x12ec
incq %r12
jmp 0x12d1
movq 0x18(%rsp), %rcx
movl 0x10(%rcx), %r15d
testl %r15d, %r15d
cmovlel %r14d, %r15d
movq 0x8(%rsp), %r12
subl $0x1, %r15d
jb 0x134a
testb $0x1, %r12b
movl %eax, %ecx
cmovel %r14d, %ecx
xorl %ecx, %r13d
shrq %r12
movzwl %ax, %edi
movl %ebx, %esi
callq 0x1657
jmp 0x1328
leaq 0xd1f(%rip), %rax       # 0x2070
movzwl (%rax,%rbx,2), %eax
leal 0x1(%rax), %ecx
addl $0xffffe002, %eax       # imm = 0xFFFFE002
cmpw $0x1fff, %cx            # imm = 0x1FFF
cmovbl %ecx, %eax
movq 0x10(%rsp), %rdx
movw %r13w, 0x60(%rsp,%rdx,2)
movzwl %ax, %eax
leaq 0x4cf6(%rip), %rcx      # 0x6070
movzwl (%rcx,%rax,2), %eax
movw %ax, (%rsp)
incq %rdx
jmp 0x12b7
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
leaq 0x70(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
leaq 0x40(%rsp), %rcx
movaps %xmm0, (%rcx)
movaps %xmm0, 0x10(%rcx)
movw $0x1, %dx
movw %dx, (%rax)
movw $0x1, 0x8(%rsp)
movw %dx, (%rcx)
xorl %r13d, %r13d
pushq $0x1
popq %r15
leaq 0x5e(%rsp), %r14
xorl %r12d, %r12d
cmpq 0x20(%rsp), %r13
je 0x150c
movl %r15d, 0x10(%rsp)
movzwl 0x60(%rsp,%r13,2), %ebx
testl %r12d, %r12d
movl $0x0, %r15d
movq %r12, %rbp
cmovgl %r12d, %r15d
xorl %r12d, %r12d
movq %r14, (%rsp)
cmpq %r12, %r15
je 0x1429
movzwl 0x42(%rsp,%r12,2), %eax
testw %ax, %ax
je 0x1420
movzwl (%r14), %ecx
testw %cx, %cx
je 0x1420
movzwl %ax, %edi
movzwl %cx, %esi
callq 0x1657
xorl %eax, %ebx
addq $-0x2, %r14
incq %r12
jmp 0x13fa
movzwl %bx, %eax
leaq 0xc3d(%rip), %rdx       # 0x2070
movzwl (%rdx,%rax,2), %ecx
movzwl 0x8(%rsp), %eax
subw (%rdx,%rax,2), %cx
leal 0x1fff(%rcx), %eax
cmpw $0x1fff, %ax            # imm = 0x1FFF
cmovael %ecx, %eax
testw %bx, %bx
je 0x1492
movzwl %ax, %eax
movq %rbp, %r12
leal (,%rbp,2), %ecx
movslq %ecx, %rcx
leaq 0x4c07(%rip), %rdx      # 0x6070
movzwl (%rdx,%rax,2), %edx
cmpq %rcx, %r13
movq (%rsp), %r14
movl 0x10(%rsp), %r15d
jge 0x14a3
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %r15d, %ecx
callq 0x1687
incl %r15d
jmp 0x1500
movl 0x10(%rsp), %r15d
incl %r15d
movq (%rsp), %r14
movq %rbp, %r12
jmp 0x1500
movaps 0x40(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
movaps %xmm1, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl %r15d, %ecx
callq 0x1687
movaps 0x90(%rsp), %xmm0
movaps 0xa0(%rsp), %xmm1
movaps %xmm1, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movl %r13d, %eax
subl %r12d, %eax
incl %eax
pushq $0x1
popq %r15
movw %bx, 0x8(%rsp)
movl %eax, %r12d
incq %r13
addq $0x2, %r14
jmp 0x13cd
movq 0x30(%rsp), %rax
leal (,%rax,8), %r15d
movaps 0x40(%rsp), %xmm0
leaq 0xb0(%rsp), %rbx
movaps %xmm0, (%rbx)
xorl %r12d, %r12d
testl %r15d, %r15d
cmovlel %r12d, %r15d
movw $0x1, %ax
pushq $0x1
popq %r14
movq 0x38(%rsp), %r13
cmpl %r12d, %r15d
je 0x157c
movzwl %ax, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x15d8
testw %ax, %ax
jne 0x1570
movl %r12d, %ecx
andb $0x7, %cl
movl %r14d, %eax
shll %cl, %eax
movl %r12d, %ecx
shrl $0x3, %ecx
movq 0x28(%rsp), %rdx
xorb %al, (%rdx,%rcx)
movl %ebp, %edi
callq 0x162b
incl %r12d
jmp 0x1540
xorl %r15d, %r15d
leaq 0xb0(%rsp), %rbx
movq 0x18(%rsp), %rcx
cmpl 0x10(%rcx), %r15d
jge 0x15c6
movzwl %ax, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x15d8
testw %ax, %ax
jne 0x15ba
movl %r15d, %ecx
andb $0x7, %cl
movl %r14d, %eax
shll %cl, %eax
movl %r15d, %ecx
shrl $0x3, %ecx
xorb %al, (%r13,%rcx)
movl %ebp, %edi
callq 0x162b
incl %r15d
jmp 0x1587
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq