// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rdi, %r15
movq %rcx, -0x38(%rbp)
movq %rcx, -0x88(%rbp)
andq $0x0, -0x70(%rbp)
xorps %xmm0, %xmm0
andq $0x0, -0x50(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x60(%rbp)
movq 0x8(%rdi), %rdi
movq 0x10(%r15), %rsi
movq %rdx, -0x40(%rbp)
callq 0x5afa
movl (%r12), %r13d
movl 0x4(%r12), %eax
movl (%r15), %r14d
movl 0x4(%r15), %ebx
movl %eax, -0x2c(%rbp)
cmpl %ebx, %eax
setb %al
cmpl %r14d, %r13d
seta %cl
orb %al, %cl
je 0x5658
subl %r14d, %ebx
incq %rbx
shrq %rbx
leal (%r14,%rbx), %eax
decl %eax
cmpl %eax, %r13d
jbe 0x5670
movb $0x1, %r13b
cmpl %eax, -0x2c(%rbp)
ja 0x56a9
jmp 0x571d
addq $0x8, %r15
leaq -0x88(%rbp), %rsi
movq %r15, %rdi
callq 0x4c7a
jmp 0x57d9
movl %eax, -0x30(%rbp)
movq 0x28(%r15), %rdi
testq %rdi, %rdi
je 0x56e0
leaq -0xa0(%rbp), %rsi
movl %r13d, (%rsi)
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%rsi)
leaq -0x60(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x55ca
movl %eax, %r13d
movl 0x4(%r12), %eax
movl %eax, -0x2c(%rbp)
cmpl -0x30(%rbp), %eax
jbe 0x5714
movq 0x20(%r15), %rdi
testq %rdi, %rdi
je 0x5775
movl (%r12), %eax
leaq -0x90(%rbp), %rsi
movl %eax, (%rsi)
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%rsi)
leaq -0x80(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x55ca
testb %al, %r13b
jne 0x571d
jmp 0x57d9
pushq $0x30
popq %rdi
callq 0x2150
movl %r14d, (%rax)
movl -0x30(%rbp), %ecx
movl %ecx, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
andq $0x0, 0x28(%rax)
movups %xmm0, 0x18(%rax)
movq %rax, 0x28(%r15)
leaq -0xa8(%rbp), %rsi
movl %r13d, (%rsi)
jmp 0x57ab
testb %r13b, %r13b
je 0x57d9
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
cmpq %rsi, %rdi
je 0x5734
callq 0x4e43
pushq $0x1
popq %rbx
jmp 0x5736
xorl %ebx, %ebx
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
cmpq %rsi, %rdi
je 0x574b
callq 0x4e43
orl $0x2, %ebx
cmpl $0x1, %ebx
je 0x57c5
cmpl $0x2, %ebx
je 0x57bf
cmpl $0x3, %ebx
jne 0x57d9
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x52b7
jmp 0x57d9
pushq $0x30
popq %rdi
callq 0x2150
addl %ebx, %r14d
movl 0x4(%r15), %ecx
movl %r14d, (%rax)
movl %ecx, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x20(%r15)
movl (%r12), %ecx
leaq -0x98(%rbp), %rsi
movl %ecx, (%rsi)
movl -0x2c(%rbp), %ecx
movl %ecx, 0x4(%rsi)
movq %rax, %rdi
movq -0x38(%rbp), %rdx
callq 0x5448
jmp 0x57d9
leaq -0x60(%rbp), %rax
jmp 0x57c9
leaq -0x80(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movq -0x40(%rbp), %rdx
callq 0x5afa
movq -0x40(%rbp), %rcx
movq (%rcx), %rax
cmpq 0x8(%rcx), %rax
setne %bl
leaq -0x60(%rbp), %rdi
callq 0x2c06
leaq -0x80(%rbp), %rdi
callq 0x2c06
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5811
jmp 0x5811
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2c06
leaq -0x80(%rbp), %rdi
callq 0x2c06
movq %rbx, %rdi
callq 0x2230