// File: /workspace/github_c_makefile_star2_O3/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 %rcx, %r8
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movslq 0x14(%rdi), %rcx
testq %rcx, %rcx
jle 0x12d2
incq %rcx
xorl %eax, %eax
shlq $0x8, %rax
movzbl -0x2(%r8,%rcx), %esi
xorq $0xff, %rsi
orq %rsi, %rax
decq %rcx
cmpq $0x1, %rcx
ja 0x12b3
jmp 0x12d4
xorl %eax, %eax
movq 0x8(%rsp), %rdi
movl 0x10(%rdi), %ecx
movq $-0x1, %rsi
movl %ecx, (%rsp)
shlq %cl, %rsi
leal (,%rdx,8), %ecx
movl %ecx, 0x4(%rsp)
movslq (%rdi), %rdi
movq %rdi, 0x10(%rsp)
testq %rdi, %rdi
movq %r8, 0x98(%rsp)
jle 0x15d2
notq %rsi
andq %rsi, %rax
movw $0x2, %r8w
xorl %ecx, %ecx
leaq 0xd4f(%rip), %r13       # 0x2070
leaq 0x4d48(%rip), %rbx      # 0x6070
xorl %edi, %edi
movzwl %r8w, %r10d
testq %rdx, %rdx
je 0x1399
movzwl (%r13,%r10,2), %r11d
movw $0x1, %r9w
xorl %r14d, %r14d
xorl %r8d, %r8d
movb (%r15,%r14), %bpl
notb %bpl
movl $0x8, %r12d
testb $0x1, %bpl
movzwl %r9w, %esi
cmovel %ecx, %r9d
xorl %r9d, %r8d
shrb %bpl
movzwl (%r13,%rsi,2), %esi
addw %r11w, %si
movzwl %si, %esi
leal -0x1fff(%rsi), %r9d
cmpl $0x1fff, %esi           # imm = 0x1FFF
cmovbl %esi, %r9d
movzwl %r9w, %esi
movzwl (%rbx,%rsi,2), %r9d
decl %r12d
jne 0x1351
incq %r14
cmpq %rdx, %r14
jne 0x1344
jmp 0x13a1
movw $0x1, %r9w
xorl %r8d, %r8d
movzwl (%r13,%r10,2), %r10d
movl (%rsp), %esi
movl %esi, %r11d
movq %rax, %r14
testl %esi, %esi
jle 0x13f2
testb $0x1, %r14b
movzwl %r9w, %esi
cmovel %ecx, %r9d
xorl %r9d, %r8d
shrq %r14
movzwl (%r13,%rsi,2), %esi
addw %r10w, %si
movzwl %si, %esi
leal -0x1fff(%rsi), %r9d
cmpl $0x1fff, %esi           # imm = 0x1FFF
cmovbl %esi, %r9d
movzwl %r9w, %esi
movzwl (%rbx,%rsi,2), %r9d
decl %r11d
jne 0x13b4
movw %r8w, 0xa0(%rsp,%rdi,2)
leal 0x1(%r10), %esi
movl %esi, %r8d
movl $0xffff, %r9d           # imm = 0xFFFF
andl %r9d, %r8d
addl $0xffffe002, %r10d      # imm = 0xFFFFE002
cmpl $0x1fff, %r8d           # imm = 0x1FFF
cmovbl %esi, %r10d
andl %r9d, %r10d
movzwl (%rbx,%r10,2), %r8d
incq %rdi
cmpq 0x10(%rsp), %rdi
jne 0x132a
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movw $0x1, %r10w
movw %r10w, 0x50(%rsp)
movw %r10w, 0x20(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x15ff
movl $0x1, %ecx
xorl %r12d, %r12d
leaq 0xa0(%rsp), %rbp
movl $0xffff, %r8d           # imm = 0xFFFF
xorl %r11d, %r11d
movzwl 0xa0(%rsp,%r12,2), %r14d
testl %r11d, %r11d
jle 0x14ed
leal 0x1(%r11), %eax
negq %rax
movq $-0x1, %r9
leaq 0x22(%rsp), %rdx
movzwl (%rdx), %esi
testq %rsi, %rsi
je 0x14e1
movzwl (%rbp,%r9,2), %edi
testq %rdi, %rdi
je 0x14e1
movzwl (%r13,%rdi,2), %edi
addw (%r13,%rsi,2), %di
movzwl %di, %esi
leal -0x1fff(%rsi), %edi
cmpl $0x1fff, %esi           # imm = 0x1FFF
cmovbl %esi, %edi
movzwl %di, %esi
xorw (%rbx,%rsi,2), %r14w
decq %r9
addq $0x2, %rdx
cmpq %r9, %rax
jne 0x14a8
movzwl %r14w, %r9d
movzwl (%r13,%r9,2), %edx
movzwl %r10w, %eax
subw (%r13,%rax,2), %dx
movl $0x1fff, %edi           # imm = 0x1FFF
leal (%rdx,%rdi), %eax
movl %eax, %esi
andl %r8d, %esi
cmpl %edi, %esi
cmovael %edx, %eax
testw %r9w, %r9w
je 0x155d
andl %r8d, %eax
leal (%r11,%r11), %edx
movslq %edx, %rsi
movzwl (%rbx,%rax,2), %edx
cmpq %rsi, %r12
movq %r11, 0x48(%rsp)
jge 0x1561
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl %ecx, 0x1c(%rsp)
movl %r10d, %r14d
callq 0x17ca
movl %r14d, %r10d
movl 0x1c(%rsp), %ecx
incl %ecx
movl $0xffff, %r8d           # imm = 0xFFFF
movq 0x48(%rsp), %r11
jmp 0x15be
incl %ecx
jmp 0x15be
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movaps %xmm1, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x17ca
movaps 0xb0(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movaps %xmm1, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl %r12d, %eax
subl 0x48(%rsp), %eax
incl %eax
movl $0x1, %ecx
movl %r14d, %r10d
movl %eax, %r11d
movl $0xffff, %r8d           # imm = 0xFFFF
incq %r12
addq $0x2, %rbp
cmpq 0x10(%rsp), %r12
jne 0x1487
jmp 0x15ff
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movw $0x1, %ax
movw %ax, 0x50(%rsp)
movw %ax, 0x20(%rsp)
movzwl 0x20(%rsp), %eax
movw %ax, 0x70(%rsp)
movq 0x22(%rsp), %rax
movq %rax, 0x72(%rsp)
movl 0x2a(%rsp), %eax
movl %eax, 0x7a(%rsp)
movzwl 0x2e(%rsp), %eax
movw %ax, 0x7e(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x16a5
movw $0x1, %ax
xorl %ebx, %ebx
leaq 0xa37(%rip), %r13       # 0x2070
movl $0xffff, %ebp           # imm = 0xFFFF
leaq 0x4a2b(%rip), %r14      # 0x6070
movzwl %ax, %r12d
leaq 0x70(%rsp), %rdi
movl %r12d, %esi
callq 0x173d
testw %ax, %ax
jne 0x1670
movl %ebx, %ecx
andb $0x7, %cl
movl $0x1, %eax
shll %cl, %eax
movl %ebx, %ecx
shrl $0x3, %ecx
xorb %al, (%r15,%rcx)
movzwl (%r13,%r12,2), %eax
leal 0x1ffe(%rax), %ecx
movl %ecx, %edx
andl %ebp, %edx
decl %eax
cmpl $0x1fff, %edx           # imm = 0x1FFF
cmovbl %ecx, %eax
andl %ebp, %eax
movzwl (%r14,%rax,2), %eax
incl %ebx
cmpl %ebx, 0x4(%rsp)
jne 0x1645
movq 0x8(%rsp), %rcx
movl 0x10(%rcx), %r14d
jmp 0x16ad
movw $0x1, %ax
movl (%rsp), %r14d
testl %r14d, %r14d
jle 0x172b
xorl %ebx, %ebx
leaq 0x9b5(%rip), %r12       # 0x2070
movl $0xffff, %ebp           # imm = 0xFFFF
leaq 0x49a9(%rip), %r13      # 0x6070
movzwl %ax, %r15d
leaq 0x70(%rsp), %rdi
movl %r15d, %esi
callq 0x173d
testw %ax, %ax
jne 0x1702
movl %ebx, %ecx
andb $0x7, %cl
movl $0x1, %eax
shll %cl, %eax
movl %ebx, %ecx
shrl $0x3, %ecx
movq 0x98(%rsp), %rdx
xorb %al, (%rdx,%rcx)
movq 0x8(%rsp), %rax
movl 0x10(%rax), %r14d
movzwl (%r12,%r15,2), %eax
leal 0x1ffe(%rax), %ecx
movl %ecx, %edx
andl %ebp, %edx
decl %eax
cmpl $0x1fff, %edx           # imm = 0x1FFF
cmovbl %ecx, %eax
andl %ebp, %eax
movzwl (%r13,%rax,2), %eax
incl %ebx
cmpl %r14d, %ebx
jl 0x16c7
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq