// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: chain_origin_trans_uid_by_purge(hap_overlaps*, ma_ug_t*, hap_cov_t*, unsigned long*)
void chain_origin_trans_uid_by_purge(hap_overlaps *x, ma_ug_t *ug, hap_cov_t *cov, uint64_t* position_index)
{
    uint32_t pri_uid, aux_uid, r_x, r_y;
    hap_candidates hap_for, hap_rev, *hap = NULL;
    long long x_pos_beg, x_pos_end, y_pos_beg, y_pos_end;

    Get_rev(hap_for) = x->rev;
    Get_x_beg(hap_for) = x->x_beg_id; Get_x_end(hap_for) = x->x_end_id - 1;
    Get_y_beg(hap_for) = x->y_beg_id; Get_y_end(hap_for) = x->y_end_id - 1;
    r_x = determine_hap_overlap_type_advance(&hap_for, &(ug->u.a[x->xUid]), &(ug->u.a[x->yUid]),
    cov->ruIndex, cov->reverse_sources, cov->coverage_cut, cov->read_g, position_index, 
    cov->max_hang, cov->min_ovlp, x->xUid, x->yUid, &(cov->u_buffer), &(cov->tailIndex), 
    &(cov->prevIndex), &x_pos_beg, &x_pos_end, &y_pos_beg, &y_pos_end);

    // if(r_x != (uint32_t)-1)
    // {
    //     adjust_hap_overlaps_score(&(ug->u.a[x->xUid]), NULL, &(hap_for.score), 
    //     x->xUid, x->yUid, x_pos_beg, x_pos_end);
    // }   
    

    Get_rev(hap_rev) = x->rev;
    Get_x_beg(hap_rev) = x->y_beg_id; Get_x_end(hap_rev) = x->y_end_id - 1;
    Get_y_beg(hap_rev) = x->x_beg_id; Get_y_end(hap_rev) = x->x_end_id - 1;
    r_y = determine_hap_overlap_type_advance(&hap_rev, &(ug->u.a[x->yUid]), &(ug->u.a[x->xUid]),
    cov->ruIndex, cov->reverse_sources, cov->coverage_cut, cov->read_g, position_index, 
    cov->max_hang, cov->min_ovlp, x->yUid, x->xUid, &(cov->u_buffer), &(cov->tailIndex), 
    &(cov->prevIndex), &y_pos_beg, &y_pos_end, &x_pos_beg, &x_pos_end);

    // if(r_y != (uint32_t)-1)
    // {
    //     adjust_hap_overlaps_score(&(ug->u.a[x->yUid]), NULL, &(hap_rev.score), 
    //     x->yUid, x->xUid, y_pos_beg, y_pos_end);
    // }

    if(r_x == (uint32_t)-1 && r_y == (uint32_t)-1)
    {
        fprintf(stderr, "ERROR-purge\n");
        return;
    }

    Get_rev(hap_for) = x->rev;
    Get_x_beg(hap_for) = x->x_beg_id; Get_x_end(hap_for) = x->x_end_id - 1;
    Get_y_beg(hap_for) = x->y_beg_id; Get_y_end(hap_for) = x->y_end_id - 1;

    Get_rev(hap_rev) = x->rev;
    Get_x_beg(hap_rev) = x->y_beg_id; Get_x_end(hap_rev) = x->y_end_id - 1;
    Get_y_beg(hap_rev) = x->x_beg_id; Get_y_end(hap_rev) = x->x_end_id - 1;


    if(r_x != (uint32_t)-1 && r_y == (uint32_t)-1)
    {
        pri_uid = x->xUid; aux_uid = x->yUid; hap = &hap_for;
    }
    else if(r_x == (uint32_t)-1 && r_y != (uint32_t)-1)
    {
        aux_uid = x->xUid; pri_uid = x->yUid; hap = &hap_rev;
    }
    else
    {
        if(hap_for.score >= hap_rev.score)
        {
            pri_uid = x->xUid; aux_uid = x->yUid; hap = &hap_for;
        }
        else
        {
            aux_uid = x->xUid; pri_uid = x->yUid; hap = &hap_rev;
        }
    }

    determine_hap_overlap_type_advance(hap, &(ug->u.a[pri_uid]), &(ug->u.a[aux_uid]),
    cov->ruIndex, cov->reverse_sources, cov->coverage_cut, cov->read_g, position_index, 
    cov->max_hang, cov->min_ovlp, pri_uid, aux_uid, &(cov->u_buffer), &(cov->tailIndex), 
    &(cov->prevIndex), &x_pos_beg, &x_pos_end, &y_pos_beg, &y_pos_end);

    // adjust_hap_overlaps_score(&(ug->u.a[pri_uid]), NULL, &(hap->score), 
    // pri_uid, aux_uid, x_pos_beg, x_pos_end);

    uint64_t pri_len = ug->u.a[pri_uid].len, aux_len = ug->u.a[aux_uid].len;
    pri_uid <<= 1; aux_uid <<= 1; aux_uid += hap->rev;

    // uint32_t i_n = cov->t_ch->k_trans.n, i;

    chain_origin_trans_uid_by_distance(cov, cov->read_g, &pri_uid, 1, x_pos_beg, &pri_len, 
                                                    &aux_uid, 1, y_pos_beg, &aux_len, ug, RC_2, hap->score, __func__);
    
    // fprintf(stderr, "\nocc: %u\n", (uint32_t)(cov->t_ch->k_trans.n - i_n));
    // fprintf(stderr, "#s-utg%.6ul\t%u\t%u\td-utg%.6ul\t%u\t%u\trev(%u)\n", 
    // x->xUid+1, x->x_beg_pos, x->x_end_pos, x->yUid+1, x->y_beg_pos, x->y_end_pos, x->rev);
    // for (i = i_n; i < cov->t_ch->k_trans.n; i++)
    // {
    //     fprintf(stderr, "s-utg%.6ul\t%u\t%u\td-utg%.6ul\t%u\t%u\trev(%u)\n", 
    //     cov->t_ch->k_trans.a[i].qn+1, cov->t_ch->k_trans.a[i].qs, cov->t_ch->k_trans.a[i].qe, 
    //     cov->t_ch->k_trans.a[i].tn+1, cov->t_ch->k_trans.a[i].ts, cov->t_ch->k_trans.a[i].te, 
    //     cov->t_ch->k_trans.a[i].rev);
    // }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, %r14
movq %rsi, 0x40(%rsp)
movq %rdi, %r13
movb (%rdi), %al
leaq 0xb0(%rsp), %rdi
movb %al, 0x28(%rdi)
movl 0x14(%r13), %eax
movl %eax, 0x8(%rdi)
movl 0x18(%r13), %eax
decl %eax
movl %eax, 0xc(%rdi)
movl 0x1c(%r13), %eax
movl %eax, 0x10(%rdi)
movl 0x20(%r13), %eax
decl %eax
movl %eax, 0x14(%rdi)
movq 0x10(%rsi), %rcx
movl 0x24(%r13), %ebx
movl 0x28(%r13), %r10d
leaq (%rbx,%rbx,4), %rdx
leaq (%rcx,%rdx,8), %rsi
leaq (%r10,%r10,4), %rdx
leaq (%rcx,%rdx,8), %rdx
movq 0x28(%r14), %rcx
movq 0x18(%r14), %r8
movq 0x20(%r14), %r9
movl 0x38(%r14), %eax
movq %rax, 0x10(%rsp)
movl 0x3c(%r14), %eax
movq %rax, 0x8(%rsp)
leaq 0x40(%r14), %rax
movq %rax, 0x38(%rsp)
leaq 0x60(%r14), %r12
leaq 0x80(%r14), %rbp
subq $0x8, %rsp
leaq 0x50(%rsp), %r11
leaq 0x28(%rsp), %r15
pushq %r11
pushq %r15
leaq 0x40(%rsp), %r11
pushq %r11
leaq 0x50(%rsp), %r11
pushq %r11
pushq %rbp
pushq %r12
pushq %rax
pushq %r10
pushq %rbx
pushq 0x58(%rsp)
pushq 0x68(%rsp)
pushq 0x78(%rsp)
pushq 0x30(%r14)
callq 0x10a80f
addq $0x70, %rsp
movl %eax, 0x10(%rsp)
movb (%r13), %al
leaq 0x68(%rsp), %rdi
movb %al, 0x28(%rdi)
movl 0x1c(%r13), %eax
movl %eax, 0x8(%rdi)
movl 0x20(%r13), %eax
decl %eax
movl %eax, 0xc(%rdi)
movl 0x14(%r13), %eax
movl %eax, 0x10(%rdi)
movl 0x18(%r13), %eax
decl %eax
movl %eax, 0x14(%rdi)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rcx
movl 0x24(%r13), %r10d
movl 0x28(%r13), %eax
leaq (%rax,%rax,4), %rdx
leaq (%rcx,%rdx,8), %rsi
leaq (%r10,%r10,4), %rdx
leaq (%rcx,%rdx,8), %rdx
movq 0x28(%r14), %rcx
movq 0x18(%r14), %r8
movq 0x20(%r14), %r9
movl 0x38(%r14), %r11d
movl 0x3c(%r14), %ebx
subq $0x8, %rsp
leaq 0x30(%rsp), %r15
pushq %r15
leaq 0x40(%rsp), %r15
pushq %r15
leaq 0x60(%rsp), %r15
pushq %r15
leaq 0x40(%rsp), %r15
pushq %r15
movq %rbp, 0x78(%rsp)
pushq %rbp
movq %r12, 0x38(%rsp)
pushq %r12
pushq 0x70(%rsp)
pushq %r10
pushq %rax
pushq %rbx
pushq %r11
pushq 0x78(%rsp)
pushq 0x30(%r14)
callq 0x10a80f
movl 0x80(%rsp), %r9d
addq $0x70, %rsp
movl %eax, %ecx
andl %r9d, %ecx
cmpl $-0x1, %ecx
je 0x112681
movb (%r13), %cl
movb %cl, 0xd8(%rsp)
movl 0x14(%r13), %edx
movl %edx, 0xb8(%rsp)
movl 0x18(%r13), %esi
decl %esi
movl %esi, 0xbc(%rsp)
movl 0x1c(%r13), %edi
movl %edi, 0xc0(%rsp)
movl 0x20(%r13), %r8d
decl %r8d
movl %r8d, 0xc4(%rsp)
movb %cl, 0x90(%rsp)
movl %edi, 0x70(%rsp)
movl %r8d, 0x74(%rsp)
movl %edx, 0x78(%rsp)
movl %esi, 0x7c(%rsp)
cmpl $-0x1, %r9d
je 0x11250f
cmpl $-0x1, %eax
jne 0x11250f
movl 0x24(%r13), %eax
movl %eax, 0x4(%rsp)
movl 0x28(%r13), %eax
movl %eax, (%rsp)
leaq 0xb0(%rsp), %r15
leaq 0x28(%rsp), %r12
jmp 0x112560
cmpl $-0x1, %r9d
leaq 0x28(%rsp), %r12
jne 0x112525
cmpl $-0x1, %eax
je 0x112525
movl 0x24(%r13), %eax
jmp 0x112543
movq 0xd0(%rsp), %rcx
movl 0x24(%r13), %eax
cmpq 0x88(%rsp), %rcx
leaq 0xb0(%rsp), %r15
jge 0x112555
movl %eax, (%rsp)
movl 0x28(%r13), %eax
movl %eax, 0x4(%rsp)
leaq 0x68(%rsp), %r15
jmp 0x112560
movl %eax, 0x4(%rsp)
movl 0x28(%r13), %eax
movl %eax, (%rsp)
movq 0x40(%rsp), %rbp
movq 0x10(%rbp), %rcx
leaq 0x4(%rsp), %rax
movl (%rax), %eax
leaq (%rax,%rax,4), %rdx
leaq (%rcx,%rdx,8), %rsi
movq %rsp, %rdx
movl (%rdx), %r11d
leaq (%r11,%r11,4), %rdx
leaq (%rcx,%rdx,8), %rdx
movq 0x28(%r14), %rcx
movq 0x18(%r14), %r8
movq 0x20(%r14), %r9
movl 0x38(%r14), %r10d
movl 0x3c(%r14), %ebx
subq $0x8, %rsp
movq %r15, %rdi
leaq 0x50(%rsp), %r13
pushq %r13
leaq 0x30(%rsp), %r13
pushq %r13
pushq %r12
leaq 0x50(%rsp), %r12
pushq %r12
pushq 0x78(%rsp)
pushq 0x38(%rsp)
pushq 0x70(%rsp)
pushq %r11
pushq %rax
pushq %rbx
pushq %r10
pushq 0x78(%rsp)
pushq 0x30(%r14)
callq 0x10a80f
addq $0x70, %rsp
movq 0x10(%rbp), %rax
leaq 0x4(%rsp), %r8
movl (%r8), %ecx
leaq (%rcx,%rcx,4), %rdx
movl (%rax,%rdx,8), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andq %rsi, %rdx
leaq 0x60(%rsp), %r9
movq %rdx, (%r9)
movq %rsp, %r11
movl (%r11), %r10d
leaq (%r10,%r10,4), %rdi
movl (%rax,%rdi,8), %edi
andq %rsi, %rdi
leaq 0x58(%rsp), %rax
movq %rdi, (%rax)
addl %ecx, %ecx
movl %ecx, (%r8)
movq %r8, %rdx
leal (%r10,%r10), %ecx
movl %ecx, (%r11)
movzbl 0x28(%r15), %ecx
leal (%rcx,%r10,2), %ecx
movl %ecx, (%r11)
movq %r11, %rbx
movq 0x30(%r14), %rsi
movl 0x30(%rsp), %r8d
movl 0x20(%rsp), %r10d
cvtsi2sdq 0x20(%r15), %xmm0
subq $0x8, %rsp
leaq 0x11b31f(%rip), %r11    # 0x22d972
movq %r14, %rdi
movl $0x1, %ecx
pushq %r11
pushq $0x2
pushq %rbp
pushq %rax
pushq %r10
pushq $0x1
pushq %rbx
callq 0xbca10
addq $0x40, %rsp
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x13dca
jmp 0x11266f