// File: /workspace/github_c_makefile_star2_O0/nh13[P]ksw/src/parasail/apps/parasail_aligner.cpp
// Function: output_sam(_IO_FILE*, bool, bool, long, parasail_matrix const*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>> const&, parasail_sequences*, parasail_sequences*, std::vector<parasail_result*, std::allocator<parasail_result*>> const&, long long, long long, bool, char const*)
inline static void output_sam(
        FILE *fop,
        bool use_sam_header,
        bool has_query,
        long sid_crossover,
        const parasail_matrix_t *matrix,
        const PairVec &vpairs,
        parasail_sequences_t *queries,
        parasail_sequences_t *sequences,
        const vector<parasail_result_t*> &results,
        long long start,
        long long stop,
        bool case_sensitive,
        const char *alphabet_aliases)
{
    if (use_sam_header && has_query && 0 == start) {
        fprintf(fop, "@HD\tVN:1.4\tSO:queryname\n");
        for (size_t index=0; index<sequences->l; ++index) {
            parasail_sequence_t ref_seq = sequences->seqs[index];
            fprintf(fop, "@SQ\tSN:%s\tLN:%d\n",
                    ref_seq.name.s, (int32_t)ref_seq.seq.l);
        }
    }
    for (long long index=start; index<stop; ++index) {
        parasail_result_t *result = results[index];
        int i = vpairs[index].first;
        int j = vpairs[index].second;
        parasail_sequence_t ref_seq;
        parasail_sequence_t read_seq;

        ref_seq = sequences->seqs[j];
        if (has_query) {
            i = i - sid_crossover;
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "query %d (%s) and ref %d (%s) saturated\n",
                        i, queries->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            read_seq = queries->seqs[i];
        }
        else {
            if (parasail_result_is_saturated(result)) {
                fprintf(stderr, "seq %d (%s) and seq %d (%s) saturated\n",
                        i, sequences->seqs[i].name.s,
                        j, sequences->seqs[j].name.s);
                continue;
            }
            read_seq = sequences->seqs[i];
        }

        fprintf(fop, "%s\t", read_seq.name.s);
        if (result->score == 0) {
            fprintf(fop, "4\t*\t0\t255\t*\t*\t0\t0\t*\t*\n");
        }
        else {
            int32_t c = 0;
            int32_t length = 0;
            uint32_t mapq = 255; /* not available */
            parasail_cigar_t *cigar = NULL;
            uint32_t mismatch = 0;

            cigar = parasail_result_get_cigar_extra(
                    result,
                    read_seq.seq.s, read_seq.seq.l,
                    ref_seq.seq.s, ref_seq.seq.l,
                    matrix,
                    case_sensitive,
                    alphabet_aliases);

            fprintf(fop, "0\t");
            fprintf(fop, "%s\t%d\t%d\t",
                    ref_seq.name.s, cigar->beg_ref + 1, mapq);
            if (parasail_result_is_sw(result)) {
                if (cigar->beg_query > 0) {
                    fprintf(fop, "%dS", cigar->beg_query);
                }
            }
            for (c=0; c<cigar->len; ++c) {
                char letter = parasail_cigar_decode_op(cigar->seq[c]);
                uint32_t length = parasail_cigar_decode_len(cigar->seq[c]);
                fprintf(fop, "%lu%c", (unsigned long)length, letter);
                if ('X' == letter || 'I' == letter || 'D' == letter) {
                    mismatch += length;
                }
            }

            length = read_seq.seq.l - result->end_query - 1;
            if (parasail_result_is_sw(result)) {
                if (length > 0) {
                    fprintf(fop, "%dS", length);
                }
            }
            fprintf(fop, "\t*\t0\t0\t");
            fprintf(fop, "%s", read_seq.seq.s);
            fprintf(fop, "\t");
            if (read_seq.qual.s) {
                fprintf (fop, "%s", read_seq.qual.s);
            }
            else {
                fprintf(fop, "*");
            }
            fprintf(fop, "\tAS:i:%d", result->score);
            fprintf(fop,"\tNM:i:%d\t", mismatch);
            fprintf(fop, "\n");

            parasail_cigar_free(cigar);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq 0x40(%rbp), %rax
movb 0x38(%rbp), %al
movq 0x30(%rbp), %r10
movq 0x28(%rbp), %r10
movq 0x20(%rbp), %r10
movq 0x18(%rbp), %r10
movq 0x10(%rbp), %r10
movq %rdi, -0x8(%rbp)
andb $0x1, %sil
movb %sil, -0x9(%rbp)
andb $0x1, %dl
movb %dl, -0xa(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
testb $0x1, -0x9(%rbp)
je 0x2b7535
testb $0x1, -0xa(%rbp)
je 0x2b7535
xorl %eax, %eax
cmpq 0x28(%rbp), %rax
jne 0x2b7535
movq -0x8(%rbp), %rdi
leaq 0xaf8457(%rip), %rsi    # 0xdaf923
movb $0x0, %al
callq 0x2af440
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2b7533
movq 0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
leaq -0x78(%rbp), %rdi
movl $0x40, %edx
callq 0x2af230
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
leaq 0xaf841e(%rip), %rsi    # 0xdaf93c
movb $0x0, %al
callq 0x2af440
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2b74db
jmp 0x2b7535
movq 0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq 0x30(%rbp), %rax
jge 0x2b7a33
movq 0x20(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x2bdca0
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x2bdcc0
movl (%rax), %eax
movl %eax, -0x8c(%rbp)
movq -0x28(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x2bdcc0
movl 0x4(%rax), %eax
movl %eax, -0x90(%rbp)
movq 0x18(%rbp), %rax
movq (%rax), %rsi
movslq -0x90(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
leaq -0xd0(%rbp), %rdi
movl $0x40, %edx
callq 0x2af230
testb $0x1, -0xa(%rbp)
je 0x2b7666
movslq -0x8c(%rbp), %rax
subq -0x18(%rbp), %rax
movl %eax, -0x8c(%rbp)
movq -0x88(%rbp), %rdi
callq 0x2daa80
cmpl $0x0, %eax
je 0x2b763b
movq 0xcc39ea(%rip), %rax    # 0xf7afd0
movq (%rax), %rdi
movl -0x8c(%rbp), %edx
movq 0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movl -0x90(%rbp), %r8d
movq 0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x90(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rax
movq 0x8(%rax), %r9
leaq 0xaf820f(%rip), %rsi    # 0xdaf83e
movb $0x0, %al
callq 0x2af440
jmp 0x2b7a22
movq 0x10(%rbp), %rax
movq (%rax), %rsi
movslq -0x8c(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
leaq -0x110(%rbp), %rdi
movl $0x40, %edx
callq 0x2af230
jmp 0x2b76f9
movq -0x88(%rbp), %rdi
callq 0x2daa80
cmpl $0x0, %eax
je 0x2b76d3
movq 0xcc3952(%rip), %rax    # 0xf7afd0
movq (%rax), %rdi
movl -0x8c(%rbp), %edx
movq 0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movl -0x90(%rbp), %r8d
movq 0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x90(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rax
movq 0x8(%rax), %r9
leaq 0xaf81a0(%rip), %rsi    # 0xdaf867
movb $0x0, %al
callq 0x2af440
jmp 0x2b7a22
movq 0x18(%rbp), %rax
movq (%rax), %rsi
movslq -0x8c(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
leaq -0x110(%rbp), %rdi
movl $0x40, %edx
callq 0x2af230
movq -0x8(%rbp), %rdi
movq -0x108(%rbp), %rdx
leaq 0xaf8242(%rip), %rsi    # 0xdaf94d
movb $0x0, %al
callq 0x2af440
movq -0x88(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x2b7735
movq -0x8(%rbp), %rdi
leaq 0xaf8228(%rip), %rsi    # 0xdaf951
movb $0x0, %al
callq 0x2af440
jmp 0x2b7a20
movl $0x0, -0x114(%rbp)
movl $0x0, -0x118(%rbp)
movl $0xff, -0x11c(%rbp)
movq $0x0, -0x128(%rbp)
movl $0x0, -0x12c(%rbp)
movq -0x88(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq -0xf0(%rbp), %rax
movl %eax, %edx
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %rax
movl %eax, %r8d
movq -0x20(%rbp), %r9
movb -0x29(%rbp), %al
andb $0x1, %al
movzbl %al, %r10d
movq 0x40(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x2cb950
movq %rax, -0x128(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xaf81c0(%rip), %rsi    # 0xdaf981
movb $0x0, %al
callq 0x2af440
movq -0x8(%rbp), %rdi
movq -0xc8(%rbp), %rdx
movq -0x128(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl -0x11c(%rbp), %r8d
leaq 0xaf817a(%rip), %rsi    # 0xdaf968
movb $0x0, %al
callq 0x2af440
movq -0x88(%rbp), %rdi
callq 0x2daa20
cmpl $0x0, %eax
je 0x2b7831
movq -0x128(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jle 0x2b782f
movq -0x8(%rbp), %rdi
movq -0x128(%rbp), %rax
movl 0xc(%rax), %edx
leaq 0xaf814a(%rip), %rsi    # 0xdaf972
movb $0x0, %al
callq 0x2af440
jmp 0x2b7831
movl $0x0, -0x114(%rbp)
movl -0x114(%rbp), %eax
movq -0x128(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x2b7908
movq -0x128(%rbp), %rax
movq (%rax), %rax
movslq -0x114(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x2cb7b0
movb %al, -0x12d(%rbp)
movq -0x128(%rbp), %rax
movq (%rax), %rax
movslq -0x114(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x2cb7f0
movl %eax, -0x134(%rbp)
movq -0x8(%rbp), %rdi
movl -0x134(%rbp), %eax
movl %eax, %edx
movsbl -0x12d(%rbp), %ecx
leaq 0xaf80cd(%rip), %rsi    # 0xdaf976
movb $0x0, %al
callq 0x2af440
movsbl -0x12d(%rbp), %ecx
movl $0x58, %eax
cmpl %ecx, %eax
je 0x2b78e0
movsbl -0x12d(%rbp), %ecx
movl $0x49, %eax
cmpl %ecx, %eax
je 0x2b78e0
movsbl -0x12d(%rbp), %ecx
movl $0x44, %eax
cmpl %ecx, %eax
jne 0x2b78f2
movl -0x134(%rbp), %eax
addl -0x12c(%rbp), %eax
movl %eax, -0x12c(%rbp)
jmp 0x2b78f4
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0x2b783b
movq -0xf0(%rbp), %rax
movq -0x88(%rbp), %rcx
movslq 0x4(%rcx), %rcx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, -0x118(%rbp)
movq -0x88(%rbp), %rdi
callq 0x2daa20
cmpl $0x0, %eax
je 0x2b795b
cmpl $0x0, -0x118(%rbp)
jle 0x2b7959
movq -0x8(%rbp), %rdi
movl -0x118(%rbp), %edx
leaq 0xaf8020(%rip), %rsi    # 0xdaf972
movb $0x0, %al
callq 0x2af440
jmp 0x2b795b
movq -0x8(%rbp), %rdi
leaq 0xaf8016(%rip), %rsi    # 0xdaf97c
movb $0x0, %al
callq 0x2af440
movq -0x8(%rbp), %rdi
movq -0xe8(%rbp), %rdx
leaq 0xaf8005(%rip), %rsi    # 0xdaf984
movb $0x0, %al
callq 0x2af440
movq -0x8(%rbp), %rdi
leaq 0xaf7ff1(%rip), %rsi    # 0xdaf982
movb $0x0, %al
callq 0x2af440
cmpq $0x0, -0xd8(%rbp)
je 0x2b79bd
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0xaf7fd0(%rip), %rsi    # 0xdaf984
movb $0x0, %al
callq 0x2af440
jmp 0x2b79cf
movq -0x8(%rbp), %rdi
leaq 0xb5758a(%rip), %rsi    # 0xe0ef52
movb $0x0, %al
callq 0x2af440
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rax
movl (%rax), %edx
leaq 0xaf7fa4(%rip), %rsi    # 0xdaf987
movb $0x0, %al
callq 0x2af440
movq -0x8(%rbp), %rdi
movl -0x12c(%rbp), %edx
leaq 0xaf7f95(%rip), %rsi    # 0xdaf990
movb $0x0, %al
callq 0x2af440
movq -0x8(%rbp), %rdi
leaq 0xaf77bd(%rip), %rsi    # 0xdaf1ca
movb $0x0, %al
callq 0x2af440
movq -0x128(%rbp), %rdi
callq 0x2cb780
jmp 0x2b7a22
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x2b753d
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopl (%rax)