// File: /workspace/github_cpp_makefile_star2_O2/NinjaDevelper[P]ReedSolomon/cxx/ReedSolomon.cpp
// Function: ReedSolomon::encodeFile(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)
int ReedSolomon::encodeFile(string origFile,string encodedFile,int K,int N){
    int i=0;
    int k=0;

    int fin=open(origFile.c_str(),O_RDONLY);
    if(fin<0){
        return 1;
    }
    int fout=open(encodedFile.c_str(),O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
    long size=getSize(fin);

    int interleave=ceil((double)size/K);
    long writeSize=size+interleave*(N-K);

    //write a last byte to ensure the output file size 
    lseek(fout,writeSize-1,SEEK_SET);
    write(fout,"",1);
    lseek(fout,0,SEEK_SET);

    unsigned char *inBuf =
        (unsigned char*) mmap(NULL, size, PROT_READ, MAP_SHARED, fin, 0);
    unsigned char *outBuf = 
        (unsigned char*)mmap(NULL, writeSize, PROT_WRITE, MAP_SHARED,
                               fout, 0);

    POLY generator={(unsigned char*)malloc(N-K+1),N-K+1};
    calcGenerator(generator);
    POLY msgIn={(unsigned char*)calloc(N,1),K}; 

    //collect data in row , encode, and write parities.
    unsigned char c=0;
    long loc=0;
    unsigned char *parity=NULL;
    for(k=0;k<interleave;k++){
        for(i=0;i<K;i++){
            loc=(long)i*interleave+k;
            if(loc<size){
                c=inBuf[loc];
                outBuf[loc]=c;
            }else{
                c=0;
            }
            msgIn.data[i]=c;
        }
        parity=encodeMessage(msgIn,generator);
        for(i=0;i<N-K;i++){
            loc=size+(long)i*interleave+k;
            outBuf[loc]=parity[i];
            parity[i]=0;
        }
    }
    munmap(inBuf,size);
    munmap(outBuf,writeSize);
    close(fin);
    close(fout);
    free(generator.data);
    free(msgIn.data);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %ebp
movl %ecx, %ebx
movq %rdx, %r12
movq %rdi, %r14
movq (%rsi), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
js 0x2ed3
movl %eax, %r15d
movq (%r12), %rdi
pushq $0x42
popq %rsi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x2110
movl %eax, %r13d
movl %r15d, %edi
callq 0x28f8
movq %rax, %r12
cvtsi2sd %rax, %xmm0
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
callq 0x2040
cvttsd2si %xmm0, %ecx
movl %ecx, 0x8(%rsp)
movq %r14, 0x28(%rsp)
movl %ebp, %r14d
subl %ebx, %r14d
movl %r14d, %eax
imull %ecx, %eax
cltq
leaq (%r12,%rax), %rcx
movq %rcx, 0x18(%rsp)
leaq (%r12,%rax), %rsi
decq %rsi
movl %r13d, %edi
xorl %edx, %edx
callq 0x2220
leaq 0x1301(%rip), %rsi      # 0x4050
pushq $0x1
popq %rdx
movl %r13d, %edi
callq 0x2060
movl %r13d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2220
movl %ebp, (%rsp)
pushq $0x1
popq %rbp
xorl %edi, %edi
movq %r12, %rsi
movl %ebp, %edx
movl %ebp, %ecx
movl %r15d, 0x14(%rsp)
movl %r15d, %r8d
xorl %r9d, %r9d
callq 0x2200
movq %rax, 0x20(%rsp)
pushq $0x2
popq %rdx
xorl %edi, %edi
movq 0x18(%rsp), %rsi
movl %ebp, %ecx
movl %r13d, 0x10(%rsp)
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x2200
movq %rax, %rbp
leal 0x1(%r14), %eax
movslq %eax, %r15
movq %r15, %rdi
callq 0x21a0
leaq 0x38(%rsp), %rsi
movq %rax, (%rsi)
movl %r15d, 0x8(%rsi)
movq 0x28(%rsp), %rdi
callq 0x24ac
movslq (%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x20e0
movq %rax, %r15
leaq 0x48(%rsp), %rax
movq %r15, (%rax)
movl %ebx, 0x8(%rax)
movq %rbp, %rdi
addq %r12, %rdi
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
movslq 0x8(%rsp), %r13
testl %r14d, %r14d
cmovlel %eax, %r14d
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
cmpq 0x30(%rsp), %rax
je 0x2e91
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
movq 0x20(%rsp), %rsi
cmpq %rcx, %rbx
je 0x2e4b
cmpq %r12, %rax
jge 0x2e3d
movb (%rsi,%rax), %dl
movb %dl, (%rbp,%rax)
jmp 0x2e3f
xorl %edx, %edx
movb %dl, (%r15,%rcx)
incq %rcx
addq %r13, %rax
jmp 0x2e2a
movq %rdi, (%rsp)
movq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x2546
movq (%rsp), %rdi
movq %rdi, %rcx
xorl %edx, %edx
cmpq %rdx, %r14
je 0x2e84
movb (%rax,%rdx), %sil
movb %sil, (%rcx)
movb $0x0, (%rax,%rdx)
incq %rdx
addq %r13, %rcx
jmp 0x2e6c
movq 0x8(%rsp), %rax
incq %rax
incq %rdi
jmp 0x2e17
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x20d0
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0x20d0
movl 0x14(%rsp), %edi
callq 0x2240
movl 0x10(%rsp), %edi
callq 0x2240
movq 0x38(%rsp), %rdi
callq 0x2190
movq %r15, %rdi
callq 0x2190
xorl %eax, %eax
jmp 0x2ed6
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop