// File: /workspace/github_cpp_makefile_star2_O1/davmac314[P]dinit/src/../dasynq/include/dasynq/btree_set.h
// Function: dasynq::btree_set<void*, int, std::less<int>, 8>::insert(dasynq::btree_set<void*, int, std::less<int>, 8>::heapnode&, int)
bool insert(handle_t & hndl, P pval = P()) noexcept
    {
        if (root_sept == nullptr) {
            root_sept = alloc_sept();
            left_sept = root_sept;
        }

        septnode * srch_sept = root_sept;

        while (! srch_sept->is_leaf()) {
            int min = 0;
            int max = N - 1;
            while (min <= max) {
                int i = (min + max) / 2;

                if (srch_sept->hn_p[i] == nullptr || pval < srch_sept->prio[i]) {
                    max = i - 1;
                }
                else if (srch_sept->prio[i] == pval) {
                    // Already present
                    return false;
                }
                else {
                    min = i + 1;
                }
            }

            // go up to the right:
            srch_sept = srch_sept->children[max + 1];
        }

        // We got to a leaf: does it have space?
        // First check if already present
        int children = srch_sept->num_vals();

        {
            int min = 0;
            int max = children - 1;
            while (min <= max) {
                int i = (min + max) / 2;

                if (pval < srch_sept->prio[i]) {
                    max = i - 1;
                }
                else if (srch_sept->prio[i] == pval) {
                    // Already present
                    return false;
                }
                else {
                    min = i + 1;
                }
            }
        }

        septnode * left_down = nullptr; // left node going down
        septnode * right_down = nullptr; // right node going down

        handle_t * hndl_p = &hndl;

        while (children == N) {
            // split and push value towards root
            septnode * new_sibling = alloc_sept();
            new_sibling->parent = srch_sept->parent;

            // create new sibling to the right:
            for (int i = N/2; i < N; i++) {
                new_sibling->prio[i - N/2] = srch_sept->prio[i];  // new[0] = old[4]
                new_sibling->hn_p[i - N/2] = srch_sept->hn_p[i];
                new_sibling->children[i - N/2 + 1] = srch_sept->children[i + 1];
                if (new_sibling->children[i - N/2 + 1]) new_sibling->children[i - N/2 + 1]->parent = new_sibling;
                new_sibling->hn_p[i - N/2]->parent = new_sibling;
                srch_sept->hn_p[i] = nullptr;
            }
            // Note that new_sibling->children[0] has not yet been set.

            if (pval < srch_sept->prio[N/2 - 1])  {
                auto o_prio = srch_sept->prio[N/2 - 1];
                auto o_hidx = srch_sept->hn_p[N/2 - 1];

                new_sibling->children[0] = srch_sept->children[N/2];
                if (new_sibling->children[0]) new_sibling->children[0]->parent = new_sibling;

                int i = N/2 - 1;
                for ( ; i > 0 && pval < srch_sept->prio[i - 1]; i--) {
                    srch_sept->prio[i] = srch_sept->prio[i - 1];
                    srch_sept->children[i+1] = srch_sept->children[i];
                    srch_sept->hn_p[i] = srch_sept->hn_p[i - 1];
                }
                srch_sept->prio[i] = pval;
                srch_sept->hn_p[i] = hndl_p;
                hndl_p->parent = srch_sept;
                srch_sept->children[i] = left_down;
                srch_sept->children[i+1] = right_down;
                hndl_p = o_hidx;
                pval = o_prio;
            }
            else if (pval < new_sibling->prio[0]) {
                // new value is right in the middle
                srch_sept->children[N/2] = left_down;
                new_sibling->children[0] = right_down;
                if (left_down) left_down->parent = srch_sept;
                if (right_down) right_down->parent = new_sibling;
            }
            else {
                auto o_prio = new_sibling->prio[0];
                auto o_hidx = new_sibling->hn_p[0];
                int i = 0;
                for ( ; i < (N/2 - 1) && new_sibling->prio[i + 1] < pval; i++) {
                    new_sibling->prio[i] = new_sibling->prio[i + 1];
                    new_sibling->children[i] = new_sibling->children[i + 1];
                    new_sibling->hn_p[i] = new_sibling->hn_p[i + 1];
                }
                new_sibling->prio[i] = pval;
                new_sibling->hn_p[i] = hndl_p;
                hndl_p->parent = new_sibling;
                new_sibling->children[i] = left_down;
                new_sibling->children[i+1] = right_down;
                if (left_down) left_down->parent = new_sibling;
                if (right_down) right_down->parent = new_sibling;
                hndl_p = o_hidx;
                pval = o_prio;
            }

            left_down = srch_sept;
            right_down = new_sibling;

            srch_sept = srch_sept->parent;
            if (srch_sept == nullptr) {
                // Need new root node:
                srch_sept = alloc_sept();
                root_sept = srch_sept;
                left_down->parent = root_sept;
                right_down->parent = root_sept;
                children = 0;
            }
            else {
                children = srch_sept->num_vals();
            }
        }

        // Insert into non-full node:
        int inspos;
        for (inspos = children; inspos > 0; inspos--) {
            if (srch_sept->prio[inspos - 1] < pval) {
                break;
            }

            srch_sept->prio[inspos] = srch_sept->prio[inspos-1];
            srch_sept->hn_p[inspos] = srch_sept->hn_p[inspos-1];
            srch_sept->children[inspos+1] = srch_sept->children[inspos];
        }

        srch_sept->prio[inspos] = pval;
        srch_sept->hn_p[inspos] = hndl_p;
        srch_sept->children[inspos] = left_down;
        srch_sept->children[inspos+1] = right_down;
        hndl_p->parent = srch_sept;
        return true;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r13
cmpq $0x0, (%rdi)
jne 0x64ed
movq 0x10(%r13), %rbx
movq 0xa8(%rbx), %rax
movq %rax, 0x10(%r13)
leaq 0x20(%rbx), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x3140
movq %rbx, (%r13)
movq %rbx, 0x8(%r13)
movq (%r13), %rbx
cmpq $0x0, 0x60(%rbx)
je 0x6552
xorl %esi, %esi
movl $0x7, %edx
movl %esi, %eax
movl %edx, %ecx
cmpl %edx, %esi
jg 0x6544
leal (%rax,%rcx), %esi
movl %esi, %edx
shrl $0x1f, %edx
addl %esi, %edx
sarl %edx
movslq %edx, %rsi
cmpq $0x0, 0x20(%rbx,%rsi,8)
je 0x6525
movl (%rbx,%rsi,4), %esi
cmpl %ebp, %esi
jle 0x652e
decl %edx
movb $0x1, %dil
movl %eax, %esi
jmp 0x653d
incl %edx
cmpl %ebp, %esi
setne %dil
movl %edx, %esi
cmovel %eax, %esi
movl %ecx, %edx
testb %dil, %dil
jne 0x64ff
jmp 0x654c
movslq %ecx, %rdx
movq 0x68(%rbx,%rdx,8), %rbx
cmpl %ecx, %eax
jg 0x64f1
jmp 0x6595
movl $0x8, %r14d
leal -0x1(%r14), %eax
cmpq $0x0, 0x20(%rbx,%rax,8)
jne 0x656c
decl %r14d
jne 0x6558
xorl %r14d, %r14d
leal -0x1(%r14), %eax
xorl %ecx, %ecx
cmpl %eax, %ecx
setg %r15b
jg 0x659d
leal (%rax,%rcx), %edx
shrl %edx
leal -0x1(%rdx), %esi
movl %edx, %edi
incl %edi
cmpl %ebp, (%rbx,%rdx,4)
cmovgel %ecx, %edi
cmovlel %eax, %esi
movl %edi, %ecx
movl %esi, %eax
jne 0x6572
xorl %r15d, %r15d
jmp 0x6858
movq %r12, 0x8(%rsp)
cmpl $0x8, %r14d
jne 0x67fb
movq %r13, 0x20(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movl %r15d, 0x1c(%rsp)
movq %r13, %r14
movq %r12, 0x10(%rsp)
movq %rbx, %r13
movq 0x20(%rsp), %rcx
movq 0x10(%rcx), %r12
movq 0xa8(%r12), %rax
movq %rax, 0x10(%rcx)
leaq 0x20(%r12), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x3140
movq 0xa8(%rbx), %rax
movq %rax, 0xa8(%r12)
xorl %eax, %eax
movl 0x10(%r13,%rax), %ecx
movl %ecx, (%r12,%rax)
movq 0x40(%r13,%rax,2), %rcx
movq %rcx, 0x20(%r12,%rax,2)
movq 0x88(%r13,%rax,2), %rcx
movq %rcx, 0x68(%r12,%rax,2)
testq %rcx, %rcx
je 0x662a
movq %r12, 0xa8(%rcx)
movq 0x20(%r12,%rax,2), %rcx
movq %r12, 0x8(%rcx)
movq $0x0, 0x40(%r13,%rax,2)
addq $0x4, %rax
cmpq $0x10, %rax
jne 0x65fe
movl 0xc(%r13), %eax
cmpl %eax, %ebp
jge 0x66a6
movq 0x38(%r13), %rcx
movq 0x80(%r13), %rdx
movq %rdx, 0x60(%r12)
testq %rdx, %rdx
je 0x666a
movq %r12, 0xa8(%rdx)
movl $0x10, %edx
movl -0x38(%r13,%rdx,4), %esi
cmpl %esi, %ebp
jge 0x66e0
movl %esi, -0x34(%r13,%rdx,4)
movq -0x50(%r13,%rdx,8), %rsi
movq -0x8(%r13,%rdx,8), %rdi
movq %rdi, (%r13,%rdx,8)
movq %rsi, -0x48(%r13,%rdx,8)
leaq -0x1(%rdx), %rsi
addq $-0xd, %rdx
cmpq $0x1, %rdx
movq %rsi, %rdx
ja 0x666f
xorl %edx, %edx
jmp 0x66e3
movl (%r12), %eax
cmpl %eax, %ebp
jge 0x670a
movq %r14, 0x80(%r13)
movq 0x10(%rsp), %rax
movq %rax, 0x60(%r12)
testq %r14, %r14
je 0x66cb
movq %r13, 0xa8(%r14)
testq %rax, %rax
je 0x6789
movq %r12, 0xa8(%rax)
jmp 0x6789
addl $-0xd, %edx
movslq %edx, %rdx
movl %ebp, (%r13,%rdx,4)
movq 0x8(%rsp), %rsi
movq %rsi, 0x20(%r13,%rdx,8)
movq %r13, 0x8(%rsi)
movq %r14, 0x60(%r13,%rdx,8)
movq 0x10(%rsp), %rsi
movq %rsi, 0x68(%r13,%rdx,8)
jmp 0x6782
movq 0x20(%r12), %rcx
xorl %edx, %edx
movl 0x4(%r12,%rdx,4), %esi
cmpl %ebp, %esi
jge 0x6744
movl %esi, (%r12,%rdx,4)
movq 0x28(%r12,%rdx,8), %rsi
movq 0x68(%r12,%rdx,8), %rdi
movq %rdi, 0x60(%r12,%rdx,8)
movq %rsi, 0x20(%r12,%rdx,8)
leaq 0x1(%rdx), %rsi
movq %rsi, %rdx
cmpq $0x3, %rsi
jne 0x6711
movl $0x3, %edx
movl %edx, %edx
movl %ebp, (%r12,%rdx,4)
movq 0x8(%rsp), %rsi
movq %rsi, 0x20(%r12,%rdx,8)
movq %r12, 0x8(%rsi)
movq %r14, 0x60(%r12,%rdx,8)
movq 0x10(%rsp), %rsi
movq %rsi, 0x68(%r12,%rdx,8)
movq %rsi, %rdx
testq %r14, %r14
je 0x6776
movq %r12, 0xa8(%r14)
testq %rdx, %rdx
je 0x6782
movq %r12, 0xa8(%rdx)
movq %rcx, 0x8(%rsp)
movl %eax, %ebp
movq 0xa8(%r13), %rbx
testq %rbx, %rbx
je 0x67b1
movl $0x8, %r14d
leal -0x1(%r14), %eax
cmpq $0x0, 0x20(%rbx,%rax,8)
jne 0x67ef
decl %r14d
jne 0x679b
xorl %r14d, %r14d
jmp 0x67ef
movq 0x20(%rsp), %r15
movq 0x10(%r15), %rbx
movq 0xa8(%rbx), %rax
movq %rax, 0x10(%r15)
leaq 0x20(%rbx), %rdi
xorl %r14d, %r14d
movl $0x90, %edx
xorl %esi, %esi
callq 0x3140
movq %rbx, (%r15)
movl 0x1c(%rsp), %r15d
movq %rbx, 0xa8(%r13)
movq %rbx, 0xa8(%r12)
cmpl $0x8, %r14d
je 0x65bc
jmp 0x6801
xorl %r13d, %r13d
xorl %r12d, %r12d
testl %r14d, %r14d
jle 0x683a
movl %r14d, %r14d
movl -0x4(%rbx,%r14,4), %eax
cmpl %ebp, %eax
jl 0x683a
movl %eax, (%rbx,%r14,4)
movq 0x18(%rbx,%r14,8), %rax
movq 0x60(%rbx,%r14,8), %rcx
movq %rax, 0x20(%rbx,%r14,8)
movq %rcx, 0x68(%rbx,%r14,8)
decq %r14
leaq 0x1(%r14), %rax
cmpq $0x1, %rax
jg 0x6809
xorl %r14d, %r14d
movslq %r14d, %rax
movl %ebp, (%rbx,%rax,4)
movq 0x8(%rsp), %rcx
movq %rcx, 0x20(%rbx,%rax,8)
movq %r13, 0x60(%rbx,%rax,8)
movq %r12, 0x68(%rbx,%rax,8)
movq %rbx, 0x8(%rcx)
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq