// File: /workspace/github_cpp_makefile_star2_O1/c2-d2[P]disty/src/main.cpp
// Function: main
int main (int argc, const char **argv) {
    params_t params;
    parse_arguments(argc, argv, params);

    assert(params.n_strategy!=n_strategy_t::REPLACE_CLOSEST);

    cerr << "Loading sequences from " << params.fasta_fn << endl;
    vector<string> names, seqs;
    load_sequences(params.fasta_fn, names, seqs);
    //print_sequences(seqs);

    int count=(int)seqs.size();
    int len=(int)seqs[0].size();

    cerr << "Computing pileup" << endl;
    vector<vector<int>> pileup(len, vector<int>(128));
    compute_pileup(seqs, pileup);
    //print_pileup(pileup);

    string consensus(len, '?');
    if(params.n_strategy==n_strategy_t::REPLACE_MAJOR){
        cerr << "Computing consensus" << endl;
        compute_consensus(pileup, consensus);
        //print_consensus(consensus);
    }


    cerr << "Computing mask" << endl;
    string mask(len, '?');
    if(params.n_strategy==n_strategy_t::IGNORE_GLOBALLY){
        compute_mask(mask, pileup, 1);
    }
    else{
        int min_n=ceil(count*params.skip_n);
        compute_mask(mask, pileup, min_n);
    }
    //print_mask(mask);


    cerr << "Computing distance matrix" << endl;
    vector<vector<int>> pair_matrix(128, vector<int>(128, 0));
    vector<vector<int>> distance_matrix(count, vector<int>(count, 0));
    pair_char_t pair_char;

    if(params.n_strategy==n_strategy_t::REPLACE_MAJOR){
        cerr << "Replacing N's by major alleles" << endl;
        for (auto &seq: seqs){
            for(int i=0;i<static_cast<int>(seq.size());i++){
                if (seq[i]=='N'||seq[i]=='N'){
                    seq[i]=consensus[i];
                }
            }
        }
    }


    /*
     * For each pair:
     */
    for(int i=0;i<count;i++){
        for(int j=0;j<=i;j++){
            //cerr << "\n(" << i << "," << j << ")" << endl;

            /*
             * 1) Pair matrix
             */
            compute_pair_matrix(seqs[i], seqs[j], mask, pair_matrix);

            /*
             * 2) Characteristics
             */

            if (params.input==input_t::ACGT){
                pair_matrix_char_acgt(pair_matrix, pair_char);
            }
            else{
                if (params.input==input_t::BINARY){
                    pair_matrix_char_binary(pair_matrix, pair_char);
                }
                else{
                    assert(false);
                }
            }

            //print_pair_matrix_char(pair_char);

            /*
             * 3) Distance
             */

            int dist=0;
            if(params.n_strategy==n_strategy_t::IGNORE_PAIRWISE_NORM){
                dist=distance_norm(pair_char);
            }
            else{
                dist=distance(pair_char);
            }
            distance_matrix[i][j]=distance_matrix[j][i]=dist;
        }
    }


    print_distance_matrix(distance_matrix, names);


    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
leaq 0xb8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq %rax, 0x10(%rcx)
movl $0x3f800000, 0x18(%rcx) # imm = 0x3F800000
leaq 0xa8(%rsp), %rdx
callq 0x24ec
cmpl $0x4, 0xcc(%rsp)
je 0x2f9c
movq 0x56c5(%rip), %rdi      # 0x7ff0
leaq 0x2aa9(%rip), %rsi      # 0x53db
movl $0x17, %edx
callq 0x21c0
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
movq 0x569d(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
xorps %xmm0, %xmm0
leaq 0x120(%rsp), %rsi
movaps %xmm0, (%rsi)
xorl %eax, %eax
movq %rax, 0x10(%rsi)
leaq 0x60(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0xa8(%rsp), %rdi
callq 0x33c3
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %r13
movq 0x8(%r15), %r14
movq 0x562c(%rip), %rbx      # 0x7ff0
leaq 0x2a28(%rip), %rsi      # 0x53f3
movl $0x10, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x55fa(%rip), %rdi      # 0x7ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movl $0x200, %edi            # imm = 0x200
callq 0x2190
movslq %r14d, %rbx
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movq %rax, 0x8(%r14)
movq %rax, %r12
addq $0x200, %r12            # imm = 0x200
movq %r12, 0x10(%r14)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x20c0
movq %r12, 0x8(%r14)
leaq 0xd8(%rsp), %rdi
leaq 0x88(%rsp), %rcx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x39d2
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2a80
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
leaq 0x60(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x3a69
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movl $0x3f, %edx
callq 0x21d0
cmpl $0x3, 0xcc(%rsp)
jne 0x2b0f
movq 0x5532(%rip), %r14      # 0x7ff0
leaq 0x293f(%rip), %rsi      # 0x5404
movl $0x13, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x5500(%rip), %rdi      # 0x7ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
leaq 0xd8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3b70
movq 0x54da(%rip), %r14      # 0x7ff0
leaq 0x28fb(%rip), %rsi      # 0x5418
movl $0xe, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x54a8(%rip), %rdi      # 0x7ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
movl $0x3f, %edx
callq 0x21d0
subq %r15, %r13
shrq $0x5, %r13
cmpl $0x2, 0xcc(%rsp)
jne 0x2ba3
leaq 0x88(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x1, %edx
callq 0x3c64
jmp 0x2bcf
cvtsi2ss %r13d, %xmm0
mulss 0xd0(%rsp), %xmm0
callq 0x20d0
cvttss2si %xmm0, %edx
leaq 0x88(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x3c64
movq 0x541a(%rip), %rbx      # 0x7ff0
leaq 0x284a(%rip), %rsi      # 0x5427
movl $0x19, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x53e8(%rip), %rdi      # 0x7ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movl $0x200, %edi            # imm = 0x200
callq 0x2190
leaq 0x40(%rsp), %rbx
movq %rax, (%rbx)
movq %rax, 0x8(%rbx)
movq %rax, %r14
addq $0x200, %r14            # imm = 0x200
movq %r14, 0x10(%rbx)
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0x20c0
movq %r14, 0x8(%rbx)
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rcx
movl $0x80, %esi
movq %rbx, %rdx
callq 0x39d2
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2c8e
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movslq %r13d, %rbx
leaq 0x11c(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rcx
movq %rbx, %rsi
callq 0x3e62
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0x6(%rsp), %rcx
movq %rbx, %rsi
callq 0x39d2
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x2cdf
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
cmpl $0x3, 0xcc(%rsp)
jne 0x2d72
movq 0x52fc(%rip), %rbx      # 0x7ff0
leaq 0x2746(%rip), %rsi      # 0x5441
movl $0x1e, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x52ca(%rip), %rdi      # 0x7ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
cmpq %rcx, %rax
je 0x2d72
cmpl $0x0, 0x8(%rax)
jle 0x2d6c
xorl %edx, %edx
movq (%rax), %rsi
cmpb $0x4e, (%rsi,%rdx)
jne 0x2d60
movq 0x20(%rsp), %rdi
movb (%rdi,%rdx), %dil
movb %dil, (%rsi,%rdx)
incq %rdx
movslq 0x8(%rax), %rsi
cmpq %rsi, %rdx
jl 0x2d4a
addq $0x20, %rax
jmp 0x2d3d
testl %r13d, %r13d
jle 0x2eb6
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl $0x1, %eax
movq %rax, 0x80(%rsp)
xorl %ebp, %ebp
leaq 0x100(%rsp), %rbx
movq %r13, 0xf0(%rsp)
movq %rbp, %r12
shlq $0x5, %r12
leaq (,%rbp,2), %rax
addq %rbp, %rax
movq %rax, 0xf8(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq 0x60(%rsp), %rsi
leaq (%rsi,%r12), %rdi
addq %r14, %rsi
leaq 0x88(%rsp), %rdx
movq %rbx, %rcx
callq 0x3edf
movl 0xc8(%rsp), %eax
cmpl $0x1, %eax
je 0x2e08
testl %eax, %eax
jne 0x2f7d
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x401c
jmp 0x2e15
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4121
cmpl $0x1, 0xcc(%rsp)
jne 0x2e59
movl 0xc(%rsp), %eax
movl 0x8(%rsp), %ecx
addl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
addl 0x10(%rsp), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm0
callq 0x21b0
cvttss2si %xmm0, %eax
jmp 0x2e5d
movl 0xc(%rsp), %eax
movq 0x40(%rsp), %rcx
movq (%rcx,%r15), %rdx
movl %eax, (%rdx,%rbp,4)
movq 0xf8(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
addq $0x20, %r14
addq $0x18, %r15
cmpq %r13, 0x80(%rsp)
leaq 0x100(%rsp), %rbx
jne 0x2dc4
incq %rbp
incq 0x80(%rsp)
movq 0xf0(%rsp), %r13
cmpq %r13, %rbp
jne 0x2da1
leaq 0x40(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x420e
leaq 0x40(%rsp), %rdi
callq 0x43cc
leaq 0x100(%rsp), %rdi
callq 0x43cc
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2f04
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2f20
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xd8(%rsp), %rdi
callq 0x43cc
leaq 0x60(%rsp), %rdi
callq 0x441a
leaq 0x120(%rsp), %rdi
callq 0x441a
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x2f69
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21a0
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24dc(%rip), %rdi      # 0x5460
leaq 0x235b(%rip), %rsi      # 0x52e6
leaq 0x242c(%rip), %rcx      # 0x53be
movl $0x255, %edx            # imm = 0x255
callq 0x20e0
leaq 0x23ea(%rip), %rdi      # 0x538d
leaq 0x233c(%rip), %rsi      # 0x52e6
leaq 0x240d(%rip), %rcx      # 0x53be
movl $0x209, %edx            # imm = 0x209
callq 0x20e0
jmp 0x304e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x303a
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x303a
movq %rax, %rbx
jmp 0x303a
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3051
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x3051
jmp 0x304e
jmp 0x3078
movq %rax, %rbx
jmp 0x3097
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x30a4
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x30a4
jmp 0x3049
jmp 0x3049
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x43cc
leaq 0x100(%rsp), %rdi
callq 0x43cc
jmp 0x3051
movq %rax, %rbx
jmp 0x30a4
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x307b
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x307b
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3097
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xd8(%rsp), %rdi
callq 0x43cc
leaq 0x60(%rsp), %rdi
callq 0x441a
leaq 0x120(%rsp), %rdi
callq 0x441a
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x30e0
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22c0
movq %rax, %rbx
jmp 0x30bb