// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movl %ecx, -0x44(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x150(%rbp)
leaq 0x16d44f(%rip), %rax    # 0x3c52d0
movq 0x8(%rax), %rax
leaq 0x16d444(%rip), %rcx    # 0x3c52d0
movq 0x40(%rcx), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movl (%rdx), %edx
addq (%rcx,%rdx,8), %rax
movq %rax, -0x158(%rbp)
movq $0x0, -0x160(%rbp)
leaq 0x16d41a(%rip), %rax    # 0x3c52d0
movq 0x40(%rax), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
leaq 0x16d3fd(%rip), %rcx    # 0x3c52d0
movq 0x40(%rcx), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx), %rdx
movl (%rdx), %edx
subq (%rcx,%rdx,8), %rax
movq %rax, -0x168(%rbp)
movb $0x4d, -0x17c(%rbp)
movb $0x4d, -0x17b(%rbp)
movb $0x49, -0x17a(%rbp)
movb $0x44, -0x179(%rbp)
leaq 0x13747e(%rip), %rdi    # 0x38f38c
leaq 0x1414bd(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rdi
leaq 0x137465(%rip), %rsi    # 0x38f394
movb $0x0, %al
callq 0x22a0
movq -0x188(%rbp), %rdi
movq -0x168(%rbp), %rax
movl %eax, %edx
movq -0x158(%rbp), %rcx
leaq 0x16d37c(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rsi
movq (%rsi), %rsi
movl (%rsi), %esi
movq (%rax,%rsi,8), %r8
leaq 0x13743f(%rip), %rsi    # 0x38f3ab
movb $0x0, %al
callq 0x22a0
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x25831f
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x58(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x257fb7
jmp 0x25830e
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq %rax, -0x170(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x258001
movq -0x38(%rbp), %rdi
movq -0x170(%rbp), %rdx
leaq 0x16d2e1(%rip), %rsi    # 0x3c52d0
callq 0xa500
movq $0x10, -0x178(%rbp)
jmp 0x258023
movq -0x38(%rbp), %rdi
movq -0x170(%rbp), %rdx
leaq 0x16d2bd(%rip), %rsi    # 0x3c52d0
callq 0xa380
movq $0x0, -0x178(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x25830c
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x258091
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x258091
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x258091
jmp 0x2582fb
leaq 0x16d238(%rip), %rax    # 0x3c52d0
movq 0x40(%rax), %rax
movq -0x170(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
leaq 0x16d221(%rip), %rcx    # 0x3c52d0
movq 0x40(%rcx), %rcx
movq -0x170(%rbp), %rdx
subq (%rcx,%rdx,8), %rax
movq %rax, -0x160(%rbp)
leaq 0x16d204(%rip), %rax    # 0x3c52d0
movq 0x8(%rax), %rax
leaq 0x16d1f9(%rip), %rcx    # 0x3c52d0
movq 0x40(%rcx), %rcx
movq -0x170(%rbp), %rdx
addq (%rcx,%rdx,8), %rax
movq %rax, -0x150(%rbp)
movq -0x70(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x70(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x60(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0x98(%rbp)
movq %rax, -0xa0(%rbp)
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x12c(%rbp)
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x128(%rbp)
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x138(%rbp)
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x134(%rbp)
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x11c(%rbp)
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x120(%rbp)
leaq -0x17c(%rbp), %rdi
movq -0x188(%rbp), %rsi
movq -0x158(%rbp), %rdx
movq -0x168(%rbp), %rax
movl %eax, %ecx
movq -0x150(%rbp), %r8
movq -0x160(%rbp), %rax
movl %eax, %r9d
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x198(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %r13
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %r10
shlq $0x5, %r10
addq %r10, %rax
movslq (%rax), %r12
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %r10
shlq $0x5, %r10
addq %r10, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movslq %eax, %r15
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %r10
shlq $0x5, %r10
addq %r10, %rax
movslq 0x8(%rax), %r14
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %r10
shlq $0x5, %r10
addq %r10, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movslq %eax, %rbx
movq -0x178(%rbp), %r11
movq -0x70(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %r10
shlq $0x5, %r10
addq %r10, %rax
movswq 0x14(%rax), %r10
leaq -0x148(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x198(%rbp), %rax
movq %rax, (%rsp)
movq -0x190(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0x257c00
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x25802b
jmp 0x25830e
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x257f7b
movq -0x188(%rbp), %rdi
callq 0x21a0
leaq 0x1371e6(%rip), %rdi    # 0x38f518
leaq 0x141099(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rdi
movq -0x168(%rbp), %rax
movl %eax, %edx
movq -0x158(%rbp), %rcx
leaq 0x12fa19(%rip), %rsi    # 0x387d7c
movb $0x0, %al
callq 0x22a0
movq -0x188(%rbp), %rdi
movl -0x44(%rbp), %edx
movq -0x40(%rbp), %rcx
leaq 0x13e9cb(%rip), %rsi    # 0x396d4a
movb $0x0, %al
callq 0x22a0
movq -0x188(%rbp), %rdi
callq 0x21a0
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)