// File: /workspace/github_c_makefile_star2_O1/nh13[P]ksw/src/parasail/apps/parasail_aligner.cpp
// Function: output_graph(_IO_FILE*, int, unsigned char*, int, int, int, parasail_matrix const*, std::vector<long, std::allocator<long>> const&, std::vector<long, std::allocator<long>> const&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>> const&, std::vector<parasail_result*, std::allocator<parasail_result*>> const&, std::vector<std::vector<std::pair<int, float>, std::allocator<std::pair<int, float>>>, std::allocator<std::vector<std::pair<int, float>, std::allocator<std::pair<int, float>>>>>&, unsigned long&, long long, long long)
inline static void output_graph(
        FILE *fop,
        int which,
        unsigned char *T,
        int AOL,
        int SIM,
        int OS,
        const parasail_matrix_t *matrix,
        const vector<long> &BEG,
        const vector<long> &END,
        const PairVec &vpairs,
        const vector<parasail_result_t*> &results,
        vector<vector<pair<int,float> > > &graph,
        unsigned long &edge_count,
        long long start,
        long long stop)
{
    if (NULL == fop) {
        for (long long index=start; index<stop; ++index) {
            parasail_result_t *result = results[index];
            int i = vpairs[index].first;
            int j = vpairs[index].second;
            long i_beg = BEG[i];
            long i_end = END[i];
            long i_len = i_end-i_beg;
            long j_beg = BEG[j];
            long j_end = END[j];
            long j_len = j_end-j_beg;

            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "seq %d and seq %d saturated\n", i, j);
                continue;
            }

            int score = parasail_result_get_score(result);
            int matches = parasail_result_get_matches(result);
            int length = parasail_result_get_length(result);
            int self_score_ = 0;
            int max_len = 0;
            int i_self_score = self_score(
                    (const char*)&T[i_beg], i_len, matrix);
            int j_self_score = self_score(
                    (const char*)&T[j_beg], j_len, matrix);

            if (i_len > j_len) {
                max_len = i_len;
                self_score_ = i_self_score;
            }
            else {
                max_len = j_len;
                self_score_ = j_self_score;
            }

            if ((length * 100 >= AOL * int(max_len))
                    && (matches * 100 >= SIM * length)
                    && (score * 100 >= OS * self_score_)) {
                float value;
                ++edge_count;
                switch (which) {
                    case 0:
                        value = 1.0*length/max_len;
                        break;
                    case 1:
                        value = 1.0*matches/length;
                        break;
                    case 2:
                        value = 1.0*score/self_score_;
                        break;
                }
                graph[i].push_back(make_pair(j,value));
                graph[j].push_back(make_pair(i,value));
            }
        }
    }
    else {
        fprintf(fop, "%lu %lu 1\n", (unsigned long)graph.size(), edge_count);
        for (size_t i=0; i<graph.size(); ++i) {
            if (graph[i].size() > 0) {
                fprintf(fop, "%d %f", graph[i][0].first+1, graph[i][0].second);
                for (size_t j=1; j<graph[i].size(); ++j) {
                    fprintf(fop, "  %d %f", graph[i][j].first+1, graph[i][j].second);
                }
                fprintf(fop, "\n");
            } else {
                fprintf(fop, "\n");
            }
        }
        fprintf(stdout, "%20s: %lu\n", "edges count", edge_count);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %r15
testq %rdi, %rdi
movq %rax, 0x18(%rsp)
je 0x30158a
movq %rdi, %r13
movq 0x8(%r15), %rdx
subq (%r15), %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
movq (%rax), %rcx
leaq 0x3a2358(%rip), %rsi    # 0x6a37d5
xorl %eax, %eax
callq 0x29e380
movq (%r15), %rax
cmpq %rax, 0x8(%r15)
je 0x301555
xorl %ebp, %ebp
leaq 0x3a2346(%rip), %r14    # 0x6a37e0
leaq (,%rbp,2), %rbx
addq %rbp, %rbx
movq (%rax,%rbx,8), %rcx
cmpq %rcx, 0x8(%rax,%rbx,8)
je 0x301520
movl (%rcx), %edx
incl %edx
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rcx), %xmm0
movq %r13, %rdi
leaq 0x3a231c(%rip), %rsi    # 0x6a37e2
movb $0x1, %al
callq 0x29e380
movq (%r15), %rcx
movq (%rcx,%rbx,8), %rax
movq 0x8(%rcx,%rbx,8), %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0x301520
movl $0x1, %r12d
movl (%rax,%r12,8), %edx
incl %edx
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rax,%r12,8), %xmm0
movq %r13, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x29e380
incq %r12
movq (%r15), %rcx
movq (%rcx,%rbx,8), %rax
movq 0x8(%rcx,%rbx,8), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jb 0x3014e8
movl $0xa, %edi
movq %r13, %rsi
callq 0x29e300
incq %rbp
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %rbp
jb 0x30149a
movq 0x5daa4c(%rip), %rax    # 0x8dbfa8
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rcx
leaq 0x3a1528(%rip), %rsi    # 0x6a2a96
leaq 0x3a2273(%rip), %rdx    # 0x6a37e8
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29e380
movq %rsi, 0x30(%rsp)
movl %edx, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movl %r8d, 0x4(%rsp)
movq 0xe8(%rsp), %rax
movq 0xe0(%rsp), %rcx
movq %rax, 0x68(%rsp)
cmpq %rax, %rcx
jge 0x3017fb
movq 0xc8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %r9, 0x40(%rsp)
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rcx,8), %r12
movq 0x58(%rsp), %rax
movq (%rax), %rax
movslq (%rax,%rcx,8), %rsi
movq %rcx, 0x70(%rsp)
movslq 0x4(%rax,%rcx,8), %rdx
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rsi,8), %r14
movq 0x50(%rsp), %rcx
movq (%rcx), %rcx
movq %rsi, 0x20(%rsp)
movq (%rcx,%rsi,8), %r13
movq (%rax,%rdx,8), %rbp
movq %rdx, 0x28(%rsp)
movq (%rcx,%rdx,8), %rbx
movq %r12, %rdi
callq 0x30f1c5
testl %eax, %eax
jne 0x3017c6
subq %r14, %r13
movq %r12, %rdi
callq 0x30f317
movl %eax, 0x10(%rsp)
movq %r12, %rdi
callq 0x30f358
movl %eax, 0x14(%rsp)
movq %r12, %rdi
callq 0x30f3a1
xorl %ecx, %ecx
movl $0x0, %edx
testl %r13d, %r13d
movq 0x40(%rsp), %r12
jle 0x3016c6
addq 0x30(%rsp), %r14
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdi
movl 0x18(%r12), %r8d
incl %r8d
movl %r13d, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %r10d, %r10d
xorl %edx, %edx
movzbl (%r14,%r10), %r11d
movzbl (%rdi,%r11,4), %r11d
imull %r8d, %r11d
movslq %r11d, %r11
addl (%rsi,%r11,4), %edx
incq %r10
cmpq %r10, %r9
jne 0x3016a9
subq %rbp, %rbx
testl %ebx, %ebx
jle 0x301711
addq 0x30(%rsp), %rbp
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdi
movl 0x18(%r12), %r8d
incl %r8d
movl %ebx, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %r10d, %r10d
xorl %ecx, %ecx
movzbl (%rbp,%r10), %r11d
movzbl (%rdi,%r11,4), %r11d
imull %r8d, %r11d
movslq %r11d, %r11
addl (%rsi,%r11,4), %ecx
incq %r10
cmpq %r10, %r9
jne 0x3016f3
cmpq %rbx, %r13
cmovgl %edx, %ecx
cmovgq %r13, %rbx
imull $0x64, %eax, %edx
movl %ebx, %esi
imull 0xc(%rsp), %esi
cmpl %esi, %edx
jl 0x3017e8
imull $0x64, 0x14(%rsp), %edx
movl %eax, %esi
imull 0x8(%rsp), %esi
cmpl %esi, %edx
jl 0x3017e8
imull $0x64, 0x10(%rsp), %edx
imull 0x4(%rsp), %ecx
cmpl %ecx, %edx
jl 0x3017e8
movq 0x20(%rsp), %rdx
movl %edx, %r14d
movq 0x18(%rsp), %rcx
incq (%rcx)
cvtsi2sd %eax, %xmm0
cvtsi2sd %ebx, %xmm1
movq 0x28(%rsp), %rbp
movl %ebp, %eax
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
leaq (%rdx,%rdx,2), %rdi
shlq $0x3, %rdi
addq (%r15), %rdi
movd %xmm0, %ebx
shlq $0x20, %rbx
orq %rbx, %rax
movq %rax, 0x38(%rsp)
leaq 0x38(%rsp), %r13
movq %r13, %rsi
callq 0x30431c
leaq (,%rbp,2), %rdi
addq %rbp, %rdi
shlq $0x3, %rdi
addq (%r15), %rdi
orq %r14, %rbx
movq %rbx, 0x38(%rsp)
movq %r13, %rsi
callq 0x30431c
jmp 0x3017e8
movq 0x5da813(%rip), %rax    # 0x8dbfe0
movq (%rax), %rdi
leaq 0x3a1fe1(%rip), %rsi    # 0x6a37b8
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0x29e380
movq 0x70(%rsp), %rcx
incq %rcx
cmpq 0x68(%rsp), %rcx
jne 0x3015f3
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq