// File: /workspace/github_cpp_makefile_star2_O1/NinjaDevelper[P]ReedSolomon/cxx/ReedSolomon.cpp
// Function: ReedSolomon::correct(POLY&, int, std::vector<int, std::allocator<int>>&)
POLY& ReedSolomon::correct(POLY& msg, int num,vector<int>& pos){
    int i=0;
    int j=0;
    int errNum=pos.size();
    unsigned char *tmp=(unsigned char *)malloc(errNum<<1);

    //calculate syndrome
    unsigned char *synd=(unsigned char *)malloc(num);
    for(i=0;i<num;i++){
        synd[i] = evaluatePoly(msg, expTable[i]);
    }

    //calculate locator
    POLY locator={(unsigned char *)malloc(errNum+1),1};
    locator.data[0]=1;
    unsigned char c[2]={0,1};
    POLY p={c,2};
    for(i=0;i<errNum;i++){
        c[0] = expTable[msg.n-1-pos[i]];
        multiplyPoly(locator,p,tmp);
    }

    //calculate evaluator
    POLY evaluator={(unsigned char *)malloc(errNum<<1),errNum};
    for(i=0;i<errNum;i++){
        evaluator.data[i]=synd[errNum-i-1];
    }
    multiplyPoly(evaluator, locator,tmp);
    for(i=0;i<errNum;i++){
        evaluator.data[i]=evaluator.data[evaluator.n-errNum+i];
    }
    evaluator.n=errNum;

    //calculate derivation of locator
    for(j=0,i=((locator.n & 0x1) ==0? 0:1);i<locator.n;i+=2,j++){
        locator.data[j]=locator.data[i];
    }
    locator.n=((locator.n & 0x1) ==0? locator.n>>1:(locator.n-1)>>1);

    /* calculate X_j*evaluator(X_j-^1)/locator'(X_j^-1)=
     * evaluator(X_j-^1)/( X_j^-1 * locator'(X_j^-1))
     */
    for(i=0;i<errNum;i++){
        unsigned char minus_i = expTable[pos[i]+256-msg.n];
        unsigned char evaluator_minus_i = evaluatePoly(evaluator, minus_i);
        unsigned char locator_minus_i = evaluatePoly(locator, 
                                multiply(evaluator_minus_i,evaluator_minus_i));
        msg.data[pos[i]] ^= divide(evaluator_minus_i, 
                                   multiply(minus_i,locator_minus_i));
    }
    free(tmp);
    free(synd);
    free(locator.data);
    free(evaluator.data);
    return msg;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r14d
movq %rsi, %rbx
movq %rdi, %r15
movq 0x8(%rcx), %r12
movq %rcx, 0x48(%rsp)
subq (%rcx), %r12
movq %r12, %rax
shrq $0x2, %rax
movq %rax, 0x8(%rsp)
addl %eax, %eax
movslq %eax, %rdi
movq %rdi, 0x50(%rsp)
callq 0x2190
movq %rax, 0x28(%rsp)
movslq %r14d, %r13
movq %r13, %rdi
callq 0x2190
movq %rax, %rbp
testl %r13d, %r13d
jle 0x26bf
movl %r14d, %r14d
xorl %r13d, %r13d
movzbl (%r15,%r13), %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x24a0
movb %al, (%rbp,%r13)
incq %r13
cmpq %r13, %r14
jne 0x26a2
movq %rbp, 0x40(%rsp)
shlq $0x1e, %r12
movabsq $0x100000000, %rdi      # imm = 0x100000000
addq %r12, %rdi
sarq $0x20, %rdi
callq 0x2190
movq %rax, 0x18(%rsp)
movl $0x1, 0x20(%rsp)
movb $0x1, (%rax)
leaq 0x16(%rsp), %rax
movw $0x100, (%rax)          # imm = 0x100
movq %rax, 0x58(%rsp)
movl $0x2, 0x60(%rsp)
cmpl $0x0, 0x8(%rsp)
movq 0x48(%rsp), %rbp
movq 0x28(%rsp), %r13
jle 0x2759
movq 0x8(%rsp), %r12
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %r14d, %r14d
movq (%rbp), %rax
movl (%rax,%r14,4), %eax
notl %eax
addl 0x8(%rbx), %eax
cltq
movb (%r15,%rax), %al
movb %al, 0x16(%rsp)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %r13, %rcx
callq 0x23ce
incq %r14
cmpq %r14, %r12
jne 0x2725
movq 0x50(%rsp), %rdi
callq 0x2190
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rax
movl %eax, 0x38(%rsp)
testl %eax, %eax
movq 0x40(%rsp), %r12
jle 0x27a4
movq 0x8(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rax,%r12), %rcx
decq %rcx
xorl %edx, %edx
movb (%rcx), %sil
movq 0x30(%rsp), %rdi
movb %sil, (%rdi,%rdx)
incq %rdx
decq %rcx
cmpq %rdx, %rax
jne 0x278d
leaq 0x30(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r15, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rcx
callq 0x23ce
cmpl $0x0, 0x8(%rsp)
movq 0x48(%rsp), %r13
jle 0x27f9
movq 0x8(%rsp), %rcx
movl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
negl %ecx
xorl %edx, %edx
movq 0x30(%rsp), %rsi
leal (%rcx,%rdx), %edi
addl 0x38(%rsp), %edi
movslq %edi, %rdi
movb (%rsi,%rdi), %dil
movb %dil, (%rsi,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x27da
movq 0x8(%rsp), %rax
movl %eax, 0x38(%rsp)
movl 0x20(%rsp), %eax
cmpl $0x2, %eax
jl 0x283e
andl $0x1, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x18(%rsp), %rsi
leaq (%rsi,%rax), %rdi
movb (%rdi,%rdx,2), %dil
movb %dil, (%rsi,%rdx)
incq %rdx
movslq 0x20(%rsp), %rsi
leaq (%rax,%rcx), %rdi
addq $0x2, %rdi
addq $0x2, %rcx
cmpq %rsi, %rdi
jl 0x2812
movl %esi, %eax
sarl %eax
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x29d3
movq 0x8(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x8(%rsp)
xorl %eax, %eax
movq (%r13), %rcx
movslq (%rcx,%rax,4), %rcx
movl 0x8(%rbx), %edx
movl %ecx, %esi
subl %edx, %esi
addl $0x100, %esi            # imm = 0x100
movslq %esi, %rdx
movzbl (%r15,%rdx), %edx
movq 0x30(%rsp), %rdi
movb (%rdi), %r10b
movslq 0x38(%rsp), %r8
cmpq $0x2, %r8
jl 0x28d0
movl $0x1, %r9d
xorl %esi, %esi
testq %rdx, %rdx
je 0x28bf
testb %r10b, %r10b
je 0x28bf
movzbl %r10b, %esi
movzbl 0x200(%r15,%rsi), %esi
movzbl 0x200(%r15,%rdx), %r10d
addq %rsi, %r10
movb (%r15,%r10), %sil
xorb (%rdi,%r9), %sil
incq %r9
movl %esi, %r10d
cmpq %r9, %r8
jne 0x2896
jmp 0x28d3
movl %r10d, %esi
testb %sil, %sil
je 0x28eb
movzbl %sil, %edi
movzbl 0x200(%r15,%rdi), %edi
movb (%r15,%rdi,2), %dil
jmp 0x28ed
xorl %edi, %edi
movq 0x18(%rsp), %r8
movb (%r8), %bpl
movslq 0x20(%rsp), %r9
cmpq $0x2, %r9
jl 0x2945
movzbl %dil, %r10d
movl $0x1, %r12d
xorl %r11d, %r11d
testb %dil, %dil
je 0x2934
testb %bpl, %bpl
je 0x2934
movzbl %bpl, %r11d
movzbl 0x200(%r15,%r11), %r11d
movzbl 0x200(%r15,%r10), %r14d
addq %r11, %r14
movb (%r15,%r14), %r11b
xorb (%r8,%r12), %r11b
incq %r12
movl %r11d, %ebp
cmpq %r12, %r9
jne 0x290a
jmp 0x2948
movl %ebp, %r11d
movb $-0x1, %dil
testq %rdx, %rdx
je 0x29b4
testb %r11b, %r11b
movq 0x28(%rsp), %r14
movq 0x40(%rsp), %r12
je 0x29be
movzbl 0x200(%r15,%rdx), %edx
movzbl %r11b, %edi
movzbl 0x200(%r15,%rdi), %edi
addq %rdx, %rdi
movzbl (%r15,%rdi), %edx
cmpq $0x1, %rdx
movl $0x0, %edi
sbbl %edi, %edi
testb %sil, %sil
je 0x29be
testb %dl, %dl
je 0x29be
movzbl %sil, %esi
movzbl 0x200(%r15,%rsi), %esi
movzbl 0x200(%r15,%rdx), %edx
subq %rdx, %rsi
movb 0xff(%rsi,%r15), %dil
jmp 0x29be
movq 0x28(%rsp), %r14
movq 0x40(%rsp), %r12
movq (%rbx), %rdx
xorb %dil, (%rdx,%rcx)
incq %rax
cmpq 0x8(%rsp), %rax
jne 0x2860
movq %r14, %rdi
callq 0x2180
movq %r12, %rdi
callq 0x2180
movq 0x18(%rsp), %rdi
callq 0x2180
movq 0x30(%rsp), %rdi
callq 0x2180
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq