// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq $0x0, -0xf0(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0x78(%rbp)
movq -0x30(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
subl $0x1, %eax
movl %eax, -0x94(%rbp)
movq -0x30(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
subl $0x1, %eax
movl %eax, -0x8c(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x40(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x40(%rbp), %rax
movq 0x20(%rax), %r9
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x170(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x40(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x15c(%rbp)
movq -0x40(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x158(%rbp)
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x154(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %r13d
movq -0x40(%rbp), %r12
addq $0x40, %r12
movq -0x40(%rbp), %r15
addq $0x60, %r15
movq -0x40(%rbp), %r14
addq $0x80, %r14
leaq -0xa0(%rbp), %rdi
leaq -0xf8(%rbp), %rbx
leaq -0x100(%rbp), %r11
leaq -0x108(%rbp), %r10
leaq -0x110(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x170(%rbp), %rax
movq %rax, (%rsp)
movq -0x168(%rbp), %rax
movq %rax, 0x8(%rsp)
movl -0x15c(%rbp), %eax
movl %eax, 0x10(%rsp)
movl -0x158(%rbp), %eax
movl %eax, 0x18(%rsp)
movl -0x154(%rbp), %eax
movl %eax, 0x20(%rsp)
movq -0x150(%rbp), %rax
movl %r13d, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
movq %r11, 0x50(%rsp)
movq %r10, 0x58(%rsp)
movq %rax, 0x60(%rsp)
callq 0x177300
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0xe0(%rbp)
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
subl $0x1, %eax
movl %eax, -0xdc(%rbp)
movq -0x30(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0xd8(%rbp)
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
subl $0x1, %eax
movl %eax, -0xd4(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x40(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x40(%rbp), %rax
movq 0x20(%rax), %r9
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x148(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x40(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x134(%rbp)
movq -0x40(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x130(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x12c(%rbp)
movq -0x30(%rbp), %rax
movl 0x24(%rax), %r13d
movq -0x40(%rbp), %r12
addq $0x40, %r12
movq -0x40(%rbp), %r15
addq $0x60, %r15
movq -0x40(%rbp), %r14
addq $0x80, %r14
leaq -0xe8(%rbp), %rdi
leaq -0x108(%rbp), %rbx
leaq -0x110(%rbp), %r11
leaq -0xf8(%rbp), %r10
leaq -0x100(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x148(%rbp), %rax
movq %rax, (%rsp)
movq -0x140(%rbp), %rax
movq %rax, 0x8(%rsp)
movl -0x134(%rbp), %eax
movl %eax, 0x10(%rsp)
movl -0x130(%rbp), %eax
movl %eax, 0x18(%rsp)
movl -0x12c(%rbp), %eax
movl %eax, 0x20(%rsp)
movq -0x128(%rbp), %rax
movl %r13d, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
movq %r11, 0x50(%rsp)
movq %r10, 0x58(%rsp)
movq %rax, 0x60(%rsp)
callq 0x177300
movl %eax, -0x58(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x183f6d
cmpl $-0x1, -0x58(%rbp)
jne 0x183f6d
movq 0x240089(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x2067cc(%rip), %rsi    # 0x38a72d
movb $0x0, %al
callq 0x22a0
jmp 0x1842be
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0x78(%rbp)
movq -0x30(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
subl $0x1, %eax
movl %eax, -0x94(%rbp)
movq -0x30(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
subl $0x1, %eax
movl %eax, -0x8c(%rbp)
movq -0x30(%rbp), %rax
movb (%rax), %al
movb %al, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0xe0(%rbp)
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
subl $0x1, %eax
movl %eax, -0xdc(%rbp)
movq -0x30(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0xd8(%rbp)
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
subl $0x1, %eax
movl %eax, -0xd4(%rbp)
cmpl $-0x1, -0x54(%rbp)
je 0x184029
cmpl $-0x1, -0x58(%rbp)
jne 0x184029
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x50(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x1840b0
cmpl $-0x1, -0x54(%rbp)
jne 0x184059
cmpl $-0x1, -0x58(%rbp)
je 0x184059
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x4c(%rbp)
leaq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x1840ae
movq -0x80(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jl 0x18408a
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x50(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x1840ac
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x4c(%rbp)
leaq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x1840ae
jmp 0x1840b0
movq -0xf0(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x4c(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
movl -0x50(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x40(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x40(%rbp), %rax
movq 0x20(%rax), %r9
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x198(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x40(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x184(%rbp)
movq -0x40(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x180(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x17c(%rbp)
movl -0x50(%rbp), %r13d
movq -0x40(%rbp), %r12
addq $0x40, %r12
movq -0x40(%rbp), %r15
addq $0x60, %r15
movq -0x40(%rbp), %r14
addq $0x80, %r14
leaq -0xf8(%rbp), %rbx
leaq -0x100(%rbp), %r11
leaq -0x108(%rbp), %r10
leaq -0x110(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x198(%rbp), %rax
movq %rax, (%rsp)
movq -0x190(%rbp), %rax
movq %rax, 0x8(%rsp)
movl -0x184(%rbp), %eax
movl %eax, 0x10(%rsp)
movl -0x180(%rbp), %eax
movl %eax, 0x18(%rsp)
movl -0x17c(%rbp), %eax
movl %eax, 0x20(%rsp)
movq -0x178(%rbp), %rax
movl %r13d, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
movq %r11, 0x50(%rsp)
movq %r10, 0x58(%rsp)
movq %rax, 0x60(%rsp)
callq 0x177300
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x4c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x118(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x50(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x120(%rbp)
movl -0x4c(%rbp), %eax
shll %eax
movl %eax, -0x4c(%rbp)
movl -0x50(%rbp), %eax
shll %eax
movl %eax, -0x50(%rbp)
movq -0xf0(%rbp), %rax
movzbl 0x28(%rax), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0xf8(%rbp), %rax
movl %eax, %r8d
movq -0x108(%rbp), %rax
movl %eax, %ebx
movq -0x38(%rbp), %r10
movq -0xf0(%rbp), %rax
cvtsi2sdq 0x20(%rax), %xmm0
leaq -0x4c(%rbp), %rdx
movl $0x1, %ecx
leaq -0x118(%rbp), %r9
leaq -0x50(%rbp), %r14
leaq -0x120(%rbp), %r11
leaq 0x2064a8(%rip), %rax    # 0x38a73a
movq %r14, (%rsp)
movl $0x1, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movl $0x2, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0xf9a10
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq