// File: /workspace/github_cpp_makefile_star2_O3/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 0x10d968(%rip), %r12    # 0x296230
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 0xb5eb7(%rip), %rdi     # 0x23e7ad
leaq 0xbfde5(%rip), %rsi     # 0x2486e2
callq 0x2210
movq %rax, %r15
leaq 0xb5ea9(%rip), %rdi     # 0x23e7b5
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 0xb5e91(%rip), %rsi     # 0x23e7cc
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 0x188af0
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 0x188ada
addq %rax, %r14
movl 0x10(%r14), %r13d
cmpl $0x0, 0x1c(%r14)
je 0x1889b7
movq 0x58(%rsp), %rdi
leaq 0x10d888(%rip), %rsi    # 0x296230
movq %r13, %rdx
callq 0x334a6
movl $0x10, %ebx
jmp 0x1889cd
movq 0x58(%rsp), %rdi
leaq 0x10d86d(%rip), %rsi    # 0x296230
movq %r13, %rdx
callq 0x333e0
xorl %ebx, %ebx
testq %r15, %r15
jle 0x188ada
movl $0x1c, %r12d
movq 0x58(%r14), %rcx
movswl -0x8(%rcx,%r12), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x188a05
movl (%rcx,%r12), %edx
testl %edx, %edx
jne 0x188a09
xorl %edx, %edx
cmpw $0x0, -0xa(%rcx,%r12)
jns 0x188a09
jmp 0x188acd
movl (%rcx,%r12), %edx
leaq 0x10d820(%rip), %rdi    # 0x296230
movq 0x40(%rdi), %rsi
movl 0x8(%rsi,%r13,8), %r9d
movq (%rsi,%r13,8), %r8
subl %r8d, %r9d
addq 0x8(%rdi), %r8
movl -0x4(%rcx,%r12), %esi
addq %rsi, %rsi
addq 0x70(%r14), %rsi
movq %rsi, 0x148(%rsp)
movl %edx, %edx
movq %rdx, 0x140(%rsp)
movq %rdx, 0x138(%rsp)
movq -0x1c(%rcx,%r12), %rdx
movq %rdx, 0xac(%rsp)
movslq -0x14(%rcx,%r12), %rsi
movl %esi, 0xa0(%rsp)
movslq -0x10(%rcx,%r12), %rcx
movl %ecx, 0xa4(%rsp)
movl %eax, 0xbc(%rsp)
movl %eax, 0xb8(%rsp)
movq 0x58(%rsp), %rdi
movups (%rdi), %xmm0
movslq %edx, %rdx
incq %rcx
cltq
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 0x18863a
addq $0x20, %r12
decq %r15
jne 0x1889dc
movq 0x88(%rsp), %rcx
incq %rcx
cmpq 0x68(%rsp), %rcx
jne 0x188966
movq %rbp, %rdi
callq 0x2190
leaq 0xb5e3a(%rip), %rdi     # 0x23e939
leaq 0xbfbdc(%rip), %rsi     # 0x2486e2
callq 0x2210
movq %rax, %rbx
leaq 0xae67c(%rip), %rsi     # 0x237191
movq %rax, %rdi
movl 0x54(%rsp), %edx
movq 0x70(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
leaq 0xbd66b(%rip), %rsi     # 0x24619a
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