// File: /workspace/github_cpp_makefile_star2_O1/derekzwy[P]an-optimal-tcam-encoding-algorithm-for-packet-classification/rtrie.cc
// Function: rt_qry_insert(rnode*, rg, std::vector<pc_rule*, std::allocator<pc_rule*>>&, pc_rule*)
bool rt_qry_insert(rnode *n, rg r, vector<pc_rule*> &set, pc_rule* index)
{
    vector<pc_rule*> set_right;
    vector<pc_rule*> set_left;
    bool br= true,bl = true;

    //for_each(n->rlist.begin(), n->rlist.end(), 
    //        [&set](int index) {set.push_back(index);} );
    copy(n->rlist.begin(), n->rlist.end(), back_inserter(set));

    if(r.high >= n->b.high && r.low <= n->b.low) {
        //stop
        n->rlist.push_back(index);
        return !set.empty();
    }

    //uint32_t m = (n->b.low + n->b.high)>>1;
    uint32_t m = (n->b.high -  n->b.low + 1ULL)/2 + n->b.low - 1;

    if(r.low <= m) {
        if(!n->left) {
            n->left = new rnode(rg(n->b.low, m));
            rt_insert(n->left, r, index);
            return !set.empty();
        }
        br =  rt_qry_insert(n->left, r, set_left, index);
    }
    if(r.high > m) {
        if(!n->right) {
            n->right = new rnode(rg(m+1, n->b.high));
            rt_insert(n->right, r, index);
            return !set.empty();
        }
        bl = rt_qry_insert(n->right, r, set_right, index);
    }
    //intersection
    int rl = 0;
    if(bl && br) {
        if(!set_right.empty()) {
            sort(set_right.begin(),set_right.end()); 
            rl |= 1;
        }
        if(!set_left.empty()) {
            sort(set_left.begin(),set_left.end()); 
            rl |= 2;
        }

        if(rl == 3) {
            set_intersection(set_right.begin(), set_right.end(),
                    set_left.begin(), set_left.end(),
                    back_inserter(set));
        }
        else if( rl == 1) {
            copy(set_right.begin(), set_right.end(), back_inserter(set));
        }
        else if( rl == 2)  
            copy(set_left.begin(), set_left.end(), back_inserter(set));
    }
    return !set.empty();

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r15
movq %rdi, %r14
movq %rcx, -0x30(%rbp)
xorl %eax, %eax
movq %rax, -0x70(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq %rax, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movq 0x8(%rdi), %rdi
movq 0x10(%r14), %rsi
movq %rdx, -0x40(%rbp)
callq 0x62d6
movl 0x4(%r15), %ecx
movl 0x4(%r14), %ebx
cmpl %ebx, %ecx
jb 0x5c84
movl (%r15), %eax
cmpl (%r14), %eax
jbe 0x5d62
movl %ecx, -0x34(%rbp)
movl (%r14), %eax
subl %eax, %ebx
incq %rbx
shrq %rbx
movq %rax, -0x88(%rbp)
leal (%rax,%rbx), %r13d
decl %r13d
movl (%r15), %r12d
movb $0x1, %al
cmpl %r13d, %r12d
ja 0x5cd4
movq 0x28(%r14), %rdi
testq %rdi, %rdi
je 0x5d81
leaq -0xa0(%rbp), %rsi
movl %r12d, (%rsi)
movl -0x34(%rbp), %eax
movl %eax, 0x4(%rsi)
movq -0x30(%rbp), %rcx
leaq -0x60(%rbp), %rdx
callq 0x5c28
movl 0x4(%r15), %r12d
cmpl %r13d, %r12d
jbe 0x5d12
movq 0x20(%r14), %rdi
testq %rdi, %rdi
je 0x5dc1
movl (%r15), %ecx
leaq -0x90(%rbp), %rsi
movl %ecx, (%rsi)
movl %r12d, 0x4(%rsi)
movl %eax, %ebx
movq -0x30(%rbp), %rcx
leaq -0x80(%rbp), %rdx
callq 0x5c28
testb %al, %bl
jne 0x5d1a
jmp 0x5ead
testb %al, %al
je 0x5ead
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r15
cmpq %r15, %r14
je 0x5e2a
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x559d
movl $0x1, %ebx
movq %r14, %rdi
movq %r15, %rsi
callq 0x5681
jmp 0x5e2c
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0x5e15
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0x5ead
movl $0x30, %edi
callq 0x2160
movq -0x88(%rbp), %rcx
movl %ecx, (%rax)
movl %r13d, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
movq %rax, 0x28(%r14)
leaq -0xa8(%rbp), %rsi
movl %r12d, (%rsi)
movl -0x34(%rbp), %ecx
movl %ecx, 0x4(%rsi)
jmp 0x5e04
movl $0x30, %edi
callq 0x2160
movq -0x88(%rbp), %rdx
addl %ebx, %edx
movl 0x4(%r14), %ecx
movl %edx, (%rax)
movl %ecx, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
movq %rax, 0x20(%r14)
movl (%r15), %ecx
leaq -0x98(%rbp), %rsi
movl %ecx, (%rsi)
movl %r12d, 0x4(%rsi)
movq -0x30(%rbp), %rdx
movq %rax, %rdi
callq 0x5a94
jmp 0x5ead
addq $0x8, %r14
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
callq 0x59be
jmp 0x5ead
xorl %ebx, %ebx
movq -0x60(%rbp), %r14
movq -0x58(%rbp), %r15
cmpq %r15, %r14
je 0x5e69
movq %r15, %rax
subq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x559d
movq %r14, %rdi
movq %r15, %rsi
callq 0x5681
orl $0x2, %ebx
cmpl $0x1, %ebx
je 0x5e99
cmpl $0x2, %ebx
je 0x5e93
cmpl $0x3, %ebx
jne 0x5ead
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x5936
jmp 0x5ead
leaq -0x60(%rbp), %rax
jmp 0x5e9d
leaq -0x80(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movq -0x40(%rbp), %rdx
callq 0x62d6
movq -0x40(%rbp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r14
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x5ec6
callq 0x2130
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x5ed4
callq 0x2130
cmpq %r14, %rbx
setne %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5ef0
jmp 0x5ef0
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x5f01
callq 0x2130
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x5f0f
callq 0x2130
movq %rbx, %rdi
callq 0x2240