// File: 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:
subq $0xb8, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %rax
movq 0x8(%rsp), %rdi
movq %rdx, 0x10(%rsp)
movq %rax, 0xa8(%rsp)
movq %rdi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movl %ecx, 0x94(%rsp)
movl %r8d, 0x90(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x8c(%rsp)
movl $0x0, 0x88(%rsp)
callq 0x20a0
movq %rax, %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x2140
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x84(%rsp)
jge 0x3835
movl $0x1, 0xb4(%rsp)
jmp 0x3b8e
movq 0x10(%rsp), %rdi
callq 0x20a0
movq %rax, %rdi
movl $0x42, %esi
movl $0x180, %edx            # imm = 0x180
movb $0x0, %al
callq 0x2140
movl %eax, 0x80(%rsp)
movl 0x84(%rsp), %edi
callq 0x2ff0
movq %rax, 0x78(%rsp)
cvtsi2sdq 0x78(%rsp), %xmm0
cvtsi2sdl 0x94(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x2040
cvttsd2si %xmm0, %eax
movl %eax, 0x74(%rsp)
movq 0x78(%rsp), %rax
movl 0x74(%rsp), %ecx
movl 0x90(%rsp), %edx
subl 0x94(%rsp), %edx
imull %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movl 0x80(%rsp), %edi
movq 0x68(%rsp), %rsi
subq $0x1, %rsi
xorl %edx, %edx
callq 0x22c0
movl 0x80(%rsp), %edi
leaq 0x472f(%rip), %rsi      # 0x8005
movl $0x1, %edx
callq 0x2070
movl 0x80(%rsp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x22c0
movq 0x78(%rsp), %rsi
movl 0x84(%rsp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
movq %r9, %rdi
movl %ecx, %edx
callq 0x22a0
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rsi
movl 0x80(%rsp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x2, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x22a0
movq %rax, 0x58(%rsp)
movl 0x90(%rsp), %eax
subl 0x94(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x2230
movq 0x18(%rsp), %rdi
movq %rax, 0x48(%rsp)
movl 0x90(%rsp), %eax
subl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
leaq 0x48(%rsp), %rsi
callq 0x2770
movslq 0x90(%rsp), %rdi
movl $0x1, %esi
callq 0x2100
movq %rax, 0x38(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x40(%rsp)
movb $0x0, 0x37(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x88(%rsp)
movl 0x88(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x3b39
movl $0x0, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
cmpl 0x94(%rsp), %eax
jge 0x3a77
movslq 0x8c(%rsp), %rax
movslq 0x74(%rsp), %rcx
imulq %rcx, %rax
movslq 0x88(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x78(%rsp), %rax
jge 0x3a48
movq 0x60(%rsp), %rax
movq 0x28(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, 0x37(%rsp)
movb 0x37(%rsp), %dl
movq 0x58(%rsp), %rax
movq 0x28(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x3a4d
movb $0x0, 0x37(%rsp)
movb 0x37(%rsp), %dl
movq 0x38(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x39e3
movq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x2840
movq %rax, 0x20(%rsp)
movl $0x0, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl 0x90(%rsp), %ecx
subl 0x94(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3b21
movq 0x78(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movslq 0x74(%rsp), %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movslq 0x88(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x58(%rsp), %rax
movq 0x28(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x20(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x3a9b
jmp 0x3b23
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x39c7
movq 0x60(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x20e0
movq 0x58(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x20e0
movl 0x84(%rsp), %edi
callq 0x22e0
movl 0x80(%rsp), %edi
callq 0x22e0
movq 0x48(%rsp), %rdi
callq 0x2210
movq 0x38(%rsp), %rdi
callq 0x2210
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopl (%rax)