// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/horder.cpp
// Function: get_long_switch_scaffolds(horder_t*, sc_lay_t*, osg_t*)
void get_long_switch_scaffolds(horder_t *h, sc_lay_t *sl, osg_t *lg)
{
    fprintf(stderr, "\n[M::%s::]\n", __func__);
    uint32_t i, k, r_i, ori, sw[3], sw_inner[3];
    uint64_t v, len;
    kvec_t(uint64_t) idx; kv_init(idx);
    lay_t *p = NULL;
    ma_utg_t *u = NULL;
    for (i = 0; i < sl->n; i++)
    {
        p = &(sl->a[i]);
        sw[0] = sw[1] = sw[2] = 0;
        for (k = 0; k < p->n; k += 2)
        {
            u = &(h->ug->u.a[p->a[k]>>1]);
            ori = p->a[k]&1;
            for (r_i = 0; r_i < u->n; r_i++)
            {
                v = (ori?u->a[u->n - r_i - 1]:u->a[r_i]);
                if(v != (uint64_t)-1)
                {
                    v >>= 33;
                    sw[R_INF.trio_flag[v]]++;
                }
            }
        }

        v = MIN(sw[FATHER], sw[MOTHER]);
        v = ((uint32_t)-1) - v;
        v <<= 32; v |= i;
        kv_push(uint64_t, idx, v);
    }

    radix_sort_ho64(idx.a, idx.a+idx.n);
    for (i = 0; i < sl->n; i++)
    {
        p = &(sl->a[(uint32_t)(idx.a[i])]);
        fprintf(stderr, "\nscaf-%u-th, occ-%u\n", (uint32_t)(idx.a[i]), (uint32_t)(p->n>>1));
        sw[0] = sw[1] = sw[2] = len = 0;
        for (k = 0; k < p->n; k += 2)
        {
            sw_inner[0] = sw_inner[1] = sw_inner[2] = 0;
            u = &(h->ug->u.a[p->a[k]>>1]);
            ori = p->a[k]&1;
            for (r_i = 0; r_i < u->n; r_i++)
            {
                v = (ori?u->a[u->n - r_i - 1]:u->a[r_i]);
                if(v != (uint64_t)-1)
                {
                    v >>= 33;
                    if(R_INF.trio_flag[v] == FATHER || R_INF.trio_flag[v] == MOTHER)
                    {
                        sw[R_INF.trio_flag[v]]++;
                        sw_inner[R_INF.trio_flag[v]]++;
                    }
                }
            }
            fprintf(stderr, "utg%.6ul (ori: %u), u->len-%u, sw_in[FATHER]-%u, sw_in[MOTHER]-%u\n", 
            (p->a[k]>>1)+1, p->a[k]&1, u->len, sw_inner[FATHER], sw_inner[MOTHER]);
            len += u->len + ((k + 2)< p->n? GAP_LEN:0);
        }
        fprintf(stderr, "sw[FATHER]-%u, sw[MOTHER]-%u, len-%lu\n", sw[FATHER], sw[MOTHER], len);
    }
    kv_destroy(idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, 0x28(%rsp)
movq 0x268919(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x21019f(%rip), %rsi    # 0x231870
leaq 0x2101a4(%rip), %rdx    # 0x23187c
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x22b0
cmpq $0x0, 0x8(%rbx)
movq %rbx, 0x20(%rsp)
je 0x2180b
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %eax, %eax
xorl %r15d, %r15d
movq (%rbx), %rsi
andl $0x0, 0x10(%rsp)
andq $0x0, 0x8(%rsp)
imulq $0x18, %r13, %rdx
movq 0x8(%rsi,%rdx), %rcx
testq %rcx, %rcx
je 0x217b1
addq %rdx, %rsi
movq 0x28(%rsp), %rdx
movq 0xc8(%rdx), %rdx
movq 0x10(%rdx), %rdx
movq (%rsi), %rsi
leaq 0x269af6(%rip), %rdi    # 0x28b230
movq 0x28(%rdi), %rdi
xorl %r8d, %r8d
movl (%rsi,%r8,4), %r9d
movl %r9d, %r10d
shrl %r10d
imulq $0x28, %r10, %r10
movl 0x10(%rdx,%r10), %r11d
testq %r11, %r11
je 0x217a1
addq %rdx, %r10
leal -0x1(%r11), %ebx
shlq $0x3, %r11
xorl %r14d, %r14d
testb $0x1, %r9b
jne 0x21776
movq 0x18(%r10), %rbp
addq %r14, %rbp
jmp 0x21780
movl %ebx, %ebp
shlq $0x3, %rbp
addq 0x18(%r10), %rbp
movq (%rbp), %rbp
cmpq $-0x1, %rbp
je 0x21796
shrq $0x21, %rbp
movzbl (%rdi,%rbp), %ebp
incl 0x8(%rsp,%rbp,4)
addq $0x8, %r14
decl %ebx
cmpq %r14, %r11
jne 0x21767
addq $0x2, %r8
movl %r8d, %r9d
andl $-0x2, %r9d
cmpq %r9, %rcx
ja 0x21741
movl 0xc(%rsp), %ecx
movl 0x10(%rsp), %ebx
cmpl %ebx, %ecx
cmovbl %ecx, %ebx
cmpq %rax, %r12
jne 0x217e7
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r15, %rdi
callq 0x2370
movq %rax, %r15
movq %r14, %rax
notl %ebx
shlq $0x20, %rbx
orq %r13, %rbx
movq %rbx, (%r15,%r12,8)
incq %r12
movl %r12d, %r13d
movq 0x20(%rsp), %rbx
cmpq %r13, 0x8(%rbx)
ja 0x216fd
jmp 0x2180e
xorl %r12d, %r12d
leaq (%r15,%r12,8), %rsi
movq %r15, %rdi
callq 0x16c477
cmpq $0x0, 0x8(%rbx)
je 0x219ac
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %eax, %eax
xorl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movq (%rbx), %rbp
movq (%r15,%rax,8), %rdx
movl %edx, %eax
imulq $0x18, %rax, %rbx
movq 0x268798(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
movq 0x8(%rbp,%rbx), %rcx
shrq %rcx
leaq 0x21003c(%rip), %rsi    # 0x231896
xorl %eax, %eax
callq 0x22b0
andl $0x0, 0x10(%rsp)
andq $0x0, 0x8(%rsp)
cmpq $0x0, 0x8(%rbp,%rbx)
jne 0x2187c
xorl %r12d, %r12d
jmp 0x21970
addq %rbx, %rbp
xorl %r14d, %r14d
xorl %r12d, %r12d
andl $0x0, 0x1c(%rsp)
andq $0x0, 0x14(%rsp)
movq 0x28(%rsp), %rax
movq 0xc8(%rax), %rax
movq 0x10(%rax), %rax
movq (%rbp), %rcx
movl (%rcx,%r14,4), %ecx
movl %ecx, %edx
shrl %edx
imulq $0x28, %rdx, %rsi
leaq (%rax,%rsi), %rbx
andl $0x1, %ecx
movl 0x10(%rax,%rsi), %eax
testq %rax, %rax
je 0x21922
leaq 0x269969(%rip), %rsi    # 0x28b230
movq 0x28(%rsi), %rsi
leal -0x1(%rax), %edi
shlq $0x3, %rax
xorl %r8d, %r8d
testl %ecx, %ecx
jne 0x218e2
movq 0x18(%rbx), %r9
addq %r8, %r9
jmp 0x218ed
movl %edi, %r9d
shlq $0x3, %r9
addq 0x18(%rbx), %r9
movq (%r9), %r9
cmpq $-0x1, %r9
je 0x21917
shrq $0x21, %r9
movzbl (%rsi,%r9), %r9d
leal -0x1(%r9), %r10d
cmpb $0x1, %r10b
ja 0x21917
shll $0x2, %r9d
incl 0x8(%rsp,%r9)
incl 0x14(%rsp,%r9)
addq $0x8, %r8
decl %edi
cmpq %r8, %rax
jne 0x218d5
movq 0x2686b7(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
incl %edx
movl (%rbx), %r8d
andl %r13d, %r8d
movl 0x18(%rsp), %r9d
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
leaq 0x20ff64(%rip), %rsi    # 0x2318ab
xorl %eax, %eax
callq 0x22b0
movl (%rbx), %eax
andl %r13d, %eax
addl $0x2, %r14d
movq 0x8(%rbp), %rcx
leal 0x64(%rax), %edx
cmpq %r14, %rcx
cmovbel %eax, %edx
addq %rdx, %r12
cmpq %r14, %rcx
ja 0x21885
movq 0x268669(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
movl 0xc(%rsp), %edx
movl 0x10(%rsp), %ecx
leaq 0x20ff65(%rip), %rsi    # 0x2318ee
movq %r12, %r8
xorl %eax, %eax
callq 0x22b0
movq 0x30(%rsp), %rcx
incl %ecx
movq %rcx, %rax
movq 0x20(%rsp), %rbx
cmpq %rcx, 0x8(%rbx)
ja 0x2182f
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220