// File: 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:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movl %edx, 0xb4(%rsp)
movq %rcx, 0xa8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x40(%rsp)
movl $0x0, 0xa4(%rsp)
movl $0x0, 0xa0(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x3ba0
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
shll %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x90(%rsp)
movslq 0xb4(%rsp), %rdi
callq 0x2230
movq %rax, 0x88(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0xb4(%rsp), %eax
jge 0x2aef
movq 0x40(%rsp), %rax
movq 0xb8(%rsp), %rsi
movslq 0xa4(%rsp), %rcx
movq %rax, %rdi
movzbl (%rax,%rcx), %edx
callq 0x26b0
movb %al, %dl
movq 0x88(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x2a96
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x78(%rsp)
movl $0x1, 0x80(%rsp)
movq 0x78(%rsp), %rax
movb $0x1, (%rax)
movw 0x554d(%rip), %ax       # 0x806d
movw %ax, 0x76(%rsp)
leaq 0x76(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x2, 0x68(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x2bba
movq 0xb8(%rsp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0xa8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0x3bc0
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl 0x3c(%rsp), %eax
subl (%rcx), %eax
cltq
movb (%rdi,%rax), %al
movb %al, 0x76(%rsp)
movq 0x90(%rsp), %rcx
leaq 0x78(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x24f0
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x2b42
movl 0x9c(%rsp), %eax
shll %eax
movslq %eax, %rdi
callq 0x2230
movq %rax, 0x50(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0x58(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x2c38
movq 0x88(%rsp), %rax
movl 0x9c(%rsp), %ecx
subl 0xa4(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x50(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x2be6
movq 0x40(%rsp), %rdi
movq 0x90(%rsp), %rcx
leaq 0x50(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0x24f0
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x2caf
movq 0x50(%rsp), %rax
movl 0x58(%rsp), %ecx
subl 0x9c(%rsp), %ecx
addl 0xa4(%rsp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x50(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x2c5f
movl 0x9c(%rsp), %eax
movl %eax, 0x58(%rsp)
movl $0x0, 0xa0(%rsp)
movl 0x80(%rsp), %edx
andl $0x1, %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x80(%rsp), %eax
jge 0x2d37
movq 0x78(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x78(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xa4(%rsp), %eax
addl $0x2, %eax
movl %eax, 0xa4(%rsp)
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x2ce3
movl 0x80(%rsp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x2d55
movl 0x80(%rsp), %eax
sarl %eax
movl %eax, 0x38(%rsp)
jmp 0x2d65
movl 0x80(%rsp), %eax
subl $0x1, %eax
sarl %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
movl %eax, 0x80(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x2fa5
movq 0xa8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0x3bc0
movq 0x40(%rsp), %rdi
movl (%rax), %eax
addl $0x100, %eax            # imm = 0x100
movq 0xb8(%rsp), %rcx
subl 0x8(%rcx), %eax
cltq
movb (%rdi,%rax), %al
movb %al, 0x4f(%rsp)
leaq 0x50(%rsp), %rsi
movzbl 0x4f(%rsp), %edx
callq 0x26b0
movb %al, 0x4e(%rsp)
movzbl 0x4e(%rsp), %eax
cmpl $0x0, %eax
je 0x2deb
movzbl 0x4e(%rsp), %eax
cmpl $0x0, %eax
jne 0x2df3
xorl %eax, %eax
movl %eax, 0x34(%rsp)
jmp 0x2e1f
movq 0x40(%rsp), %rax
movzbl 0x4e(%rsp), %ecx
movzbl 0x200(%rax,%rcx), %ecx
movzbl 0x4e(%rsp), %edx
movzbl 0x200(%rax,%rdx), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x34(%rsp)
movq 0x40(%rsp), %rdi
movl 0x34(%rsp), %eax
leaq 0x78(%rsp), %rsi
movzbl %al, %edx
callq 0x26b0
movb %al, 0x4d(%rsp)
movzbl 0x4f(%rsp), %eax
cmpl $0x0, %eax
je 0x2e4d
movzbl 0x4d(%rsp), %eax
cmpl $0x0, %eax
jne 0x2e55
xorl %eax, %eax
movl %eax, 0x30(%rsp)
jmp 0x2e81
movq 0x40(%rsp), %rax
movzbl 0x4f(%rsp), %ecx
movzbl 0x200(%rax,%rcx), %ecx
movzbl 0x4d(%rsp), %edx
movzbl 0x200(%rax,%rdx), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl $0x0, %eax
jne 0x2e98
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x2c(%rsp)
jmp 0x2f4a
movzbl 0x4e(%rsp), %eax
cmpl $0x0, %eax
jne 0x2ead
xorl %eax, %eax
movl %eax, 0x28(%rsp)
jmp 0x2f42
movq 0x40(%rsp), %rax
movzbl 0x4e(%rsp), %ecx
movzbl 0x200(%rax,%rcx), %ecx
addl $0xff, %ecx
movl %ecx, 0x1c(%rsp)
addq $0x200, %rax            # imm = 0x200
movq %rax, 0x20(%rsp)
movzbl 0x4f(%rsp), %eax
cmpl $0x0, %eax
je 0x2ee8
movzbl 0x4d(%rsp), %eax
cmpl $0x0, %eax
jne 0x2ef0
xorl %eax, %eax
movl %eax, 0x18(%rsp)
jmp 0x2f1c
movq 0x40(%rsp), %rax
movzbl 0x4f(%rsp), %ecx
movzbl 0x200(%rax,%rcx), %ecx
movzbl 0x4d(%rsp), %edx
movzbl 0x200(%rax,%rdx), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x18(%rsp)
movq 0x40(%rsp), %rax
movl 0x1c(%rsp), %ecx
movq 0x20(%rsp), %rdx
movl 0x18(%rsp), %esi
movslq %esi, %rsi
movzbl (%rdx,%rsi), %edx
subl %edx, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0xb8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rsp), %rdi
movslq 0xa4(%rsp), %rsi
callq 0x3bc0
movl 0xc(%rsp), %esi
movq %rax, %rcx
movq 0x10(%rsp), %rax
movslq (%rcx), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x2d7b
movq 0x90(%rsp), %rdi
callq 0x2210
movq 0x88(%rsp), %rdi
callq 0x2210
movq 0x78(%rsp), %rdi
callq 0x2210
movq 0x50(%rsp), %rdi
callq 0x2210
movq 0xb8(%rsp), %rax
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)