// File: /workspace/github_cpp_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %rcx, 0x20(%rsp)
movq %rdx, %r14
movq %rsi, 0x8(%rsp)
movq %rdi, %r13
movb (%rdi), %al
leaq 0xc0(%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), %r15d
movl 0x28(%r13), %r10d
imulq $0x28, %r15, %rsi
addq %rcx, %rsi
imulq $0x28, %r10, %rdx
addq %rcx, %rdx
movq 0x28(%r14), %rcx
movq 0x18(%r14), %r8
movq 0x20(%r14), %r9
movl 0x38(%r14), %eax
movq %rax, (%rsp)
movl 0x3c(%r14), %eax
movq %rax, 0x18(%rsp)
leaq 0x40(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0x60(%r14), %r12
leaq 0x80(%r14), %rbp
subq $0x8, %rsp
leaq 0x40(%rsp), %r11
leaq 0x18(%rsp), %rbx
pushq %r11
pushq %rbx
leaq 0x58(%rsp), %r11
pushq %r11
leaq 0x48(%rsp), %r11
pushq %r11
pushq %rbp
pushq %r12
pushq %rax
pushq %r10
pushq %r15
pushq 0x68(%rsp)
pushq 0x58(%rsp)
pushq 0x80(%rsp)
pushq 0x30(%r14)
callq 0xcbd98
addq $0x70, %rsp
movl %eax, (%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 0x8(%rsp), %rax
movq 0x10(%rax), %rcx
movl 0x24(%r13), %r10d
movl 0x28(%r13), %eax
imulq $0x28, %rax, %rsi
addq %rcx, %rsi
imulq $0x28, %r10, %rdx
addq %rcx, %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 0x48(%rsp), %r15
pushq %r15
leaq 0x38(%rsp), %r15
pushq %r15
leaq 0x50(%rsp), %r15
pushq %r15
leaq 0x30(%rsp), %r15
pushq %r15
movq %rbp, 0x80(%rsp)
pushq %rbp
movq %r12, 0x48(%rsp)
pushq %r12
pushq 0x68(%rsp)
pushq %r10
pushq %rax
pushq %rbx
pushq %r11
pushq 0x80(%rsp)
pushq 0x30(%r14)
callq 0xcbd98
movl 0x70(%rsp), %r10d
addq $0x70, %rsp
movl %eax, %ecx
andl %r10d, %ecx
cmpl $-0x1, %ecx
je 0xd2f42
leaq 0xe8(%rsp), %r9
leaq 0xe0(%rsp), %rbx
movb (%r13), %cl
movb %cl, 0x8(%rbx)
movl 0x14(%r13), %edx
movl %edx, -0x18(%rbx)
movl 0x18(%r13), %esi
decl %esi
movl %esi, -0x14(%rbx)
movl 0x1c(%r13), %edi
movl %edi, -0x10(%rbx)
movl 0x20(%r13), %r8d
decl %r8d
movl %r8d, -0xc(%rbx)
movb %cl, 0x90(%rsp)
movl %edi, 0x70(%rsp)
movl %r8d, 0x74(%rsp)
movl %edx, 0x78(%rsp)
movl %esi, 0x7c(%rsp)
cmpl $-0x1, %r10d
je 0xd2da8
cmpl $-0x1, %eax
jne 0xd2da8
movq 0x8(%rsp), %rsi
movl 0x24(%r13), %ebp
movl 0x28(%r13), %r8d
leaq 0xc0(%rsp), %rdi
leaq 0x10(%rsp), %r11
movq %r9, (%rsp)
jmp 0xd2e22
movq 0x8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x88(%rsp), %rcx
cmpl $-0x1, %r10d
leaq 0xc0(%rsp), %rdi
leaq 0x10(%rsp), %r11
jne 0xd2deb
cmpl $-0x1, %eax
je 0xd2deb
movl 0x24(%r13), %r8d
movl 0x28(%r13), %ebp
movq %rdx, (%rsp)
movq %rcx, %rbx
leaq 0x68(%rsp), %rdi
jmp 0xd2e22
movq 0xe0(%rsp), %rax
cmpq 0x88(%rsp), %rax
movl 0x24(%r13), %ebp
movl 0x28(%r13), %eax
movl %eax, %r8d
cmovll %ebp, %r8d
cmovll %eax, %ebp
cmovlq %rdx, %r9
movq %r9, (%rsp)
cmovlq %rcx, %rbx
leaq 0x68(%rsp), %rax
cmovlq %rax, %rdi
movq 0x10(%rsi), %rdx
movl %ebp, %eax
imulq $0x28, %rax, %r15
leaq (%rdx,%r15), %rsi
movl %r8d, %eax
imulq $0x28, %rax, %r13
addq %r13, %rdx
movq 0x28(%r14), %rcx
movq %r8, %rax
movq %r8, 0x50(%rsp)
movq 0x18(%r14), %r8
movq 0x20(%r14), %r9
movl 0x38(%r14), %r10d
movq %r10, 0x48(%rsp)
movl 0x3c(%r14), %r10d
subq $0x8, %rsp
leaq 0x40(%rsp), %r12
pushq %r12
pushq %r11
leaq 0x58(%rsp), %r11
pushq %r11
leaq 0x48(%rsp), %r11
pushq %r11
pushq 0x80(%rsp)
pushq 0x48(%rsp)
pushq 0x68(%rsp)
pushq %rax
pushq %rbp
pushq %r10
pushq 0xa0(%rsp)
pushq 0x80(%rsp)
pushq 0x30(%r14)
callq 0xcbd98
addq $0x70, %rsp
movq 0x8(%rsp), %r12
movq 0x10(%r12), %rax
movl (%rax,%r15), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq %rdx, %rcx
leaq 0xb8(%rsp), %r9
movq %rcx, (%r9)
movl (%rax,%r13), %ecx
andq %rdx, %rcx
leaq 0xb0(%rsp), %rax
movq %rcx, (%rax)
addl %ebp, %ebp
leaq 0x64(%rsp), %rdx
movl %ebp, (%rdx)
movq (%rsp), %rcx
movzbl (%rcx), %ecx
movq 0x50(%rsp), %rsi
leal (%rcx,%rsi,2), %ecx
leaq 0x60(%rsp), %r10
movl %ecx, (%r10)
movq 0x30(%r14), %rsi
movl 0x28(%rsp), %r8d
movl 0x10(%rsp), %r11d
cvtsi2sdq (%rbx), %xmm0
subq $0x8, %rsp
leaq 0x118b4d(%rip), %rbx    # 0x1eba62
pushq $0x1
popq %rcx
movq %r14, %rdi
pushq %rbx
pushq $0x2
pushq %r12
pushq %rax
pushq %r11
pushq $0x1
pushq %r10
callq 0x82297
addq $0x40, %rsp
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x180097(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x118b02(%rip), %rdi    # 0x1eba55
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
jmp 0xd2f30