// File: /workspace/github_cpp_makefile_star2_O3/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, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, (%rsp)
movq %rdi, 0x50(%rsp)
movq 0x10(%rdi), %r12
cmpq %r12, 0x8(%r8)
jae 0x6f1d2
leaq -0x1(%r12), %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, %rdi
movq %rax, 0x10(%rbx)
jmp 0x6f1d6
movq 0x10(%rbx), %rdi
leaq (,%r12,8), %r14
xorl %esi, %esi
movq %r14, %rdx
callq 0x20e0
movq %r12, (%rbx)
testq %r12, %r12
jle 0x6f4bb
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
leaq -0x1(%r12), %rcx
movq %rcx, 0x28(%rsp)
imulq $0xa8, %r12, %rcx
addq $-0xa8, %rcx
movq %rcx, 0x60(%rsp)
movq %r12, %rcx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, 0x58(%rsp)
xorl %r15d, %r15d
movq %r12, 0x68(%rsp)
imulq $0xa8, %r15, %rcx
movq 0x50(%rsp), %rax
movq (%rax), %rsi
movq %rcx, 0x70(%rsp)
addq %rcx, %rsi
movq (%rsp), %rax
movq (%rax), %rdx
leaq 0x7c(%rsp), %rdi
callq 0x6ecd8
leaq 0xc(%rsp), %rbp
movl %r15d, 0x7c(%rsp)
cmpq %r15, 0x28(%rsp)
jle 0x6f459
movq %r15, %rax
shlq $0x20, %rax
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %r13
movq 0x60(%rsp), %r14
movq 0x28(%rsp), %r12
movq 0x50(%rsp), %rax
movq (%rax), %rsi
movl 0x8(%rsi,%r14), %eax
movq 0x70(%rsp), %rcx
cmpl 0x4(%rsi,%rcx), %eax
jb 0x6f459
addq %r14, %rsi
movq (%rsp), %rax
movq (%rax), %rdx
movq %rbp, %rdi
callq 0x6ecd8
movl %r12d, 0xc(%rsp)
movl 0x84(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jbe 0x6f2da
movl %ecx, %edx
movl %eax, %ecx
leaq 0x7c(%rsp), %rax
movq %rbp, %r8
jmp 0x6f2fc
jne 0x6f2ed
movl 0x80(%rsp), %edx
cmpl 0x10(%rsp), %edx
jae 0x6f446
movl %eax, %edx
leaq 0xc(%rsp), %rbp
movq %rbp, %rax
leaq 0x7c(%rsp), %r8
movl 0x4(%rax), %esi
movl 0x4(%r8), %edi
cmpl %edi, %esi
movl %edi, %r9d
cmoval %esi, %r9d
cmpl %edx, %ecx
movl %edx, %r10d
cmovbl %ecx, %r10d
cmpl %r9d, %r10d
jbe 0x6f424
leal 0x80(%rsi), %r9d
cmpl %r9d, %edi
jbe 0x6f366
movl $0x80, %r9d
addl %r9d, %edi
cmpl %esi, %edi
jb 0x6f424
movl $0x80, %esi
addl %esi, %edx
cmpl %ecx, %edx
jb 0x6f424
movq (%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movsd 0x48(%rsp), %xmm0
movq %r8, %rcx
movq %rax, %r8
jmp 0x6f37d
movq (%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movsd 0x48(%rsp), %xmm0
movq %rax, %rcx
callq 0x6f0a2
leaq 0xc(%rsp), %rbp
testl %eax, %eax
je 0x6f424
movq 0x10(%rbx), %rax
incq (%rax,%r15,8)
incq (%rax,%r12,8)
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x6f3cb
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movq %r12, %rdx
orq 0x30(%rsp), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x6f40e
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0xc(%rsp), %rbp
movq %r13, %rdx
orq %r15, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
decq %r12
addq $-0xa8, %r14
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rax, %r13
cmpq %r15, %r12
jg 0x6f28a
jmp 0x6f459
movl %eax, %edx
movl %eax, %ecx
leaq 0x7c(%rsp), %rax
leaq 0xc(%rsp), %rbp
jmp 0x6f2d5
incq %r15
movq 0x68(%rsp), %r12
cmpq %r12, %r15
jne 0x6f232
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq (%rcx,%r12,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x45373
testq %r12, %r12
jle 0x6f4a9
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movq %r12, %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, %r12
jne 0x6f48c
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq 0x10(%rbx), %r14
movq %r14, %rdi
movq %r14, %rsi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45373