// File: /workspace/github_cpp_makefile_star2_O2/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, 0x70(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %r14
movq 0x10(%rdi), %r15
leaq 0x11014c(%rip), %r13    # 0x254230
movq 0x40(%r13), %rax
movq (%rdi), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rdx
movq 0x8(%r13), %rbx
addq %rdx, %rbx
incl %ecx
movl (%rax,%rcx,8), %ebp
subl %edx, %ebp
leaq 0x64(%rsp), %rax
movl $0x44494d4d, (%rax)     # imm = 0x44494D4D
leaq 0xac55c(%rip), %rdi     # 0x1f066d
leaq 0xb6589(%rip), %rsi     # 0x1fa6a1
callq 0x2210
movq %rax, %r12
leaq 0xac54e(%rip), %rdi     # 0x1f0675
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2410
movq 0x18(%r13), %rax
movq %r14, 0x78(%rsp)
movq (%r14), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %r8
leaq 0xac53e(%rip), %rsi     # 0x1f068c
xorl %r14d, %r14d
movq %r12, %r13
movq %r12, %rdi
movl %ebp, 0x54(%rsp)
movl %ebp, %edx
movq %rbx, 0x68(%rsp)
movq %rbx, %rcx
xorl %eax, %eax
callq 0x22b0
testq %r15, %r15
cmovleq %r14, %r15
movq %r15, 0x80(%rsp)
xorl %ecx, %ecx
cmpq 0x80(%rsp), %rcx
je 0x144311
movq 0x78(%rsp), %rax
movq (%rax), %r14
movq %rcx, 0x88(%rsp)
imulq $0xa8, %rcx, %rax
movq 0x48(%r14,%rax), %r15
testq %r15, %r15
je 0x144301
addq %rax, %r14
movl 0x10(%r14), %ebp
cmpl $0x0, 0x1c(%r14)
je 0x1441d7
movq 0x58(%rsp), %rdi
leaq 0x110066(%rip), %rsi    # 0x254230
movq %rbp, %rdx
callq 0x7215
pushq $0x10
popq %rbx
jmp 0x1441ed
movq 0x58(%rsp), %rdi
leaq 0x11004d(%rip), %rsi    # 0x254230
movq %rbp, %rdx
callq 0x715e
xorl %ebx, %ebx
testq %r15, %r15
movl $0x0, %eax
cmovleq %rax, %r15
pushq $0x1c
popq %r12
subq $0x1, %r15
jb 0x144301
movq 0x58(%r14), %rcx
movswl -0x8(%rcx,%r12), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x144230
movl (%rcx,%r12), %edx
testl %edx, %edx
jne 0x144234
xorl %edx, %edx
cmpw $0x0, -0xa(%rcx,%r12)
jns 0x144234
jmp 0x1442f8
movl (%rcx,%r12), %edx
leaq 0x10fff5(%rip), %rdi    # 0x254230
movq 0x40(%rdi), %rsi
movl 0x8(%rsi,%rbp,8), %r9d
movq (%rsi,%rbp,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 %r13, %rsi
movq 0x68(%rsp), %rdx
movl 0x54(%rsp), %ecx
callq 0x143ed6
addq $0x20, %r12
jmp 0x1441fd
movq 0x88(%rsp), %rcx
incq %rcx
jmp 0x14417d
movq %r13, %rdi
callq 0x2190
leaq 0xac4d9(%rip), %rdi     # 0x1f07f9
leaq 0xb637a(%rip), %rsi     # 0x1fa6a1
callq 0x2210
movq %rax, %rbx
leaq 0xa4d5b(%rip), %rsi     # 0x1e9091
movq %rax, %rdi
movl 0x54(%rsp), %edx
movq 0x68(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
leaq 0xb3d1a(%rip), %rsi     # 0x1f806a
movq %rbx, %rdi
movl 0x60(%rsp), %edx
movq 0x70(%rsp), %rcx
xorl %eax, %eax
callq 0x22b0
movq %rbx, %rdi
callq 0x2190
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq