// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/levdist.c
// Function: wf_print_alignment
void wf_print_alignment(char *ts, int tl, char *qs, int ql, uint32_t *cigar, int32_t n_cigar, int lwd, FILE *fo)
{
    if (!cigar || !n_cigar)
        return;
    
    int32_t i, ti, qi;
    uint32_t cl, l;
    uint8_t c;
    kstring_t t_seq = {0, 0, 0}, q_seq = {0, 0, 0}, a_seq = {0, 0, 0};
    ti = qi = l = 0;
    for (i = 0; i < n_cigar; ++i) {
        c = "MIDNSHP=XB"[cigar[i]&0xf];
        cl = cigar[i]>>4;
        switch (c) {
            case 'I':
                kputcn_('-', cl, &t_seq);
                kputsn_(&qs[qi], cl, &q_seq);
                kputcn_(' ', cl, &a_seq);
                qi += cl;
                break;
            case 'D':
                kputsn_(&ts[ti], cl, &t_seq);
                kputcn_('-', cl, &q_seq);
                kputcn_(' ', cl, &a_seq);
                ti += cl;
                break;
            case 'M':
            case '=':
            case 'X':
                kputsn_(&ts[ti], cl, &t_seq);
                if (c == 'M')
                    for (l = 0; l < cl; ++l)
                        kputc_(ts[ti+l] == qs[qi+l]? '|' : '*', &a_seq);
                else
                    kputcn_(c == '='? '|' : '*', cl, &a_seq);
                kputsn_(&qs[qi], cl, &q_seq);
                ti += cl;
                qi += cl;
                break;
            default:
                fprintf(stderr, "[E::%s] unsupported cigar element '%c'\n", __func__, c);
                goto do_clean;
        }
    }

    if (lwd <= 0) {
        fprintf(fo, "target: %.*s\n", (int) t_seq.l, t_seq.s);
        fprintf(fo, "        %.*s\n", (int) a_seq.l, a_seq.s);
        fprintf(fo, " query: %.*s\n", (int) q_seq.l, q_seq.s);
    } else {
        uint32_t j, t_beg, t_end, q_beg, q_end;
        int b = 0;
        l = t_seq.l;
        while (l > 0) { l /= 10; ++b; }
        kstring_t pad = {0, 0, 0};
        kputcn_(' ', b + b + 14, &pad);
        l = 0;
        t_beg = q_beg = 0;
        while (l < t_seq.l) {
            cl = l;
            l += lwd;
            if (l > t_seq.l)
                l = t_seq.l;
            t_end = t_beg + l - cl;
            q_end = q_beg + l - cl;
            for (j = cl; j < l; ++j) {
                t_end -= (t_seq.s[j] == '-');
                q_end -= (q_seq.s[j] == '-');
            }
            fprintf(fo, "target [%*.u - %-*.u]: %.*s\n", b, t_beg + 1, b, t_end, (int) (l - cl), &t_seq.s[cl]);
            fprintf(fo, "%.*s%.*s\n", (int) pad.l, pad.s, (int) (l - cl), &a_seq.s[cl]);
            fprintf(fo, " query [%*.u - %-*.u]: %.*s\n", b, q_beg + 1, b, q_end, (int) (l - cl), &q_seq.s[cl]);
            fputc('\n', fo);
            t_beg = t_end;
            q_beg = q_end;
        }
        free(pad.s);
    }

do_clean:
    free(t_seq.s); free(q_seq.s); free(a_seq.s);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq %r8, 0x90(%rsp)
testq %r8, %r8
sete %al
testl %r9d, %r9d
sete %cl
orb %al, %cl
jne 0x11c9f
movl %r9d, %r15d
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r13
movaps %xmm0, (%r13)
andq $0x0, 0x10(%r13)
leaq 0x50(%rsp), %rbp
movaps %xmm0, (%rbp)
andq $0x0, 0x10(%rbp)
leaq 0x30(%rsp), %rax
andq $0x0, 0x10(%rax)
xorl %edx, %edx
testl %r9d, %r9d
cmovlel %edx, %r15d
movaps %xmm0, (%rax)
xorl %esi, %esi
xorl %r8d, %r8d
movq %r15, 0x88(%rsp)
cmpq %r15, %rdx
je 0x11a88
movq 0x90(%rsp), %rax
movl (%rax,%rdx,4), %edi
movl %edi, %r14d
andl $0xf, %r14d
leaq 0x9266(%rip), %rax      # 0x1aadd
movzbl (%r14,%rax), %ecx
shrl $0x4, %edi
movq %rdi, (%rsp)
movl %esi, 0x20(%rsp)
movslq %esi, %rbx
movslq %r8d, %r12
cmpl $0x3d, %ecx
movq %rdx, 0x48(%rsp)
je 0x118f5
cmpl $0x44, %ecx
je 0x11a3a
cmpl $0x58, %ecx
je 0x118f5
cmpl $0x4d, %ecx
je 0x118f5
cmpl $0x49, %ecx
jne 0x11cb1
pushq $0x2d
popq %rdi
movq (%rsp), %rbx
movl %ebx, %esi
movq %r13, %rdx
callq 0x11ce4
movq 0x18(%rsp), %rax
leaq (%rax,%r12), %rdi
movl %ebx, %esi
movq %rbp, %rdx
callq 0x11d63
pushq $0x20
popq %rdi
movl %ebx, %esi
leaq 0x30(%rsp), %rdx
callq 0x11ce4
addl %ebx, %r12d
movl %r12d, %r8d
movl 0x20(%rsp), %esi
jmp 0x11a7b
movq %r12, 0x98(%rsp)
movl %r8d, %r12d
addq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq (%rsp), %rbx
movl %ebx, %esi
movq %r13, %rdx
callq 0x11d63
testl %r14d, %r14d
je 0x11939
cmpl $0x7, %r14d
pushq $0x2a
popq %rdi
pushq $0x7c
popq %rax
cmovel %eax, %edi
movl %ebx, %esi
leaq 0x30(%rsp), %rdx
callq 0x11ce4
jmp 0x11a11
movq 0x30(%rsp), %r13
movq %rbx, %rbp
movq 0x38(%rsp), %rbx
movq 0x40(%rsp), %rax
movl 0x20(%rsp), %r14d
movl %r12d, %r15d
subq $0x1, %rbp
jb 0x119ec
movl %r14d, %ecx
movq 0x10(%rsp), %rdx
movb (%rdx,%rcx), %dil
movl %r15d, %ecx
movq 0x18(%rsp), %rdx
movb (%rdx,%rcx), %cl
leaq 0x1(%r13), %r12
cmpq %rbx, %r12
jbe 0x119ce
movb %cl, 0x28(%rsp)
movq %r13, %rcx
shrq %rcx
orq %r13, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %rbx
shrq $0x10, %rbx
orq %rdx, %rbx
incq %rbx
movb %dil, 0xc(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2350
movb 0x28(%rsp), %cl
movb 0xc(%rsp), %dil
cmpb %cl, %dil
pushq $0x2a
popq %rcx
pushq $0x7c
popq %rdx
cmovel %edx, %ecx
movb %cl, (%rax,%r13)
incl %r15d
incl %r14d
movq %r12, %r13
jmp 0x11953
movq %rbx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq %r13, 0x30(%rsp)
movq 0x88(%rsp), %r15
leaq 0x70(%rsp), %r13
leaq 0x50(%rsp), %rbp
movq (%rsp), %rbx
movq 0x18(%rsp), %rax
movq 0x98(%rsp), %r14
leaq (%rax,%r14), %rdi
movl %ebx, %esi
movq %rbp, %rdx
callq 0x11d63
movl 0x20(%rsp), %esi
addl %ebx, %esi
addl %ebx, %r14d
movl %r14d, %r8d
jmp 0x11a7b
movq 0x10(%rsp), %rax
leaq (%rax,%rbx), %rdi
movq (%rsp), %r14
movl %r14d, %esi
movq %r13, %rdx
movl %r8d, %r12d
callq 0x11d63
pushq $0x2d
popq %rdi
movl %r14d, %esi
movq %rbp, %rdx
callq 0x11ce4
pushq $0x20
popq %rdi
movl %r14d, %esi
leaq 0x30(%rsp), %rdx
callq 0x11ce4
movl %r12d, %r8d
addl %r14d, %ebx
movl %ebx, %esi
movq 0x48(%rsp), %rdx
incq %rdx
jmp 0x11855
movq 0x70(%rsp), %r12
movl 0xf0(%rsp), %eax
testl %eax, %eax
jle 0x11c26
xorl %edi, %edi
pushq $0xe
popq %rsi
pushq $0xa
popq %rcx
movl %r12d, %eax
testl %eax, %eax
je 0x11ab6
xorl %edx, %edx
divl %ecx
incl %edi
addl $0x2, %esi
jmp 0x11aa7
movl %edi, 0x10(%rsp)
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rbx
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
pushq $0x20
popq %rdi
movq %rbx, %rdx
callq 0x11ce4
movq 0x80(%rsp), %r14
movq 0x60(%rsp), %r13
movl (%rbx), %eax
movl %eax, 0x48(%rsp)
movq 0x10(%rbx), %rax
movq %rax, (%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %edx, %edx
movq %r12, 0x20(%rsp)
movl %ebp, %r15d
cmpq %r15, %r12
jbe 0x11c16
movl 0xf0(%rsp), %eax
leal (%r15,%rax), %ebp
cmpq %rbp, %r12
cmovbl %r12d, %ebp
movl %ecx, %ebx
subl %r15d, %ebx
addl %ebp, %ebx
movl %edx, 0xc(%rsp)
movl %edx, %r12d
subl %r15d, %r12d
addl %ebp, %r12d
movq %r15, %rax
cmpq %rbp, %rax
jae 0x11b61
xorl %edx, %edx
cmpb $0x2d, (%r14,%rax)
sete %dl
subl %edx, %ebx
xorl %edx, %edx
cmpb $0x2d, (%r13,%rax)
sete %dl
subl %edx, %r12d
incq %rax
jmp 0x11b3d
incl %ecx
movl %ebp, %eax
subl %r15d, %eax
movq %rax, %r11
movq %rax, 0x18(%rsp)
leaq (%r14,%r15), %r10
movq 0xf8(%rsp), %rdi
leaq 0x8fca(%rip), %rsi      # 0x1ab4d
movl 0x10(%rsp), %edx
movl %edx, %r8d
movl %ebx, %r9d
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x2280
popq %rcx
popq %rdx
movq 0x28(%rsp), %rax
leaq (%rax,%r15), %r9
movq 0xf8(%rsp), %rdi
leaq 0x8fb8(%rip), %rsi      # 0x1ab6a
movl 0x48(%rsp), %edx
movq (%rsp), %rcx
movq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x2280
movl 0xc(%rsp), %ecx
incl %ecx
addq %r13, %r15
movq 0xf8(%rsp), %rdi
leaq 0x8f96(%rip), %rsi      # 0x1ab74
movl 0x10(%rsp), %edx
movl %edx, %r8d
movl %r12d, %r9d
xorl %eax, %eax
pushq %r15
pushq 0x20(%rsp)
callq 0x2280
popq %rcx
popq %rdx
pushq $0xa
popq %rdi
movq 0xf8(%rsp), %rsi
callq 0x2210
movl %ebx, %ecx
movl %r12d, %edx
movq 0x20(%rsp), %r12
jmp 0x11b08
movq (%rsp), %rdi
callq 0x2060
movq 0x28(%rsp), %r15
jmp 0x11c87
movq 0x80(%rsp), %r14
leaq 0x8eee(%rip), %rsi      # 0x1ab23
movq 0xf8(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2280
movl 0x30(%rsp), %edx
movq 0x40(%rsp), %r15
leaq 0x8ed4(%rip), %rsi      # 0x1ab31
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x2280
movl 0x50(%rsp), %edx
movq 0x60(%rsp), %r13
leaq 0x8ec5(%rip), %rsi      # 0x1ab3f
movq %rbx, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x2280
movq %r14, %rdi
callq 0x2060
movq %r13, %rdi
callq 0x2060
movq %r15, %rdi
callq 0x2060
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf338(%rip), %rax      # 0x20ff0
movq (%rax), %rdi
leaq 0x8e26(%rip), %rsi      # 0x1aae8
leaq 0x8e47(%rip), %rdx      # 0x1ab10
xorl %eax, %eax
callq 0x2280
movq 0x80(%rsp), %r14
movq 0x60(%rsp), %r13
movq 0x40(%rsp), %r15
jmp 0x11c87