// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/horder.cpp
// Function: insert_sc(osg_t*, lay_t*, unsigned int)
void insert_sc(osg_t *g, lay_t *p, uint32_t uid)
{
    uint32_t i, b, e, vb, ve, max_i = (uint32_t)-1, is_found, ori, max_ori = (uint32_t)-1;
    osg_arc_t *bE = NULL, *eE = NULL;
    double w[2], s_w, max_w = -1;
    for (i = 1; i+1 < p->n; i++)///middle points
    {
        b = p->a[i]; e = p->a[i+1]; w[0] = w[1] = 0; 
        s_w = 0; ori = 0; is_found = 0;

        vb = (uid<<1); ve = (uid<<1)+1;
        bE = get_osg_arc(g, vb, b);
        eE = get_osg_arc(g, ve, e);
        if(bE || eE) ///different with slsa2
        {
            if(bE) w[0] += bE->nw;
            if(eE) w[0] += eE->nw;
            is_found++;
        }


        vb = (uid<<1)+1; ve = (uid<<1);
        bE = get_osg_arc(g, vb, b);
        eE = get_osg_arc(g, ve, e);
        if(bE || eE) ///different with slsa2
        {
            if(bE) w[1] += bE->nw;
            if(eE) w[1] += eE->nw;
            is_found++;
        }

        if(is_found > 0)
        {
            s_w = MAX(w[0], w[1]);
            ori = ((w[0] >= w[1])? 0 : 1);
            
            if(max_i == (uint32_t)-1 || max_w < s_w)
            {
                max_w = s_w;
                max_i = i;
                max_ori = ori;
            }
        }
    }


    ///beg point, ///different with slsa2
    i = 0;
    b = (uint32_t)-1; e = p->a[0]; w[0] = w[1] = 0; 
    s_w = 0; ori = 0; is_found = 0;

    vb = (uint32_t)-1; ve = (uid<<1)+1;
    bE = NULL;
    eE = get_osg_arc(g, ve, e);
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[0] += bE->nw;
        if(eE) w[0] += eE->nw;
        is_found++;
    }

    vb = (uint32_t)-1; ve = (uid<<1);
    bE = NULL;
    eE = get_osg_arc(g, ve, e);
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[1] += bE->nw;
        if(eE) w[1] += eE->nw;
        is_found++;
    }

    if(is_found > 0)
    {
        s_w = MAX(w[0], w[1]);
        ori = ((w[0] >= w[1])? 0 : 1);
        
        if(max_i == (uint32_t)-1 || max_w < s_w)
        {
            max_w = s_w;
            max_i = i;
            max_ori = ori;
        }
    }

    ///end point, ///different with slsa2
    i = p->n - 1;
    b = p->a[p->n - 1]; e = (uint32_t)-1; w[0] = w[1] = 0; 
    s_w = 0; ori = 0; is_found = 0;

    vb = (uid<<1); ve = (uint32_t)-1;
    bE = get_osg_arc(g, vb, b);
    eE = NULL;
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[0] += bE->nw;
        if(eE) w[0] += eE->nw;
        is_found++;
    }

    vb = (uid<<1)+1; ve = (uint32_t)-1;
    bE = get_osg_arc(g, vb, b);
    eE = NULL;
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[1] += bE->nw;
        if(eE) w[1] += eE->nw;
        is_found++;
    }

    if(is_found > 0)
    {
        s_w = MAX(w[0], w[1]);
        ori = ((w[0] >= w[1])? 0 : 1);
        
        if(max_i == (uint32_t)-1 || max_w < s_w)
        {
            max_w = s_w;
            max_i = i;
            max_ori = ori;
        }
    }

    if(max_i != 0 && max_i != (uint32_t)-1) max_i++;

    i = p->n;
    kv_resize(uint32_t, *p, p->n+2);
    p->n += 2; 
    while (i > max_i)
    {
        i--;
        p->a[i+2] = p->a[i];
    }
    
    p->a[max_i] = (uid<<1) + max_ori;
    p->a[max_i+1] = (uid<<1) + 1 - max_ori;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, 0x20(%rsp)
movq (%rsi), %r12
movq 0x8(%rsi), %r14
leal (%rdx,%rdx), %ebp
leal 0x1(,%rdx,2), %eax
movl %eax, 0x14(%rsp)
cmpq $0x3, %r14
movq %rsi, 0x28(%rsp)
jb 0x17cbe6
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movsd 0xb4ba4(%rip), %xmm0    # 0x231638
movsd %xmm0, 0x18(%rsp)
movl $0x2, %eax
movl $0x1, %ebx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r12, 0x30(%rsp)
movl %ebp, 0x3c(%rsp)
movq %r13, 0x40(%rsp)
movl %ebx, %ecx
movl (%r12,%rcx,4), %r15d
movl (%r12,%rax,4), %r13d
movq 0x20(%rsp), %r12
movq %r12, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x17ca18
movq %rax, %rbp
movq %r12, %rdi
movl 0x14(%rsp), %esi
movl %r13d, %edx
callq 0x17ca18
movq %rbp, %rcx
orq %rax, %rcx
sete 0x13(%rsp)
xorpd %xmm0, %xmm0
je 0x17cb13
testq %rbp, %rbp
je 0x17cb09
movsd 0x18(%rbp), %xmm0
addsd 0xb4b0f(%rip), %xmm0    # 0x231618
testq %rax, %rax
je 0x17cb13
addsd 0x18(%rax), %xmm0
movapd %xmm0, 0x50(%rsp)
movq 0x20(%rsp), %r12
movq %r12, %rdi
movl 0x14(%rsp), %esi
movl %r15d, %edx
callq 0x17ca18
movq %rax, %r15
movq %r12, %rdi
movl 0x3c(%rsp), %ebp
movl %ebp, %esi
movl %r13d, %edx
callq 0x17ca18
movq %r15, %rcx
orq %rax, %rcx
je 0x17cb63
testq %r15, %r15
movq 0x40(%rsp), %r13
je 0x17cb80
movsd 0x18(%r15), %xmm0
addsd 0xb4ab7(%rip), %xmm0    # 0x231618
jmp 0x17cb84
xorpd %xmm0, %xmm0
cmpb $0x0, 0x13(%rsp)
movq 0x30(%rsp), %r12
movq 0x40(%rsp), %r13
movapd 0x50(%rsp), %xmm3
je 0x17cb99
jmp 0x17cbd6
xorpd %xmm0, %xmm0
movq 0x30(%rsp), %r12
movapd 0x50(%rsp), %xmm3
testq %rax, %rax
je 0x17cb99
addsd 0x18(%rax), %xmm0
movapd %xmm0, %xmm1
cmpnlesd %xmm3, %xmm1
movapd %xmm1, %xmm2
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm1
orpd %xmm2, %xmm1
cmpl $-0x1, %r13d
je 0x17cbc0
ucomisd 0x18(%rsp), %xmm1
jbe 0x17cbd6
xorl %eax, %eax
ucomisd %xmm0, %xmm3
setb %al
movl %eax, 0xc(%rsp)
movl %ebx, %r13d
movsd %xmm1, 0x18(%rsp)
leal 0x2(%rbx), %eax
incl %ebx
cmpq %rax, %r14
ja 0x17cab3
jmp 0x17cc02
movsd 0xb4a4a(%rip), %xmm0    # 0x231638
movsd %xmm0, 0x18(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl (%r12), %r15d
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movl 0x14(%rsp), %esi
movl %r15d, %edx
callq 0x17ca18
testq %rax, %rax
je 0x17cc54
xorpd %xmm0, %xmm0
movsd 0x18(%rax), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x17ca18
xorpd %xmm3, %xmm3
movapd 0x40(%rsp), %xmm2
testq %rax, %rax
movl 0xc(%rsp), %ecx
jne 0x17cc6e
jmp 0x17cc77
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x17ca18
testq %rax, %rax
movl 0xc(%rsp), %ecx
je 0x17ccb0
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
addsd 0x18(%rax), %xmm3
movapd %xmm3, %xmm0
cmpnlesd %xmm2, %xmm0
movapd %xmm0, %xmm1
andpd %xmm3, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cmpl $-0x1, %r13d
je 0x17cc9e
ucomisd 0x18(%rsp), %xmm0
jbe 0x17ccb0
xorl %ecx, %ecx
ucomisd %xmm3, %xmm2
setb %cl
xorl %r13d, %r13d
movsd %xmm0, 0x18(%rsp)
movl %ecx, 0xc(%rsp)
movl -0x4(%r12,%r14,4), %r15d
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x17ca18
testq %rax, %rax
je 0x17cd0c
xorpd %xmm0, %xmm0
movsd 0x18(%rax), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
movq %rbx, %rdi
movl 0x14(%rsp), %esi
movl %r15d, %edx
callq 0x17ca18
xorpd %xmm1, %xmm1
movsd 0x40(%rsp), %xmm0
testq %rax, %rax
movq 0x28(%rsp), %r15
movl 0xc(%rsp), %ebx
jne 0x17cd31
jmp 0x17cd3a
movq %rbx, %rdi
movl 0x14(%rsp), %esi
movl %r15d, %edx
callq 0x17ca18
testq %rax, %rax
je 0x17ce1e
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %r15
movl 0xc(%rsp), %ebx
xorpd %xmm1, %xmm1
addsd 0x18(%rax), %xmm1
leaq -0x1(%r14), %rax
cmpl $-0x1, %r13d
sete %dl
xorl %ecx, %ecx
ucomisd %xmm1, %xmm0
setb %cl
jb 0x17cd54
movapd %xmm0, %xmm1
ucomisd 0x18(%rsp), %xmm1
seta %sil
orb %sil, %dl
cmovnel %eax, %r13d
cmovnel %ecx, %ebx
leal -0x1(%r13), %eax
cmpl $-0x2, %eax
adcl $0x0, %r13d
leaq 0x2(%r14), %rax
cmpq %rax, 0x10(%r15)
jae 0x17cdd8
leaq 0x1(%r14), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x10(%r15)
leaq 0x4(,%rcx,4), %rsi
movq %r12, %rdi
callq 0x2370
movq %rax, %r12
movq %rax, (%r15)
movq 0x8(%r15), %rax
addq $0x2, %rax
movq %rax, 0x8(%r15)
movl %r13d, %eax
cmpl %r14d, %r13d
jae 0x17cdfc
leaq -0x1(%r14), %rcx
movl -0x4(%r12,%r14,4), %edx
incl %r14d
movl %edx, (%r12,%r14,4)
movq %rcx, %r14
cmpq %rax, %rcx
ja 0x17cde4
addl %ebx, %ebp
movl %ebp, (%r12,%rax,4)
movl 0x14(%rsp), %eax
subl %ebx, %eax
incl %r13d
movl %eax, (%r12,%r13,4)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %r15
movl 0xc(%rsp), %ebx
jmp 0x17cd68