// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movsd 0x13e4cc(%rip), %xmm0   # 0x382278
movsd %xmm0, -0x70(%rbp)
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x243fac
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x18(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x14(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
movl -0x14(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x243cb0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x243cb0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x243e60
cmpq $0x0, -0x48(%rbp)
je 0x243e9d
cmpq $0x0, -0x40(%rbp)
je 0x243e7a
movq -0x40(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x243e94
movq -0x48(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x14(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x14(%rbp), %eax
shll %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x243cb0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x243cb0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x243ee4
cmpq $0x0, -0x48(%rbp)
je 0x243f21
cmpq $0x0, -0x40(%rbp)
je 0x243efe
movq -0x40(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x243f18
movq -0x48(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jbe 0x243f9c
movsd -0x60(%rbp), %xmm0
ucomisd -0x58(%rbp), %xmm0
jb 0x243f3f
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
jmp 0x243f49
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
movl $0x1, %eax
xorl %ecx, %ecx
ucomisd %xmm1, %xmm0
cmovael %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x2c(%rbp)
je 0x243f84
movsd -0x70(%rbp), %xmm1
movsd -0x68(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x243f9a
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x243f9c
jmp 0x243f9e
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x243db8
movl $0x0, -0x18(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x243cb0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x244027
cmpq $0x0, -0x48(%rbp)
je 0x244064
cmpq $0x0, -0x40(%rbp)
je 0x244041
movq -0x40(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x24405b
movq -0x48(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
shll %eax
movl %eax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x243cb0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x24409c
cmpq $0x0, -0x48(%rbp)
je 0x2440d9
cmpq $0x0, -0x40(%rbp)
je 0x2440b6
movq -0x40(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x2440d0
movq -0x48(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jbe 0x244154
movsd -0x60(%rbp), %xmm0
ucomisd -0x58(%rbp), %xmm0
jb 0x2440f7
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0x244101
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
movl $0x1, %eax
xorl %ecx, %ecx
ucomisd %xmm1, %xmm0
cmovael %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x2c(%rbp)
je 0x24413c
movsd -0x70(%rbp), %xmm1
movsd -0x68(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x244152
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x244154
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
subq $0x1, %rax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x14(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x243cb0
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2441e1
cmpq $0x0, -0x48(%rbp)
je 0x24421e
cmpq $0x0, -0x40(%rbp)
je 0x2441fb
movq -0x40(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x244215
movq -0x48(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x14(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x243cb0
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x244259
cmpq $0x0, -0x48(%rbp)
je 0x244296
cmpq $0x0, -0x40(%rbp)
je 0x244273
movq -0x40(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x24428d
movq -0x48(%rbp), %rax
movsd 0x18(%rax), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jbe 0x24431a
movsd -0x60(%rbp), %xmm0
ucomisd -0x58(%rbp), %xmm0
jb 0x2442b7
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
jmp 0x2442c4
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
movl $0x1, %eax
xorl %ecx, %ecx
ucomisd %xmm1, %xmm0
cmovael %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x2c(%rbp)
je 0x244302
movsd -0x70(%rbp), %xmm1
movsd -0x68(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x244318
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x24431a
cmpl $0x0, -0x2c(%rbp)
je 0x24432f
cmpl $-0x1, -0x2c(%rbp)
je 0x24432f
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
addq $0x2, %rcx
cmpq %rcx, %rax
jae 0x244424
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x2, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x244426
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x2, %rcx
movq %rcx, 0x8(%rax)
movl -0x18(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0x244468
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x18(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x18(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x244436
movl -0x14(%rbp), %edx
shll %edx
addl -0x38(%rbp), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x14(%rbp), %edx
shll %edx
addl $0x1, %edx
subl -0x38(%rbp), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)