// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq %rdi, %r14
movq 0x11915c(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xb5af2(%rip), %rsi     # 0x1ef980
leaq 0xb5af7(%rip), %rdx     # 0x1ef98c
xorl %eax, %eax
callq 0x22b0
xorl %eax, %eax
xorl %ecx, %ecx
xorl %r12d, %r12d
movq %rbx, 0x40(%rsp)
movl %r12d, %r13d
cmpq %r13, 0x8(%rbx)
movq %rax, 0x20(%rsp)
jbe 0x139fb7
movq %rcx, 0x28(%rsp)
movq (%rbx), %rdx
andl $0x0, 0x1c(%rsp)
imulq $0x18, %r13, %rsi
andq $0x0, 0x14(%rsp)
leaq (%rdx,%rsi), %rcx
movq 0x8(%rdx,%rsi), %rdx
leaq 0x11a34f(%rip), %rax    # 0x254230
movq 0x28(%rax), %rsi
xorl %edi, %edi
movl %edi, %edi
cmpq %rdi, %rdx
jbe 0x139f58
movq 0xc8(%r14), %r8
movq 0x10(%r8), %r10
movq (%rcx), %r8
movl (%r8,%rdi,4), %r8d
movl %r8d, %r9d
shrl %r9d
imulq $0x28, %r9, %r11
leaq (%r10,%r11), %r9
movl 0x10(%r10,%r11), %r11d
leal -0x1(%r11), %r10d
negq %r11
xorl %r15d, %r15d
movq %r11, %rbp
addq %r15, %rbp
je 0x139f53
testb $0x1, %r8b
movq 0x18(%r9), %rbp
movl %r10d, %ebx
cmoveq %r15, %rbx
movq (%rbp,%rbx,8), %rbp
cmpq $-0x1, %rbp
je 0x139f4b
shrq $0x21, %rbp
movzbl (%rsi,%rbp), %ebx
incl 0x14(%rsp,%rbx,4)
incq %r15
decl %r10d
jmp 0x139f1d
addl $0x2, %edi
jmp 0x139ee7
movl 0x18(%rsp), %ecx
movl 0x1c(%rsp), %r15d
cmpl %r15d, %ecx
cmovbl %ecx, %r15d
movq 0x28(%rsp), %rcx
cmpq %rcx, %r12
jne 0x139f97
leaq (%rcx,%rcx), %rbx
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x2370
movq %rbx, %rcx
jmp 0x139f9c
movq 0x20(%rsp), %rax
notl %r15d
shlq $0x20, %r15
orq %r13, %r15
movq %r15, (%rax,%r12,8)
incq %r12
movq 0x40(%rsp), %rbx
jmp 0x139ea8
leaq (%rax,%r12,8), %rsi
movq %rax, %rdi
callq 0x12f210
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, 0x8(%rbx)
jbe 0x13a139
movq (%rbx), %r15
movq 0x20(%rsp), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rdx
movl %edx, %eax
imulq $0x18, %rax, %r12
leaq (%r15,%r12), %r13
movq 0x118fe8(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq 0x8(%r15,%r12), %rcx
shrq %rcx
xorl %ebx, %ebx
leaq 0xb599a(%rip), %rsi     # 0x1ef9a6
xorl %eax, %eax
callq 0x22b0
andl $0x0, 0x1c(%rsp)
andq $0x0, 0x14(%rsp)
movq 0x8(%r15,%r12), %rax
xorl %r12d, %r12d
movl %ebx, %ebx
cmpq %rbx, %rax
jbe 0x13a105
andl $0x0, 0x3c(%rsp)
andq $0x0, 0x34(%rsp)
movq 0xc8(%r14), %rax
movq 0x10(%rax), %rax
movq (%r13), %rcx
movl (%rcx,%rbx,4), %ecx
movl %ecx, %edx
shrl %edx
imulq $0x28, %rdx, %rsi
leaq (%rax,%rsi), %r15
andl $0x1, %ecx
movl 0x10(%rax,%rsi), %edi
leaq 0x11a1c8(%rip), %rax    # 0x254230
movq 0x28(%rax), %rax
leal -0x1(%rdi), %esi
negq %rdi
xorl %r8d, %r8d
movq %rdi, %r9
addq %r8, %r9
je 0x13a0bc
testl %ecx, %ecx
movq 0x18(%r15), %r9
movl %esi, %r10d
cmoveq %r8, %r10
movq (%r9,%r10,8), %r9
cmpq $-0x1, %r9
je 0x13a0b5
shrq $0x21, %r9
movzbl (%rax,%r9), %r9d
leal -0x1(%r9), %r10d
cmpb $0x1, %r10b
ja 0x13a0b5
shll $0x2, %r9d
incl 0x14(%rsp,%r9)
incl 0x34(%rsp,%r9)
incq %r8
decl %esi
jmp 0x13a075
movq 0x118f1d(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
incl %edx
movl (%r15), %r8d
andl %ebp, %r8d
movl 0x38(%rsp), %r9d
movl 0x3c(%rsp), %eax
movl %eax, (%rsp)
leaq 0xb58da(%rip), %rsi     # 0x1ef9bb
xorl %eax, %eax
callq 0x22b0
movl (%r15), %ecx
andl %ebp, %ecx
addl $0x2, %ebx
movq 0x8(%r13), %rax
leal 0x64(%rcx), %edx
cmpq %rbx, %rax
cmovbel %ecx, %edx
addq %rdx, %r12
jmp 0x13a026
movq 0x118ed4(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movl 0x18(%rsp), %edx
movl 0x1c(%rsp), %ecx
leaq 0xb58e0(%rip), %rsi     # 0x1ef9fe
movq %r12, %r8
xorl %eax, %eax
callq 0x22b0
movq 0x28(%rsp), %rcx
incl %ecx
movq 0x40(%rsp), %rbx
jmp 0x139fca
movq 0x20(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220