// File: /workspace/github_c_makefile_star2_O1/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 %rsi, %r15
movq %rdi, 0x10(%rsp)
movslq 0x14(%rdi), %rdi
testq %rdi, %rdi
jle 0x12ce
incq %rdi
xorl %eax, %eax
shlq $0x8, %rax
movzbl -0x2(%rcx,%rdi), %esi
xorq $0xff, %rsi
orq %rsi, %rax
decq %rdi
cmpq $0x1, %rdi
jg 0x12b0
jmp 0x12d0
xorl %eax, %eax
movq %rcx, 0x78(%rsp)
movq 0x10(%rsp), %rdi
movl 0x10(%rdi), %r13d
movq $-0x1, %rsi
movl %r13d, %ecx
shlq %cl, %rsi
movslq (%rdi), %rdi
movq %rdi, 0x18(%rsp)
testq %rdi, %rdi
jle 0x1424
notq %rsi
andq %rsi, %rax
movw $0x2, %r9w
xorl %esi, %esi
leaq 0xd60(%rip), %rdi       # 0x2070
leaq 0x4d59(%rip), %r8       # 0x6070
xorl %r10d, %r10d
movl %r13d, 0x8(%rsp)
movzwl %r9w, %ecx
testq %rdx, %rdx
je 0x138a
movzwl (%rdi,%rcx,2), %r14d
movw $0x1, %bp
xorl %r12d, %r12d
xorl %ebx, %ebx
movb (%r15,%r12), %r13b
notb %r13b
movl $0x8, %r9d
testb $0x1, %r13b
movzwl %bp, %r11d
cmovel %esi, %ebp
xorl %ebp, %ebx
shrb %r13b
movzwl (%rdi,%r11,2), %r11d
addw %r14w, %r11w
movzwl %r11w, %r11d
leal -0x1fff(%r11), %ebp
cmpl $0x1fff, %r11d          # imm = 0x1FFF
cmovbl %r11d, %ebp
movzwl %bp, %r11d
movzwl (%r8,%r11,2), %ebp
decl %r9d
jne 0x1343
incq %r12
cmpq %rdx, %r12
jne 0x1336
jmp 0x1390
movw $0x1, %bp
xorl %ebx, %ebx
movl 0x8(%rsp), %r13d
testl %r13d, %r13d
jle 0x13e2
movzwl (%rdi,%rcx,2), %r14d
movl %r13d, %r12d
movq %rax, %r9
testb $0x1, %r9b
movzwl %bp, %r11d
cmovel %esi, %ebp
xorl %ebp, %ebx
shrq %r9
movzwl (%rdi,%r11,2), %r11d
addw %r14w, %r11w
movzwl %r11w, %r11d
leal -0x1fff(%r11), %ebp
cmpl $0x1fff, %r11d          # imm = 0x1FFF
cmovbl %r11d, %ebp
movzwl %bp, %r11d
movzwl (%r8,%r11,2), %ebp
decl %r12d
jne 0x13a5
movw %bx, 0xa0(%rsp,%r10,2)
movzwl (%rdi,%rcx,2), %ecx
leal 0x1(%rcx), %r9d
movl %r9d, %r11d
movl $0xffff, %ebx           # imm = 0xFFFF
andl %ebx, %r11d
addl $0xffffe002, %ecx       # imm = 0xFFFFE002
cmpl $0x1fff, %r11d          # imm = 0x1FFF
cmovbl %r9d, %ecx
andl %ebx, %ecx
movzwl (%r8,%rcx,2), %r9d
incq %r10
cmpq 0x18(%rsp), %r10
jne 0x131f
leal (,%rdx,8), %eax
movl %eax, 0xc(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movw $0x1, %r13w
movw %r13w, 0x80(%rsp)
movw %r13w, 0x20(%rsp)
cmpl $0x0, 0x18(%rsp)
jle 0x15f8
movl $0x1, %ecx
movw $0x1, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0xa0(%rsp), %r14
leaq 0xbde(%rip), %r12       # 0x2070
movl $0xffff, %r8d           # imm = 0xFFFF
leaq 0x4bd1(%rip), %r9       # 0x6070
xorl %r11d, %r11d
movzwl 0xa0(%rsp,%rbx,2), %ebp
testl %r11d, %r11d
jle 0x1504
leal 0x1(%r11), %eax
negq %rax
movq $-0x1, %r10
leaq 0x22(%rsp), %rdx
movzwl (%rdx), %esi
testq %rsi, %rsi
je 0x14f8
movzwl (%r14,%r10,2), %edi
testq %rdi, %rdi
je 0x14f8
movzwl (%r12,%rdi,2), %edi
addw (%r12,%rsi,2), %di
movzwl %di, %esi
leal -0x1fff(%rsi), %edi
cmpl $0x1fff, %esi           # imm = 0x1FFF
cmovbl %esi, %edi
movzwl %di, %esi
xorw (%r9,%rsi,2), %bp
decq %r10
addq $0x2, %rdx
cmpq %r10, %rax
jne 0x14c2
movzwl %bp, %r10d
movzwl (%r12,%r10,2), %edx
movzwl 0x8(%rsp), %eax
subw (%r12,%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 %r10w, %r10w
je 0x1574
andl %r8d, %eax
leal (%r11,%r11), %edx
movslq %edx, %rsi
movzwl (%r9,%rax,2), %edx
cmpq %rsi, %rbx
movq %r11, 0x48(%rsp)
jge 0x1578
leaq 0x20(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl %ecx, %ebp
callq 0x179e
movl %ebp, %ecx
incl %ecx
movl $0xffff, %r8d           # imm = 0xFFFF
leaq 0x4b03(%rip), %r9       # 0x6070
movq 0x48(%rsp), %r11
jmp 0x15e6
incl %ecx
jmp 0x15e6
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movaps %xmm1, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x179e
movaps 0xb0(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
movaps %xmm1, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movl %ebx, %eax
subl 0x48(%rsp), %eax
incl %eax
movl $0x1, %ecx
movw %bp, 0x8(%rsp)
movl %eax, %r11d
movl $0xffff, %r8d           # imm = 0xFFFF
leaq 0x4a8a(%rip), %r9       # 0x6070
incq %rbx
addq $0x2, %r14
cmpq 0x18(%rsp), %rbx
jne 0x14a2
movzwl 0x20(%rsp), %eax
movw %ax, 0x50(%rsp)
movq 0x22(%rsp), %rax
movq %rax, 0x52(%rsp)
movl 0x2a(%rsp), %eax
movl %eax, 0x5a(%rsp)
movzwl 0x2e(%rsp), %eax
movw %ax, 0x5e(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x1693
movw $0x1, %r13w
xorl %ebx, %ebx
leaq 0xa3d(%rip), %r14       # 0x2070
movl $0xffff, %ebp           # imm = 0xFFFF
leaq 0x4a31(%rip), %r12      # 0x6070
movzwl %r13w, %r13d
leaq 0x50(%rsp), %rdi
movl %r13d, %esi
callq 0x1721
testw %ax, %ax
jne 0x166a
movl %ebx, %ecx
andb $0x7, %cl
movl $0x1, %eax
shll %cl, %eax
movl %ebx, %ecx
shrl $0x3, %ecx
xorb %al, (%r15,%rcx)
movzwl (%r14,%r13,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 (%r12,%rax,2), %r13d
incl %ebx
cmpl %ebx, 0xc(%rsp)
jne 0x163f
movq 0x10(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jle 0x170f
xorl %ebx, %ebx
leaq 0x9c9(%rip), %r14       # 0x2070
movl $0xffff, %ebp           # imm = 0xFFFF
leaq 0x49bd(%rip), %r15      # 0x6070
movzwl %r13w, %r12d
leaq 0x50(%rsp), %rdi
movl %r12d, %esi
callq 0x1721
testw %ax, %ax
jne 0x16e2
movl %ebx, %ecx
andb $0x7, %cl
movl $0x1, %eax
shll %cl, %eax
movl %ebx, %ecx
shrl $0x3, %ecx
movq 0x78(%rsp), %rdx
xorb %al, (%rdx,%rcx)
movzwl (%r14,%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 (%r15,%rax,2), %r13d
incl %ebx
movq 0x10(%rsp), %rax
cmpl 0x10(%rax), %ebx
jl 0x16b3
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq