// File: /workspace/github_cpp_makefile_star2_O1/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), %r11
leaq (%rax,%r11,8), %r8
movl 0x18(%r8), %ecx
testq %rcx, %rcx
je 0x14e853
movl 0x24(%r8), %edx
cmpl $0x1, %edx
je 0x14e853
cmpl $-0x1, %edx
jne 0x14ea97
cmpl $0x0, 0x1c(%r8)
je 0x14e86c
movl 0x28(%r8), %edx
cmpl $0x1, %edx
je 0x14e86c
cmpl $-0x1, %edx
jne 0x14ea97
movl 0x18(%rax,%r11,8), %r9d
movl 0x1c(%rax,%r11,8), %edx
shlq $0x2, %rcx
addq 0x10(%r8), %rcx
testq %rdx, %rdx
cmoveq %rdx, %rcx
movq %r9, %r10
testq %r9, %r9
je 0x14e892
movq 0x10(%rax,%r11,8), %r10
je 0x14e9a2
movq %r11, -0x8(%rsp)
leaq (%rax,%r11,8), %r8
addq $0x24, %r8
movq %r8, -0x10(%rsp)
movl (%r8), %r8d
movq %rsi, -0x20(%rsp)
movq 0x10(%rsi), %rsi
movq %rsi, -0x28(%rsp)
movq %r8, -0x18(%rsp)
leal (%r8,%r8), %esi
cvtsi2sd %esi, %xmm0
xorpd 0xe06f0(%rip), %xmm0    # 0x22efc0
xorl %r15d, %r15d
movl (%r10,%r15,4), %esi
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movq -0x28(%rsp), %r8
movq (%r8,%rsi), %r13
testq %r13, %r13
je 0x14e97c
movq -0x28(%rsp), %r11
leaq (%r11,%rsi), %r8
movq 0x10(%r8), %rbp
addq %rsi, %r11
addq $0x10, %r11
xorl %ebx, %ebx
leaq (%rbx,%rbx,4), %rsi
cmpb $0x0, 0x18(%rbp,%rsi,8)
js 0x14e972
movq (%r11), %r8
movl 0x8(%r8,%rsi,8), %r8d
xorl %r14d, %r14d
cmpl %r8d, (%r10,%r14,4)
je 0x14e972
incq %r14
cmpq %r14, %r9
jne 0x14e91d
movq (%r11), %r14
movl 0x8(%r14,%rsi,8), %r8d
movq 0x18(%rdi), %r12
movl (%r12,%r8,4), %r8d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %r12, %r8
je 0x14e972
leaq (%r14,%rsi,8), %rsi
movl %r8d, %r14d
shrl %r14d
andl $0x1, %r8d
leaq (%r14,%r14,8), %r14
leaq (%rax,%r14,8), %r14
movsd (%rsi), %xmm1
mulsd %xmm0, %xmm1
addsd 0x30(%r14,%r8,8), %xmm1
movsd %xmm1, 0x30(%r14,%r8,8)
incq %rbx
movl %ebx, %esi
cmpq %rsi, %r13
ja 0x14e907
incq %r15
cmpq %r9, %r15
jne 0x14e8d3
movq -0x18(%rsp), %r8
negl %r8d
movq -0x10(%rsp), %rsi
movl %r8d, (%rsi)
movq -0x20(%rsp), %rsi
movq -0x8(%rsp), %r11
testq %rdx, %rdx
je 0x14ea97
leaq (%rax,%r11,8), %r8
addq $0x28, %r8
movq %r8, -0x28(%rsp)
movl (%r8), %r8d
movq 0x10(%rsi), %rsi
movq %r8, -0x20(%rsp)
addl %r8d, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
xorpd 0xe05e9(%rip), %xmm0    # 0x22efc0
xorl %r10d, %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl (%rcx,%r10,4), %r8d
leaq (%r8,%r8,2), %r8
shlq $0x4, %r8
movq (%rsi,%r8), %rbx
testq %rbx, %rbx
je 0x14ea7d
leaq (%rsi,%r8), %r14
movq 0x10(%r14), %r14
leaq (%rsi,%r8), %r15
addq $0x10, %r15
xorl %r12d, %r12d
leaq (%r12,%r12,4), %r13
cmpb $0x0, 0x18(%r14,%r13,8)
js 0x14ea72
movq (%r15), %r8
movl 0x8(%r8,%r13,8), %ebp
xorl %r8d, %r8d
cmpl %ebp, (%rcx,%r8,4)
je 0x14ea72
incq %r8
cmpq %r8, %rdx
jne 0x14ea23
movq (%r15), %r8
movl 0x8(%r8,%r13,8), %ebp
movq 0x18(%rdi), %r9
movl (%r9,%rbp,4), %ebp
cmpq %r11, %rbp
je 0x14ea72
leaq (%r8,%r13,8), %r8
movl %ebp, %r9d
shrl %r9d
andl $0x1, %ebp
leaq (%r9,%r9,8), %r9
leaq (%rax,%r9,8), %r9
movsd (%r8), %xmm1
mulsd %xmm0, %xmm1
addsd 0x30(%r9,%rbp,8), %xmm1
movsd %xmm1, 0x30(%r9,%rbp,8)
incq %r12
movl %r12d, %r8d
cmpq %r8, %rbx
ja 0x14ea0c
incq %r10
cmpq %rdx, %r10
jne 0x14e9e0
movq -0x20(%rsp), %rcx
negl %ecx
movq -0x28(%rsp), %rax
movl %ecx, (%rax)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq