// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: init_phase(ha_ug_index*, kv_u_trans_t*, bubble_type*, ps_t*)
void init_phase(ha_ug_index* idx, kv_u_trans_t *ta, bubble_type* bub, ps_t *st)
{
    double index_time = yak_realtime();
    uint8_t *vis = NULL; CALLOC(vis, idx->ug->g->n_seq);
    bub_p_t_warp b; memset(&b, 0, sizeof(bub_p_t_warp));
    CALLOC(b.a, idx->ug->g->n_seq*2);
    uint32_t i, k, *a = NULL, n, beg, end;
    memset(st->s, 0, sizeof(int8_t)*idx->ug->g->n_seq);
    
    double z[2];
    u_trans_t *e = NULL;
    uint32_t e_n;
    while(1)
    {
        i = get_weightest_node(ta, bub, idx->ug, st->s, vis);
        if(i == (uint32_t)-1) break;
        if(i&1)
        {
            i>>=1;
            z[0] = z[1] = 0;
            e = u_trans_a(*ta, i);
            e_n = u_trans_n(*ta, i);
            for (k = 0; k < e_n; k++)
            {
                if(e[k].del) continue;
                if(st->s[e[k].tn] > 0) z[0] += e[k].nw;
                else if(st->s[e[k].tn] < 0) z[1] += e[k].nw;
            }
            if(z[0] - z[1] < 0)
            {
                st->s[i] = 1;
            } 
            else if(z[0] - z[1] > 0)
            {
                st->s[i] = -1;
            }
            else
            {
                st->xs = kr_splitmix64(st->xs);
                st->s[i] = st->xs&1? 1 : -1;
            }
        }
        else
        {
            i>>=1;
            get_bubbles(bub, i, &beg, &end, &a, &n, NULL);
            bub_phase(idx->ug, beg, end, &b, ta, st);
            bub_phase(idx->ug, beg, end, &b, ta, st);
        }
    }
    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
    free(vis);

    for (i = 0; i < idx->ug->g->n_seq; i++)
    {
        if(st->s[i] == 0) fprintf(stderr, "ERROR\n");
        if(u_trans_n(*ta, i) == 0) st->s[i] = 0;
    }

    fprintf(stderr, "[M::%s::%.3f]\n", __func__, yak_realtime()-index_time);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, %r14
movq %rdx, %rbp
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
callq 0xefea7
movsd %xmm0, 0x30(%rsp)
movq (%r15), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %r13d
movl %r13d, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, 0x28(%rsp)
xorpd %xmm0, %xmm0
leaq 0x48(%rsp), %rbx
movupd %xmm0, 0x58(%rbx)
movupd %xmm0, 0x48(%rbx)
movupd %xmm0, 0x38(%rbx)
movupd %xmm0, 0x28(%rbx)
movupd %xmm0, 0x18(%rbx)
movupd %xmm0, 0x8(%rbx)
andq $0x0, 0x68(%rbx)
addl %r13d, %r13d
pushq $0x38
popq %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, (%rbx)
leaq 0x38(%rsp), %rax
andq $0x0, (%rax)
movq (%r14), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %rbp, %r12
callq 0x20e0
movq 0x10(%rsp), %rdi
movq %r15, 0x18(%rsp)
movq (%r15), %r13
movq %r14, %rbx
movq (%r14), %rbp
movq %r12, %r15
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movq 0x28(%rsp), %r8
callq 0x111819
cmpl $-0x1, %eax
je 0x111d63
movl %eax, %r12d
shrl %r12d
testb $0x1, %al
jne 0x111c9b
movl %r12d, %esi
andq $0x0, (%rsp)
movq %r15, %r12
movq %r15, %rdi
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x44(%rsp), %r9
callq 0xf6ab5
movl 0x24(%rsp), %esi
movl 0x20(%rsp), %edx
movq %r13, %rdi
leaq 0x48(%rsp), %rbp
movq %rbp, %rcx
movq 0x10(%rsp), %r13
movq %r13, %r8
movq %rbx, %r14
movq %rbx, %r9
callq 0x111048
movq 0x18(%rsp), %r15
movq (%r15), %rdi
movl 0x24(%rsp), %esi
movl 0x20(%rsp), %edx
movq %rbp, %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0x111048
movq %r13, %rdi
jmp 0x111bf6
movq 0x10(%rsp), %rdi
movq 0x10(%rdi), %rcx
movq 0x28(%rdi), %rax
movq (%rax,%r12,8), %rax
movl %eax, %edx
movq %rax, %rsi
shrq $0x20, %rsi
imulq $0x30, %rdx, %rax
imulq $0x30, %rsi, %rdx
addq %rdx, %rcx
addq $0x28, %rcx
xorpd %xmm1, %xmm1
xorl %edx, %edx
xorpd %xmm0, %xmm0
cmpq %rdx, %rax
je 0x111cfa
cmpb $0x0, (%rcx,%rdx)
js 0x111cf4
movl -0x14(%rcx,%rdx), %esi
cmpb $0x0, (%rbp,%rsi)
jle 0x111cec
addsd -0x8(%rcx,%rdx), %xmm0
jmp 0x111cf4
jns 0x111cf4
addsd -0x8(%rcx,%rdx), %xmm1
addq $0x30, %rdx
jmp 0x111cce
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x111d10
movb $0x1, (%rbp,%r12)
jmp 0x111d1c
ucomisd %xmm1, %xmm0
jbe 0x111d2c
movb $-0x1, (%rbp,%r12)
movq %r15, %r12
movq 0x18(%rsp), %r15
movq %rbx, %r14
jmp 0x111bf6
movq %rbx, %r14
movq 0x8(%rbx), %rdi
callq 0x111000
movq 0x10(%rsp), %rdi
movq %rax, 0x8(%rbx)
andl $0x1, %eax
cmpq $0x1, %rax
movl $0x0, %eax
sbbl %eax, %eax
orb $0x1, %al
movb %al, (%rbp,%r12)
movq %r15, %r12
movq 0x18(%rsp), %r15
jmp 0x111bf6
movq 0x48(%rsp), %rdi
callq 0x2220
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
movq 0x28(%rsp), %rdi
callq 0x2220
xorl %r12d, %r12d
movq 0x141231(%rip), %r15    # 0x252fe0
pushq $0x6
popq %r13
pushq $0x1
popq %rbp
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rcx
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jae 0x111e12
movq (%rbx), %rax
cmpb $0x0, (%rax,%r12)
je 0x111df6
movq 0x28(%rcx), %rax
cmpl $0x0, (%rax,%r12,8)
jne 0x111df1
movq (%rbx), %rax
movb $0x0, (%rax,%r12)
incq %r12
jmp 0x111dc0
movq (%r15), %rcx
leaq 0xd8f84(%rip), %rdi     # 0x1ead84
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2410
movq 0x10(%rsp), %rcx
jmp 0x111dde
movq (%r15), %rbx
callq 0xefea7
subsd 0x30(%rsp), %xmm0
leaq 0xdbcb4(%rip), %rsi     # 0x1edadb
leaq 0xdc2e8(%rip), %rdx     # 0x1ee116
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq