// File: /workspace/github_cpp_makefile_star2_O2/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
imulq $0x48, %rcx, %r8
leaq (%rax,%r8), %rdx
movl 0x18(%rax,%r8), %r9d
testq %r9, %r9
je 0x10dc40
movl 0x24(%rdx), %ecx
cmpl $0x1, %ecx
je 0x10dc40
cmpl $-0x1, %ecx
jne 0x10de49
movl 0x1c(%rdx), %ecx
testq %rcx, %rcx
je 0x10dc59
movl 0x28(%rdx), %edx
cmpl $0x1, %edx
je 0x10dc59
cmpl $-0x1, %edx
jne 0x10de49
testq %rcx, %rcx
movq 0x10(%rax,%r8), %r10
leaq (%r10,%r9,4), %rdx
cmoveq %rcx, %rdx
testq %r9, %r9
cmoveq %r9, %r10
je 0x10dd6f
movq %r8, -0x10(%rsp)
addq %rax, %r8
addq $0x24, %r8
movq %r8, -0x18(%rsp)
movl (%r8), %r8d
movq %rsi, -0x8(%rsp)
movq 0x10(%rsi), %rsi
movq %rsi, -0x30(%rsp)
movq %r8, -0x20(%rsp)
leal (%r8,%r8), %esi
cvtsi2sd %esi, %xmm0
xorpd 0xdf413(%rip), %xmm0    # 0x1ed0c0
xorl %esi, %esi
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %r9, %rsi
je 0x10dd55
movq %rsi, -0x28(%rsp)
movl (%r10,%rsi,4), %esi
imulq $0x30, %rsi, %rsi
movq -0x30(%rsp), %r8
leaq (%r8,%rsi), %r13
movq (%r8,%rsi), %rbp
xorl %r11d, %r11d
movl %r11d, %r11d
cmpq %r11, %rbp
jbe 0x10dd48
movq 0x10(%r13), %rbx
imulq $0x28, %r11, %rsi
cmpb $0x0, 0x18(%rbx,%rsi)
js 0x10dd43
movl 0x8(%rbx,%rsi), %r8d
xorl %r14d, %r14d
cmpq %r14, %r9
je 0x10dd0e
leaq 0x1(%r14), %r15
cmpl %r8d, (%r10,%r14,4)
movq %r15, %r14
jne 0x10dcfa
jmp 0x10dd43
movq 0x18(%rdi), %r14
movl (%r14,%r8,4), %r8d
cmpq %r12, %r8
je 0x10dd43
movl %r8d, %r14d
shrl %r14d
andl $0x1, %r8d
imulq $0x48, %r14, %r14
addq %rax, %r14
movsd (%rbx,%rsi), %xmm1
mulsd %xmm0, %xmm1
addsd 0x30(%r14,%r8,8), %xmm1
movsd %xmm1, 0x30(%r14,%r8,8)
incl %r11d
jmp 0x10dcdb
movq -0x28(%rsp), %rsi
incq %rsi
jmp 0x10dcb5
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 0x10de49
addq %rax, %r8
addq $0x28, %r8
movq %r8, -0x28(%rsp)
movl (%r8), %r8d
movq 0x10(%rsi), %rsi
movq %r8, -0x30(%rsp)
addl %r8d, %r8d
cvtsi2sd %r8d, %xmm0
xorpd 0xdf320(%rip), %xmm0    # 0x1ed0c0
xorl %r10d, %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
cmpq %rcx, %r10
je 0x10de3b
movl (%rdx,%r10,4), %r8d
imulq $0x30, %r8, %r8
leaq (%rsi,%r8), %rbx
movq (%rsi,%r8), %r14
xorl %r15d, %r15d
movl %r15d, %r15d
cmpq %r15, %r14
jbe 0x10de33
movq 0x10(%rbx), %r12
imulq $0x28, %r15, %r13
cmpb $0x0, 0x18(%r12,%r13)
js 0x10de2e
movl 0x8(%r12,%r13), %ebp
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0x10ddf9
leaq 0x1(%r8), %r9
cmpl %ebp, (%rdx,%r8,4)
movq %r9, %r8
jne 0x10dde5
jmp 0x10de2e
movq 0x18(%rdi), %r8
movl (%r8,%rbp,4), %ebp
cmpq %r11, %rbp
je 0x10de2e
movl %ebp, %r8d
shrl %r8d
andl $0x1, %ebp
imulq $0x48, %r8, %r8
addq %rax, %r8
movsd (%r12,%r13), %xmm1
mulsd %xmm0, %xmm1
addsd 0x30(%r8,%rbp,8), %xmm1
movsd %xmm1, 0x30(%r8,%rbp,8)
incl %r15d
jmp 0x10ddc5
incq %r10
jmp 0x10dda9
movq -0x30(%rsp), %rcx
negl %ecx
movq -0x28(%rsp), %rax
movl %ecx, (%rax)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq