// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rsi, %r14
movq %rdi, 0x38(%rsp)
movq 0x27452d(%rip), %r15    # 0x294fe0
movq (%r15), %rdi
leaq 0x21d003(%rip), %rsi    # 0x23dac0
leaq 0x21d008(%rip), %rdx    # 0x23dacc
xorl %eax, %eax
callq 0x22b0
cmpq $0x0, 0x8(%r14)
je 0x20bf6
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %edi, %edi
movq (%r14), %rdx
andl $0x0, 0x14(%rsp)
imulq $0x18, %r13, %rsi
andq $0x0, 0xc(%rsp)
movq 0x8(%rdx,%rsi), %rax
movl $0x0, %ebx
movl $0x0, %ecx
testq %rax, %rax
je 0x20baf
movq %rdi, 0x18(%rsp)
movq %r14, %r15
addq %rsi, %rdx
movq 0x38(%rsp), %rcx
movq 0xc8(%rcx), %rcx
movq 0x10(%rcx), %rcx
movq (%rdx), %rdx
leaq 0x275701(%rip), %rsi    # 0x296230
movq 0x28(%rsi), %rsi
xorl %edi, %edi
movl (%rdx,%rdi,4), %r8d
movl %r8d, %r9d
shrl %r9d
imulq $0x28, %r9, %r10
movl 0x10(%rcx,%r10), %r9d
testq %r9, %r9
je 0x20b88
addq %rcx, %r10
movq 0x18(%r10), %r10
leal -0x1(%r9), %r11d
xorl %ebx, %ebx
testb $0x1, %r8b
movl %r11d, %r14d
cmoveq %rbx, %r14
movq (%r10,%r14,8), %r14
cmpq $-0x1, %r14
je 0x20b7d
shrq $0x21, %r14
movzbl (%rsi,%r14), %r14d
incl 0xc(%rsp,%r14,4)
incq %rbx
decl %r11d
cmpq %rbx, %r9
jne 0x20b5a
addq $0x2, %rdi
movl %edi, %r8d
andl $-0x2, %r8d
cmpq %r8, %rax
ja 0x20b35
movl 0x10(%rsp), %ecx
movl 0x14(%rsp), %ebx
movq %r15, %r14
movq 0x274436(%rip), %r15    # 0x294fe0
movq 0x18(%rsp), %rdi
cmpl %ebx, %ecx
cmovbl %ecx, %ebx
cmpq %rbp, %r12
jne 0x20bd7
leaq (%r12,%r12), %rbp
testq %r12, %r12
pushq $0x2
popq %rax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
callq 0x2370
movq %rax, %rdi
notl %ebx
shlq $0x20, %rbx
orq %r13, %rbx
movq %rbx, (%rdi,%r12,8)
incq %r12
movl %r12d, %r13d
cmpq %r13, 0x8(%r14)
ja 0x20ae0
jmp 0x20bfb
xorl %r12d, %r12d
xorl %edi, %edi
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0x18(%rsp)
callq 0x172ebc
cmpq $0x0, 0x8(%r14)
je 0x20d92
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %eax, %eax
xorl %ebx, %ebx
movq %r14, 0x40(%rsp)
movq %rbx, 0x30(%rsp)
movq (%r14), %rbp
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rdx
movl %edx, %eax
imulq $0x18, %rax, %rbx
movq (%r15), %rdi
movq 0x8(%rbp,%rbx), %rcx
shrq %rcx
leaq 0x21ce9a(%rip), %rsi    # 0x23dae6
xorl %eax, %eax
callq 0x22b0
andl $0x0, 0x14(%rsp)
andq $0x0, 0xc(%rsp)
cmpq $0x0, 0x8(%rbp,%rbx)
jne 0x20c72
xorl %ecx, %ecx
xorl %edx, %edx
xorl %r12d, %r12d
jmp 0x20d6a
addq %rbx, %rbp
xorl %r14d, %r14d
xorl %r12d, %r12d
andl $0x0, 0x2c(%rsp)
andq $0x0, 0x24(%rsp)
movq 0x38(%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
movl $0x0, %esi
movl $0x0, %r9d
testq %rax, %rax
je 0x20d1f
leaq 0x275568(%rip), %rsi    # 0x296230
movq 0x28(%rsi), %rsi
movq 0x18(%rbx), %rdi
leal -0x1(%rax), %r8d
xorl %r9d, %r9d
testl %ecx, %ecx
movl %r8d, %r10d
cmoveq %r9, %r10
movq (%rdi,%r10,8), %r10
cmpq $-0x1, %r10
je 0x20d0b
shrq $0x21, %r10
movzbl (%rsi,%r10), %r10d
leal -0x1(%r10), %r11d
cmpb $0x1, %r11b
ja 0x20d0b
shll $0x2, %r10d
incl 0xc(%rsp,%r10)
incl 0x24(%rsp,%r10)
incq %r9
decl %r8d
cmpq %r9, %rax
jne 0x20cd7
movl 0x28(%rsp), %r9d
movl 0x2c(%rsp), %esi
movq (%r15), %rdi
incl %edx
movl (%rbx), %r8d
andl %r13d, %r8d
movl %esi, (%rsp)
leaq 0x21cdc7(%rip), %rsi    # 0x23dafb
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 0x20c7b
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %ecx
movq 0x40(%rsp), %r14
movq 0x30(%rsp), %rbx
movq (%r15), %rdi
leaq 0x21cdc5(%rip), %rsi    # 0x23db3e
movq %r12, %r8
xorl %eax, %eax
callq 0x22b0
incl %ebx
movq %rbx, %rax
cmpq %rbx, 0x8(%r14)
ja 0x20c23
movq 0x18(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220