// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/ecovlp.cpp
// Function: prt_ovlp_sam(overlap_region_alloc*, UC_Read*, char*, int)
void prt_ovlp_sam(overlap_region_alloc* ol, UC_Read* tu, char *ref_seq, int32_t ref_seq_n)
{
    int64_t on = ol->length, k, i, zwn; overlap_region *z; bit_extz_t ez;
    char *qry = NULL, *ref = Get_NAME(R_INF, ol->list[0].x_id); 
    uint64_t qry_n = 0, ref_n = Get_NAME_LENGTH(R_INF, ol->list[0].x_id), qid, rev;
    char cm[4]; cm[0] = 'M'; cm[1] = 'M'; cm[2] = 'I'; cm[3] = 'D';  
    FILE *fp = fopen("aln.sam", "w");
    fprintf(fp, "@HD\tVN:1.6\tSO:unknown\n");
    fprintf(fp, "@SQ\tSN:%.*s\tLN:%lu\n", (int32_t)ref_n, ref, Get_READ_LENGTH(R_INF, ol->list[0].x_id));
    for (k = 0; k < on; k++) {
        z = &(ol->list[k]); zwn = z->w_list.n; 
        if(!zwn) continue; 
        qid = ol->list[k].y_id; 
        if(z->y_pos_strand) {
            recover_UC_Read_RC(tu, &R_INF, qid); rev = 16;
        } else {
            recover_UC_Read(tu, &R_INF, qid); rev = 0;
        }
        for (i = 0; i < zwn; i++) {
            if(is_ualn_win(z->w_list.a[i])) continue;
            qry_n = Get_NAME_LENGTH(R_INF, qid); 
            qry = Get_NAME(R_INF, qid);
            set_bit_extz_t(ez, (*z), i);
            
            prt_ovlp_sam_0(cm, fp, ref, ref_n, qry, qry_n, tu->seq, tu->length, z->w_list.a[i].x_start, z->w_list.a[i].x_end + 1, z->w_list.a[i].y_start, z->w_list.a[i].y_end + 1, rev, z->w_list.a[i].error, &ez);
        }
    }
    fclose(fp);

    fp = fopen("ref.fa", "w");
    fprintf(fp, ">%.*s\n", (int32_t)ref_n, ref);
    fprintf(fp, "%.*s\n", ref_seq_n, ref_seq);
    fclose(fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl %ecx, 0x60(%rsp)
movq %rdx, 0x78(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %r14
movq 0x10(%rdi), %rax
movq %rax, 0x68(%rsp)
leaq 0x109edb(%rip), %r12    # 0x28b230
movq 0x40(%r12), %rax
movq (%rdi), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rdx
movq 0x8(%r12), %rbx
addq %rdx, %rbx
incl %ecx
movl (%rax,%rcx,8), %r13d
subl %edx, %r13d
movl $0x44494d4d, 0x64(%rsp) # imm = 0x44494D4D
leaq 0xb11da(%rip), %rdi     # 0x23255d
leaq 0xbb074(%rip), %rsi     # 0x23c3fe
callq 0x2210
movq %rax, %r15
leaq 0xb11cc(%rip), %rdi     # 0x232565
movl $0x16, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2410
movq 0x18(%r12), %rax
movq %r14, 0x80(%rsp)
movq (%r14), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %r8
leaq 0xb11b4(%rip), %rsi     # 0x23257c
movq %r15, %rbp
movq %r15, %rdi
movl %r13d, 0x54(%rsp)
movl %r13d, %edx
movq %rbx, 0x70(%rsp)
movq %rbx, %rcx
xorl %eax, %eax
callq 0x22b0
cmpq $0x0, 0x68(%rsp)
jle 0x18158e
xorl %ecx, %ecx
movq 0x80(%rsp), %rax
movq (%rax), %r14
movq %rcx, 0x88(%rsp)
imulq $0xa8, %rcx, %rax
movq 0x48(%r14,%rax), %r15
testq %r15, %r15
je 0x181578
addq %rax, %r14
movl 0x10(%r14), %r13d
cmpl $0x0, 0x1c(%r14)
je 0x181444
movq 0x58(%rsp), %rdi
leaq 0x109dfb(%rip), %rsi    # 0x28b230
movq %r13, %rdx
callq 0x33dc3
movl $0x10, %ebx
jmp 0x18145a
movq 0x58(%rsp), %rdi
leaq 0x109de0(%rip), %rsi    # 0x28b230
movq %r13, %rdx
callq 0x33cf8
xorl %ebx, %ebx
testq %r15, %r15
jle 0x181578
movl $0x1c, %r12d
movq 0x58(%r14), %rcx
movswq -0x8(%rcx,%r12), %rax
cmpq $0x7fff, %rax           # imm = 0x7FFF
jne 0x18148f
cmpl $0x0, (%rcx,%r12)
jne 0x18148f
cmpw $0x0, -0xa(%rcx,%r12)
js 0x18156b
leaq 0x109d9a(%rip), %rsi    # 0x28b230
movq 0x40(%rsi), %rdx
movl 0x8(%rdx,%r13,8), %r9d
movq (%rdx,%r13,8), %r8
subl %r8d, %r9d
addq 0x8(%rsi), %r8
movl -0x4(%rcx,%r12), %edx
addq %rdx, %rdx
addq 0x70(%r14), %rdx
movq %rdx, 0x148(%rsp)
movl (%rcx,%r12), %edx
movq %rdx, 0x140(%rsp)
movq %rdx, 0x138(%rsp)
movslq -0x1c(%rcx,%r12), %rdx
movl %edx, 0xac(%rsp)
movl -0x18(%rcx,%r12), %esi
movl %esi, 0xb0(%rsp)
movl -0x14(%rcx,%r12), %esi
movl %esi, 0xa0(%rsp)
movl -0x10(%rcx,%r12), %esi
movl %esi, 0xa4(%rsp)
movswl -0x8(%rcx,%r12), %esi
movl %esi, 0xbc(%rsp)
movl %esi, 0xb8(%rsp)
movq 0x58(%rsp), %rsi
movups (%rsi), %xmm0
movslq -0x14(%rcx,%r12), %rsi
movslq -0x10(%rcx,%r12), %rcx
incq %rcx
leaq 0x90(%rsp), %rdi
movq %rdi, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movq %rbx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdx, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x64(%rsp), %rdi
movq %rbp, %rsi
movq 0x70(%rsp), %rdx
movl 0x54(%rsp), %ecx
callq 0x1810ef
addq $0x20, %r12
decq %r15
jne 0x181469
movq 0x88(%rsp), %rcx
incq %rcx
cmpq 0x68(%rsp), %rcx
jne 0x1813f3
movq %rbp, %rdi
callq 0x2190
leaq 0xb114c(%rip), %rdi     # 0x2326e9
leaq 0xbae5a(%rip), %rsi     # 0x23c3fe
callq 0x2210
movq %rax, %rbx
leaq 0xa99ee(%rip), %rsi     # 0x22afa1
movq %rax, %rdi
movl 0x54(%rsp), %edx
movq 0x70(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
leaq 0xb88e9(%rip), %rsi     # 0x239eb6
movq %rbx, %rdi
movl 0x60(%rsp), %edx
movq 0x78(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
movq %rbx, %rdi
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2190