// File: /workspace/github_cpp_makefile_star2_O2/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 $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
movl %edi, %ebp
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
callq 0x3010
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x243c
cmpl $0x4, 0x104(%rsp)
je 0x2c09
movq 0x5839(%rip), %rdi      # 0x7ff0
leaq 0x2c1d(%rip), %rsi      # 0x53db
callq 0x2140
leaq 0xe0(%rsp), %rsi
movq %rax, %rdi
callq 0x2130
movq %rax, %rdi
callq 0x2090
xorps %xmm0, %xmm0
leaq 0x110(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
leaq 0xe0(%rsp), %rdi
callq 0x303b
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r14
movq 0x8(%r15), %rbx
movq 0x57d3(%rip), %rdi      # 0x7ff0
leaq 0x2bcf(%rip), %rsi      # 0x53f3
callq 0x2140
movq %rax, %rdi
callq 0x2090
leaq 0x60(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
movl $0x80, %esi
callq 0x3426
movslq %ebx, %rbx
leaq 0x98(%rsp), %rdi
leaq 0x60(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movq %rbx, %rsi
callq 0x3478
leaq 0x60(%rsp), %rdi
callq 0x412a
leaq 0x10(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x34ce
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x60(%rsp), %rdi
pushq $0x3f
popq %rdx
movq %rbx, %rsi
callq 0x2180
cmpl $0x3, 0x104(%rsp)
jne 0x28d4
movq 0x5742(%rip), %rdi      # 0x7ff0
leaq 0x2b4f(%rip), %rsi      # 0x5404
callq 0x2140
movq %rax, %rdi
callq 0x2090
leaq 0x98(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x359f
movq 0x5715(%rip), %rdi      # 0x7ff0
leaq 0x2b36(%rip), %rsi      # 0x5418
callq 0x2140
movq %rax, %rdi
callq 0x2090
leaq 0xd0(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0xc0(%rsp), %rdi
pushq $0x3f
popq %rdx
movq %rbx, %rsi
callq 0x2180
subq %r15, %r14
shrq $0x5, %r14
cmpl $0x2, 0x104(%rsp)
jne 0x2939
leaq 0xc0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x366e
jmp 0x2965
cvtsi2ss %r14d, %xmm0
mulss 0x108(%rsp), %xmm0
callq 0x20b0
cvttss2si %xmm0, %edx
leaq 0xc0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x366e
movq 0x5684(%rip), %rdi      # 0x7ff0
leaq 0x2ab4(%rip), %rsi      # 0x5427
callq 0x2140
movq %rax, %rdi
callq 0x2090
leaq 0x30(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x48(%rsp), %rdi
leaq 0xc(%rsp), %rcx
movl $0x80, %esi
callq 0x37f6
leaq 0x80(%rsp), %rdi
leaq 0x48(%rsp), %rdx
leaq 0xa(%rsp), %rcx
movl $0x80, %esi
callq 0x3478
leaq 0x48(%rsp), %rdi
callq 0x412a
movslq %r14d, %rbx
leaq 0xc(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x30(%rsp), %rdi
leaq 0xa(%rsp), %rcx
movq %rbx, %rsi
callq 0x37f6
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rdx
leaq 0xb(%rsp), %rcx
movq %rbx, %rsi
callq 0x3478
leaq 0x30(%rsp), %rdi
callq 0x412a
cmpl $0x3, 0x104(%rsp)
jne 0x2a60
movq 0x55df(%rip), %rdi      # 0x7ff0
leaq 0x2a29(%rip), %rsi      # 0x5441
callq 0x2140
movq %rax, %rdi
callq 0x2090
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
je 0x2a60
xorl %edx, %edx
movslq 0x8(%rax), %rsi
cmpq %rsi, %rdx
jge 0x2a5a
movq (%rax), %rsi
cmpb $0x4e, (%rsi,%rdx)
jne 0x2a55
movq 0x60(%rsp), %rdi
movb (%rdi,%rdx), %dil
movb %dil, (%rsi,%rdx)
incq %rdx
jmp 0x2a36
addq $0x20, %rax
jmp 0x2a2f
xorl %ebx, %ebx
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
movq %rax, 0xb0(%rsp)
pushq $0x1
popq %rax
movq %rax, 0x28(%rsp)
leaq 0x80(%rsp), %r13
cmpq 0xb0(%rsp), %rbx
je 0x2b65
movq %rbx, %r12
shlq $0x5, %r12
imulq $0x18, %rbx, %rax
movq %rax, 0xb8(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq %rbp, 0x28(%rsp)
je 0x2b58
movq 0x10(%rsp), %rsi
leaq (%rsi,%r12), %rdi
addq %r14, %rsi
leaq 0xc0(%rsp), %rdx
movq %r13, %rcx
callq 0x384c
movl 0x100(%rsp), %eax
cmpl $0x1, %eax
je 0x2afe
testl %eax, %eax
jne 0x2bea
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x3972
jmp 0x2b0b
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
callq 0x3a73
cmpl $0x1, 0x104(%rsp)
jne 0x2b21
leaq 0x30(%rsp), %rdi
callq 0x2741
jmp 0x2b25
movl 0x34(%rsp), %eax
movq 0x48(%rsp), %rcx
movq (%rcx,%r15), %rdx
movl %eax, (%rdx,%rbx,4)
movq 0xb8(%rsp), %rsi
movq (%rcx,%rsi), %rcx
movl %eax, (%rcx,%rbp,4)
incq %rbp
addq $0x20, %r14
addq $0x18, %r15
leaq 0x80(%rsp), %r13
jmp 0x2aaf
incq %rbx
incq 0x28(%rsp)
jmp 0x2a86
leaq 0x48(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x3b62
leaq 0x48(%rsp), %rdi
callq 0x3cc4
leaq 0x80(%rsp), %rdi
callq 0x3cc4
leaq 0xc0(%rsp), %rdi
callq 0x22d8
leaq 0x60(%rsp), %rdi
callq 0x22d8
leaq 0x98(%rsp), %rdi
callq 0x3cc4
leaq 0x10(%rsp), %rdi
callq 0x3ce6
leaq 0x110(%rsp), %rdi
callq 0x3ce6
leaq 0xe0(%rsp), %rdi
callq 0x22d8
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x286f(%rip), %rdi      # 0x5460
leaq 0x26ee(%rip), %rsi      # 0x52e6
leaq 0x27bf(%rip), %rcx      # 0x53be
movl $0x255, %edx            # imm = 0x255
callq 0x20c0
leaq 0x277d(%rip), %rdi      # 0x538d
leaq 0x26cf(%rip), %rsi      # 0x52e6
leaq 0x27a0(%rip), %rcx      # 0x53be
movl $0x209, %edx            # imm = 0x209
callq 0x20c0
jmp 0x2c8a
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x412a
jmp 0x2c76
movq %rax, %rbx
jmp 0x2c76
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x412a
jmp 0x2c8d
jmp 0x2c8a
jmp 0x2c9c
movq %rax, %rbx
jmp 0x2ca9
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x412a
jmp 0x2cb6
jmp 0x2c85
jmp 0x2c85
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3cc4
leaq 0x80(%rsp), %rdi
callq 0x3cc4
jmp 0x2c8d
movq %rax, %rbx
jmp 0x2cb6
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x22d8
jmp 0x2c9f
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x22d8
leaq 0x98(%rsp), %rdi
callq 0x3cc4
leaq 0x10(%rsp), %rdi
callq 0x3ce6
leaq 0x110(%rsp), %rdi
callq 0x3ce6
leaq 0xe0(%rsp), %rdi
callq 0x22d8
movq %rbx, %rdi
callq 0x2290
movq %rax, %rbx
jmp 0x2ccd