// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_gov_idx(overlap_region_alloc*, ul_idx_t const*, ug_opt_t const*, long, double, asg64_v*)
void gen_gov_idx(overlap_region_alloc* ol, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, double diff_ec_ul, asg64_v* idx)
{
    int64_t on = ol->length, k, i; uint64_t os, oe, ovlp; ul_ov_t p, q, *li, *lj;
    kv_resize(uint64_t, *idx, (uint64_t)on); memset(idx->a, 0, sizeof(*(idx->a))*on);
    for (k = 0, idx->n = on; k < on; k++) {
        convert_ul_ov_t(&p, &(ol->list[k]), uref->ug); p.qn = k; 
        // idx->a[k] = idx->n; idx->a[k] <<= 32;
        for (i = on - 1; i >= 0 && i > k && ol->list[i].x_pos_e >= ol->list[k].x_pos_s; i--) {
            // if(k >= i) continue;
            convert_ul_ov_t(&q, &(ol->list[i]), uref->ug); q.qn = i;

            if(p.qe > q.qe) li = &p, lj = &q;
			else if(p.qe == q.qe && p.qs >= q.qs) li = &p, lj = &q;
			else lj = &p, li = &q;
			os = MAX(li->qs, lj->qs), oe = MIN(li->qe, lj->qe);
			ovlp = ((oe > os)? (oe - os):0);
			if(!ovlp) continue;//no overlap

            if(lj->qs <= li->qs+G_CHAIN_INDEL) {
				if(govlp_check(uref, uopt, bw, diff_ec_ul, li, lj)) {
                    idx->a[k]++; idx->a[i]++; 
                    kv_push(uint64_t, *idx, (((uint64_t)k)<<32)|((uint64_t)i));
                    kv_push(uint64_t, *idx, (((uint64_t)i)<<32)|((uint64_t)k));
                }
			} else if((lj->qe+G_CHAIN_INDEL>=li->qe) && (lj->qs+G_CHAIN_INDEL>=li->qs)) {
				if(govlp_check(uref, uopt, bw, diff_ec_ul, lj, li)) {
                    idx->a[k]++; idx->a[i]++; 
                    kv_push(uint64_t, *idx, (((uint64_t)k)<<32)|((uint64_t)i));
                    kv_push(uint64_t, *idx, (((uint64_t)i)<<32)|((uint64_t)k));
                }
			}
        }
    }
    radix_sort_bc64(idx->a + on, idx->a + idx->n);
    for (k = 0, os = oe = on; k < on; k++) {
        oe = os + idx->a[k];
        idx->a[k] = (os<<32)|oe;
        os = oe;
    }

    // for (k = 0; k < on; k++) {
    //     int64_t s, e;
    //     s = idx->a[k]>>32; e = (uint32_t)idx->a[k];
    //     for (i = s; i < e; i++) {
    //         assert((idx->a[i]>>32) == (uint32_t)k);
    //         fprintf(stderr, "k::%ld[M::%s::utg%.6dl] utg%.6dl\n", k, __func__, 
    //             (int32_t)ol->list[k].y_id+1, (int32_t)ol->list[(uint32_t)idx->a[i]].y_id+1);
    //     }
    // }   
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %rbx
movsd %xmm0, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x10(%rdi), %r14
cmpq %r14, 0x8(%r8)
jae 0x6f882
leaq -0x1(%r14), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq 0x10(%rbx), %rdi
leaq (,%r14,8), %rdx
xorl %esi, %esi
callq 0x20e0
movq %r14, (%rbx)
testq %r14, %r14
jle 0x6faf5
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
leaq -0x1(%r14), %rcx
movq %rcx, 0x10(%rsp)
movq %r14, %rcx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, 0x48(%rsp)
imulq $0xa8, %r14, %rax
addq $-0xa8, %rax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movq %r14, 0x50(%rsp)
imulq $0xa8, %r15, %rcx
movq 0x38(%rsp), %rax
movq (%rax), %rsi
movq %rcx, 0x58(%rsp)
addq %rcx, %rsi
movq 0x8(%rsp), %rax
movq (%rax), %rdx
leaq 0x60(%rsp), %rdi
callq 0x6f306
movl %r15d, 0x60(%rsp)
cmpq %r15, 0x10(%rsp)
jle 0x6fae4
movq %r15, %rax
shlq $0x20, %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %r12
movq 0x10(%rsp), %r14
movq 0x38(%rsp), %rax
movq (%rax), %rsi
movl 0x8(%rsi,%r13), %eax
movq 0x58(%rsp), %rcx
cmpl 0x4(%rsi,%rcx), %eax
jb 0x6fae4
addq %r13, %rsi
movq 0x8(%rsp), %rax
movq (%rax), %rdx
leaq 0x7c(%rsp), %rbp
movq %rbp, %rdi
callq 0x6f306
movl %r14d, 0x7c(%rsp)
movl 0x84(%rsp), %ecx
leaq 0x60(%rsp), %rax
cmpl %ecx, 0x68(%rsp)
ja 0x6f99a
jne 0x6f990
movl 0x64(%rsp), %ecx
leaq 0x7c(%rsp), %rbp
cmpl 0x80(%rsp), %ecx
jae 0x6f99a
leaq 0x7c(%rsp), %rax
leaq 0x60(%rsp), %rbp
movl 0x4(%rax), %ecx
movl 0x8(%rax), %esi
movl 0x4(%rbp), %edx
movl 0x8(%rbp), %edi
cmpl %edx, %ecx
movl %edx, %r9d
cmoval %ecx, %r9d
cmpl %edi, %esi
movl %edi, %r10d
cmovbl %esi, %r10d
cmpl %r9d, %r10d
jbe 0x6fac4
leal 0x80(%rcx), %r9d
cmpl %r9d, %edx
jbe 0x6fa0a
movl $0x80, %r9d
addl %r9d, %edi
cmpl %esi, %edi
setb %sil
addl %r9d, %edx
cmpl %ecx, %edx
setb %cl
orb %sil, %cl
jne 0x6fac4
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movsd 0x30(%rsp), %xmm0
movq %rbp, %rcx
movq %rax, %r8
jmp 0x6fa25
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movsd 0x30(%rsp), %xmm0
movq %rax, %rcx
movq %rbp, %r8
callq 0x6f757
testl %eax, %eax
je 0x6fac4
movq 0x10(%rbx), %rdi
incq (%rdi,%r15,8)
incq (%rdi,%r14,8)
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x6fa68
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq %r14, %rax
orq 0x18(%rsp), %rax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %rax, (%rdi,%rcx,8)
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x6faac
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq %r12, %rax
orq %r15, %rax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
decq %r14
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %r12
addq $-0xa8, %r13
cmpq %r15, %r14
jg 0x6f931
incq %r15
movq 0x50(%rsp), %r14
cmpq %r14, %r15
jne 0x6f8dd
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq (%rcx,%r14,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x45d47
testq %r14, %r14
jle 0x6fb34
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movq %r14, %rdx
movq (%rax,%rcx,8), %rsi
addq %rdx, %rsi
shlq $0x20, %rdx
orq %rsi, %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
movq %rsi, %rdx
cmpq %rcx, %r14
jne 0x6fb17
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq