// File: /workspace/github_cpp_makefile_star2_O3/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 0x2f97
movq 0x56c5(%rip), %rdi      # 0x7ff0
leaq 0x2add(%rip), %rsi      # 0x540f
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 0x33b7
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %r13
movq 0x8(%r15), %r14
movq 0x562c(%rip), %rbx      # 0x7ff0
leaq 0x2a5c(%rip), %rsi      # 0x5427
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, %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 0x395e
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2a7c
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
leaq 0x60(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x39f5
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 0x2b0b
movq 0x5536(%rip), %r14      # 0x7ff0
leaq 0x2977(%rip), %rsi      # 0x5438
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 0x5504(%rip), %rdi      # 0x7ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
leaq 0xd8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3afc
movq 0x54de(%rip), %r14      # 0x7ff0
leaq 0x2933(%rip), %rsi      # 0x544c
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 0x54ac(%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 0x2b9f
leaq 0x88(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x1, %edx
callq 0x3bd8
jmp 0x2bcb
cvtsi2ss %r13d, %xmm0
mulss 0xd0(%rsp), %xmm0
callq 0x20d0
cvttss2si %xmm0, %edx
leaq 0x88(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x3bd8
movq 0x541e(%rip), %rbx      # 0x7ff0
leaq 0x2882(%rip), %rsi      # 0x545b
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 0x53ec(%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, %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 0x395e
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2c86
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 0x3dc2
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0x6(%rsp), %rcx
movq %rbx, %rsi
callq 0x395e
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x2cd7
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
cmpl $0x3, 0xcc(%rsp)
jne 0x2d6d
movq 0x5304(%rip), %rbx      # 0x7ff0
leaq 0x2782(%rip), %rsi      # 0x5475
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 0x52d2(%rip), %rdi      # 0x7ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
cmpq %rcx, %rax
je 0x2d6d
movq 0x8(%rax), %rdx
testl %edx, %edx
jle 0x2d67
xorl %esi, %esi
movq (%rax), %rdi
cmpb $0x4e, (%rdi,%rsi)
jne 0x2d5c
movq 0x20(%rsp), %rdx
movb (%rdx,%rsi), %dl
movb %dl, (%rdi,%rsi)
movq 0x8(%rax), %rdx
incq %rsi
movslq %edx, %rdi
cmpq %rdi, %rsi
jl 0x2d44
addq $0x20, %rax
jmp 0x2d35
testl %r13d, %r13d
jle 0x2eb1
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 0x3f3e
movl 0xc8(%rsp), %eax
cmpl $0x1, %eax
je 0x2e03
testl %eax, %eax
jne 0x2f78
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x407b
jmp 0x2e10
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x417f
cmpl $0x1, 0xcc(%rsp)
jne 0x2e54
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 0x2e58
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 0x2dbf
incq %rbp
incq 0x80(%rsp)
movq 0xf0(%rsp), %r13
cmpq %r13, %rbp
jne 0x2d9c
leaq 0x40(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x426c
leaq 0x40(%rsp), %rdi
callq 0x442a
leaq 0x100(%rsp), %rdi
callq 0x442a
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2eff
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2f1b
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xd8(%rsp), %rdi
callq 0x442a
leaq 0x60(%rsp), %rdi
callq 0x447e
leaq 0x120(%rsp), %rdi
callq 0x447e
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x2f64
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 0x2515(%rip), %rdi      # 0x5494
leaq 0x2394(%rip), %rsi      # 0x531a
leaq 0x2465(%rip), %rcx      # 0x53f2
movl $0x255, %edx            # imm = 0x255
callq 0x20e0
leaq 0x2423(%rip), %rdi      # 0x53c1
leaq 0x2375(%rip), %rsi      # 0x531a
leaq 0x2446(%rip), %rcx      # 0x53f2
movl $0x209, %edx            # imm = 0x209
callq 0x20e0
jmp 0x3049
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x3035
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x3035
movq %rax, %rbx
jmp 0x3035
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x304c
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x304c
jmp 0x3049
jmp 0x3073
movq %rax, %rbx
jmp 0x3092
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x309f
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x309f
jmp 0x3044
jmp 0x3044
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x442a
leaq 0x100(%rsp), %rdi
callq 0x442a
jmp 0x304c
movq %rax, %rbx
jmp 0x309f
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3076
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x3076
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3092
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0xd8(%rsp), %rdi
callq 0x442a
leaq 0x60(%rsp), %rdi
callq 0x447e
leaq 0x120(%rsp), %rdi
callq 0x447e
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x30db
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22c0
movq %rax, %rbx
jmp 0x30b6