// File: /workspace/github_cpp_makefile_star2_O1/NinjaDevelper[P]ReedSolomon/cxx/ReedSolomon.cpp
// Function: ReedSolomon::decodeFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int, long, std::vector<long, std::allocator<long>>&)
int ReedSolomon::decodeFile(string inFile,string outFile,int K,int N,
                            long size,vector<long>&errLoc){
    int i=0;
    int k=0;

    int fin=open(inFile.c_str(),O_RDWR);
    if(fin<0){
        return 1;
    }
    long inSize=getSize(fin);

    //use MAP_PRIVATE not to copy mem to file.
    unsigned char *buf = 
        (unsigned char*)mmap(NULL, inSize, PROT_READ|PROT_WRITE, 
                               MAP_PRIVATE, fin, 0);

    int interleave=ceil((double)size/K);
    map<int, vector<int> > errors;

    //translate location in file to column/row number of 
    //K x interleave length matrix.
    for(unsigned int i=0;i<errLoc.size();i++){
        long loc=errLoc.at(i);
        errors[loc%interleave].push_back(loc/interleave);
    }
    //it error does not exist in data area but in parity area, ignore it.
    for(i=0;i<interleave;i++){
        bool existData=false;
        for(unsigned int k=0;k<errors[i].size();k++){
            if(errors[i].at(k)<K) existData=true;
        }
        if(!existData) errors[i].clear();
    }
    POLY msgIn={(unsigned char*)malloc(N),N}; 

    //collect data in row , and execute correction.
    unsigned char c=0;
    long loc=0;
    int e=0;
    for(k=0;k<interleave;k++){
        if(errors[k].size()==0) continue;
        for(i=0;i<K;i++){
            loc=(long)i*interleave+k;
            if(loc<size){
                c=buf[loc];
            }else{
                c=0;
            }
            msgIn.data[i]=c;
        }
        for(i=0;i<N-K;i++){
            loc=size+(long)i*interleave+k;
            c=buf[loc];
            msgIn.data[K+i]=c;
        }
        correct(msgIn,N-K,errors[k]);
        for(unsigned int i=0;i<errors[k].size();i++){
            e=errors[k].at(i);
            if(e<K)
                buf[e*interleave+k]=msgIn.data[e];
        }
    }

    //copy only data area to destination file.
    FILE *fp=fopen(outFile.c_str(),"wb");
    fwrite(buf,1,size,fp);
    fclose(fp);

    close(fin);
    munmap(buf,inSize);
    free(msgIn.data);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %rbx
movl %r8d, 0x14(%rsp)
movl %ecx, %r13d
movq %rdx, 0x48(%rsp)
movq %rdi, 0x50(%rsp)
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movq (%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2100
testl %eax, %eax
js 0x2e22
movq %rbx, %r15
movq 0xc0(%rsp), %r14
xorl %ebx, %ebx
movl %eax, %ebp
movl %eax, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2210
movq %rax, %r12
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2210
xorl %edi, %edi
movq %r12, 0x40(%rsp)
movq %r12, %rsi
movl $0x3, %edx
movl $0x2, %ecx
movl %ebp, 0x38(%rsp)
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x21f0
movq %r15, 0x30(%rsp)
cvtsi2sd %r15, %xmm0
movq %rax, 0x18(%rsp)
cvtsi2sd %r13d, %xmm1
divsd %xmm1, %xmm0
callq 0x2040
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
leaq 0x60(%rsp), %rax
movl $0x0, (%rax)
movq %rbx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rbx, 0x20(%rax)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x2b7e
movslq 0xc(%rsp), %r15
movl $0x1, %ebx
xorl %ecx, %ecx
leaq 0x20(%rsp), %r12
movq (%rax,%rcx,8), %rax
cqto
idivq %r15
movq %rax, %rbp
movl %edx, 0x20(%rsp)
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x30d6
movl %ebp, 0x3c(%rsp)
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x2b5a
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rax)
jmp 0x2b67
movq %rax, %rdi
leaq 0x3c(%rsp), %rdx
callq 0x342c
movl %ebx, %ecx
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
incl %ebx
cmpq %rcx, %rdx
ja 0x2b23
movl $0x0, 0x8(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x2c2f
xorl %r15d, %r15d
leaq 0x58(%rsp), %r12
leaq 0x8(%rsp), %rbp
movl $0x1, %r14d
xorl %ebx, %ebx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x313a
movl %ebx, %ebx
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x2, %rcx
cmpq %rbx, %rcx
jbe 0x2bf5
movq %r12, %rdi
movq %rbp, %rsi
callq 0x313a
movq (%rax), %rcx
movq 0x8(%rax), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
jbe 0x2e4a
cmpl %r13d, (%rcx,%rbx,4)
movzbl %r15b, %r15d
cmovll %r14d, %r15d
incl %ebx
jmp 0x2ba6
testb $0x1, %r15b
jne 0x2c13
movq %r12, %rdi
movq %rbp, %rsi
callq 0x313a
movq (%rax), %rcx
cmpq %rcx, 0x8(%rax)
je 0x2c13
movq %rcx, 0x8(%rax)
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
xorl %r15d, %r15d
movl $0x0, %ebx
cmpl 0xc(%rsp), %eax
jl 0x2ba6
movslq 0x14(%rsp), %rbx
movq %rbx, %rdi
callq 0x2190
movq %rax, 0x20(%rsp)
movl %ebx, 0x28(%rsp)
movl $0x0, 0x10(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x2dbb
movl 0xc(%rsp), %r15d
subl %r13d, 0x14(%rsp)
movq 0x18(%rsp), %r14
addq 0x30(%rsp), %r14
leaq 0x58(%rsp), %rbp
leaq 0x10(%rsp), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x313a
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
je 0x2da7
movl $0x0, 0x8(%rsp)
testl %r13d, %r13d
jle 0x2cd8
xorl %eax, %eax
cltq
movq %rax, %rdx
imulq %r15, %rdx
movslq 0x10(%rsp), %rcx
addq %rdx, %rcx
cmpq 0x30(%rsp), %rcx
jge 0x2cbf
movq 0x18(%rsp), %rdx
movb (%rdx,%rcx), %cl
jmp 0x2cc1
xorl %ecx, %ecx
movq 0x20(%rsp), %rdx
movb %cl, (%rdx,%rax)
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
cmpl %r13d, %eax
jl 0x2c9d
movl $0x0, 0x8(%rsp)
movl 0x14(%rsp), %esi
testl %esi, %esi
jle 0x2d19
xorl %eax, %eax
cltq
movq %rax, %rcx
imulq %r15, %rcx
movslq 0x10(%rsp), %rdx
addq %r14, %rcx
movb (%rdx,%rcx), %cl
movq 0x20(%rsp), %rdx
addl %r13d, %eax
cltq
movb %cl, (%rdx,%rax)
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
cmpl %esi, %eax
jl 0x2cea
movq %rbp, %rdi
movq %r12, %rsi
callq 0x313a
movq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %rax, %rcx
callq 0x2646
xorl %ebx, %ebx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x313a
movl %ebx, %ebx
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x2, %rcx
cmpq %rbx, %rcx
jbe 0x2da7
movq %rbp, %rdi
movq %r12, %rsi
callq 0x313a
movq (%rax), %rcx
movq 0x8(%rax), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
jbe 0x2e39
movslq (%rcx,%rbx,4), %rax
cmpl %r13d, %eax
jge 0x2da3
movq 0x20(%rsp), %rcx
movb (%rcx,%rax), %cl
imull 0xc(%rsp), %eax
movslq 0x10(%rsp), %rdx
cltq
addq %rdx, %rax
movq 0x18(%rsp), %rdx
movb %cl, (%rdx,%rax)
incl %ebx
jmp 0x2d3c
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
cmpl 0xc(%rsp), %eax
jl 0x2c76
movq 0x48(%rsp), %rax
movq (%rax), %rdi
leaq 0x123a(%rip), %rsi      # 0x4004
callq 0x2170
movq %rax, %rbx
movl $0x1, %esi
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rdx
movq %rax, %rcx
callq 0x2220
movq %rbx, %rdi
callq 0x2130
movl 0x38(%rsp), %edi
callq 0x2230
movq %r14, %rdi
movq 0x40(%rsp), %rsi
callq 0x20c0
movq 0x20(%rsp), %rdi
callq 0x2180
leaq 0x58(%rsp), %rdi
callq 0x31aa
xorl %eax, %eax
jmp 0x2e27
movl $0x1, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11c7(%rip), %rdi      # 0x4007
movq %rbx, %rsi
xorl %eax, %eax
callq 0x21b0
leaq 0x11b6(%rip), %rdi      # 0x4007
movq %rbx, %rsi
xorl %eax, %eax
callq 0x21b0
jmp 0x2e6b
jmp 0x2e6b
jmp 0x2e6b
jmp 0x2e6b
jmp 0x2e6b
jmp 0x2e6b
jmp 0x2e6b
jmp 0x2e6b
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x31aa
movq %rbx, %rdi
callq 0x21e0