// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: flip_unitig(G_partition*, hc_links*, bubble_type*, unsigned int)
void flip_unitig(G_partition* g_p, hc_links* link, bubble_type* bub, uint32_t id)
{
    if(g_p->a[id].h[0] > 0 && g_p->a[id].status[0] != 1 && g_p->a[id].status[0] != -1) return;
    if(g_p->a[id].h[1] > 0 && g_p->a[id].status[1] != 1 && g_p->a[id].status[1] != -1) return;
    uint32_t k, j, m, *h0, h0_n, *h1, h1_n, uID, *h = NULL, h_n;
    int status;
    double weight;
    get_phased_block(g_p, NULL, id, NULL, NULL, &h0, &h0_n, &h1, &h1_n, NULL, NULL);
    // fprintf(stderr, "h0_n: %u, h1_n: %u\n", h0_n, h1_n);
    if(h0_n > 0)
    {

        status = g_p->a[id].status[0];
        // fprintf(stderr, "+status: %d\n", status);
        h = h0; h_n = h0_n;
        for (j = 0; j < h_n; j++)
        {
            // fprintf(stderr, "+j: %u, h_n: %u\n", j, h_n);
            for (k = 0; k < link->a.a[h[j]].e.n; k++)
            {
                // fprintf(stderr, "+k: %u, e_n: %u\n", k, (uint32_t)link->a.a[h[j]].e.n);
                if(link->a.a[h[j]].e.a[k].del) continue;
                for (m = 0; m < h_n; m++)
                {
                    if(h[m] == link->a.a[h[j]].e.a[k].uID) break;
                }
                // fprintf(stderr, "+m: %u, h_n: %u\n", m, h_n);
                if(m < h_n) continue;

                uID = link->a.a[h[j]].e.a[k].uID;

                weight = link->a.a[h[j]].e.a[k].weight;

                if(g_p->index[uID] == (uint32_t)-1) continue;
                

                g_p->a[g_p->index[uID]>>1].weight[g_p->index[uID]&1] -= (2*status*weight);

                
            }
        }
        g_p->a[id].status[0] *= -1;
    }

    // fprintf(stderr, "hehehe\n");
    if(h1_n > 0)
    {
        status = g_p->a[id].status[1];
        // fprintf(stderr, "-status: %d\n", status);
        h = h1; h_n = h1_n;
        for (j = 0; j < h_n; j++)
        {
            // fprintf(stderr, "-j: %u, h_n: %u\n", j, h_n);
            for (k = 0; k < link->a.a[h[j]].e.n; k++)
            {
                // fprintf(stderr, "-k: %u, e_n: %u\n", k, (uint32_t)link->a.a[h[j]].e.n);
                if(link->a.a[h[j]].e.a[k].del) continue;
                for (m = 0; m < h_n; m++)
                {
                    if(h[m] == link->a.a[h[j]].e.a[k].uID) break;
                }
                // fprintf(stderr, "-m: %u, h_n: %u\n", m, h_n);
                if(m < h_n) continue;

                uID = link->a.a[h[j]].e.a[k].uID;
                // fprintf(stderr, "-uID: %u\n", uID);

                weight = link->a.a[h[j]].e.a[k].weight;

                if(g_p->index[uID] == (uint32_t)-1) continue;


                g_p->a[g_p->index[uID]>>1].weight[g_p->index[uID]&1] -= (2*status*weight);

            }
        }
        g_p->a[id].status[1] *= -1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x10(%rdi), %rax
movl %ecx, %ecx
leaq (%rcx,%rcx,8), %r8
leaq (%rax,%r8,8), %rdx
movl 0x18(%rdx), %r9d
testq %r9, %r9
je 0x154de9
movl 0x24(%rdx), %ecx
cmpl $0x1, %ecx
je 0x154de9
cmpl $-0x1, %ecx
jne 0x154fdf
movl 0x1c(%rdx), %ecx
testq %rcx, %rcx
je 0x154e02
movl 0x28(%rdx), %edx
cmpl $0x1, %edx
je 0x154e02
cmpl $-0x1, %edx
jne 0x154fdf
testq %rcx, %rcx
movq 0x10(%rax,%r8,8), %r10
leaq (%r10,%r9,4), %rdx
cmoveq %rcx, %rdx
testq %r9, %r9
cmoveq %r9, %r10
je 0x154f0b
movq %r8, -0x10(%rsp)
leaq (%rax,%r8,8), %r8
addq $0x24, %r8
movq %r8, -0x18(%rsp)
movl (%r8), %r8d
movq %rsi, -0x8(%rsp)
movq 0x10(%rsi), %r14
movq %r8, -0x20(%rsp)
leal (%r8,%r8), %esi
cvtsi2sd %esi, %xmm0
xorpd 0xe637e(%rip), %xmm0    # 0x23b1d0
xorl %r15d, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl (%r10,%r15,4), %esi
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movq (%r14,%rsi), %r13
testq %r13, %r13
je 0x154ee5
addq %r14, %rsi
movq 0x10(%rsi), %rbp
xorl %r11d, %r11d
leaq (%r11,%r11,4), %rbx
cmpb $0x0, 0x18(%rbp,%rbx,8)
js 0x154eda
movl 0x8(%rbp,%rbx,8), %esi
xorl %r8d, %r8d
cmpl %esi, (%r10,%r8,4)
je 0x154eda
incq %r8
cmpq %r8, %r9
jne 0x154e8c
movl 0x8(%rbp,%rbx,8), %esi
movq 0x18(%rdi), %r8
movl (%r8,%rsi,4), %esi
cmpq %r12, %rsi
je 0x154eda
leaq (,%rbx,8), %r8
addq %rbp, %r8
movl %esi, %ebx
shrl %ebx
andl $0x1, %esi
leaq (%rbx,%rbx,8), %rbx
leaq (%rax,%rbx,8), %rbx
movsd (%r8), %xmm1
mulsd %xmm0, %xmm1
addsd 0x30(%rbx,%rsi,8), %xmm1
movsd %xmm1, 0x30(%rbx,%rsi,8)
incq %r11
movl %r11d, %esi
cmpq %rsi, %r13
ja 0x154e7a
incq %r15
cmpq %r9, %r15
jne 0x154e5b
movq -0x20(%rsp), %r8
negl %r8d
movq -0x18(%rsp), %rsi
movl %r8d, (%rsi)
movq -0x8(%rsp), %rsi
movq -0x10(%rsp), %r8
testq %rcx, %rcx
je 0x154fdf
leaq (%rax,%r8,8), %r8
addq $0x28, %r8
movl (%r8), %r9d
movq 0x10(%rsi), %rsi
leal (%r9,%r9), %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
xorpd 0xe6299(%rip), %xmm0    # 0x23b1d0
xorl %r10d, %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl (%rdx,%r10,4), %ebx
leaq (%rbx,%rbx,2), %r14
shlq $0x4, %r14
movq (%rsi,%r14), %rbx
testq %rbx, %rbx
je 0x154fcd
addq %rsi, %r14
movq 0x10(%r14), %r14
xorl %r15d, %r15d
leaq (%r15,%r15,4), %r12
cmpb $0x0, 0x18(%r14,%r12,8)
js 0x154fc2
movl 0x8(%r14,%r12,8), %ebp
xorl %r13d, %r13d
cmpl %ebp, (%rdx,%r13,4)
je 0x154fc2
incq %r13
cmpq %r13, %rcx
jne 0x154f73
movl 0x8(%r14,%r12,8), %r13d
movq 0x18(%rdi), %rbp
movl (%rbp,%r13,4), %r13d
cmpq %r11, %r13
je 0x154fc2
leaq (%r14,%r12,8), %r12
movl %r13d, %ebp
shrl %ebp
andl $0x1, %r13d
leaq (%rbp,%rbp,8), %rbp
leaq (%rax,%rbp,8), %rbp
movsd (%r12), %xmm1
mulsd %xmm0, %xmm1
addsd 0x30(%rbp,%r13,8), %xmm1
movsd %xmm1, 0x30(%rbp,%r13,8)
incq %r15
movl %r15d, %r12d
cmpq %r12, %rbx
ja 0x154f5f
incq %r10
cmpq %rcx, %r10
jne 0x154f40
negl %r9d
movl %r9d, (%r8)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq