// File: /workspace/github_cpp_makefile_star2_O3/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)
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(%r15), %rsi
movq %rdx, -0x40(%rbp)
callq 0x62aa
movl (%r12), %r13d
movl 0x4(%r12), %eax
movl (%r15), %ebx
movl 0x4(%r15), %r14d
movl %eax, -0x2c(%rbp)
cmpl %r14d, %eax
setb %al
cmpl %ebx, %r13d
seta %cl
orb %al, %cl
je 0x5c76
subl %ebx, %r14d
incq %r14
shrq %r14
leal (%rbx,%r14), %eax
decl %eax
cmpl %eax, %r13d
jbe 0x5c95
movb $0x1, %r13b
cmpl %eax, -0x2c(%rbp)
ja 0x5cd6
jmp 0x5daa
movq 0x10(%r15), %rsi
cmpq 0x18(%r15), %rsi
je 0x5d51
movq -0x38(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r15)
jmp 0x5e7f
movl %eax, -0x30(%rbp)
movq 0x28(%r15), %rdi
testq %rdi, %rdi
je 0x5d69
leaq -0xa0(%rbp), %rsi
movl %r13d, (%rsi)
movl -0x2c(%rbp), %eax
movl %eax, 0x4(%rsi)
leaq -0x60(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x5be7
movl %eax, %r13d
movl 0x4(%r12), %eax
movl %eax, -0x2c(%rbp)
cmpl -0x30(%rbp), %eax
jbe 0x5da1
movq 0x20(%r15), %rdi
testq %rdi, %rdi
je 0x5e17
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 0x5be7
testb %al, %r13b
je 0x5e7f
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r15
cmpq %r15, %r14
je 0x5daa
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 0x5567
movl $0x1, %ebx
movq %r14, %rdi
movq %r15, %rsi
callq 0x564b
jmp 0x5dac
addq $0x8, %r15
leaq -0x88(%rbp), %rdx
movq %r15, %rdi
callq 0x5964
jmp 0x5e7f
movl $0x30, %edi
callq 0x2160
movl %ebx, (%rax)
movl -0x30(%rbp), %ecx
movl %ecx, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $0x0, 0x28(%rax)
movq %rax, 0x28(%r15)
leaq -0xa8(%rbp), %rsi
movl %r13d, (%rsi)
jmp 0x5e51
testb %r13b, %r13b
je 0x5e7f
xorl %ebx, %ebx
movq -0x60(%rbp), %r14
movq -0x58(%rbp), %r15
cmpq %r15, %r14
je 0x5de9
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 0x5567
movq %r14, %rdi
movq %r15, %rsi
callq 0x564b
orl $0x2, %ebx
cmpl $0x1, %ebx
je 0x5e6b
cmpl $0x2, %ebx
je 0x5e65
cmpl $0x3, %ebx
jne 0x5e7f
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x58dc
jmp 0x5e7f
movl $0x30, %edi
callq 0x2160
addl %r14d, %ebx
movl 0x4(%r15), %ecx
movl %ebx, (%rax)
movl %ecx, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movups %xmm0, 0x18(%rax)
movq $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 0x5a3c
jmp 0x5e7f
leaq -0x60(%rbp), %rax
jmp 0x5e6f
leaq -0x80(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movq -0x40(%rbp), %rdx
callq 0x62aa
movq -0x40(%rbp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r14
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x5e98
callq 0x2130
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x5ea6
callq 0x2130
cmpq %r14, %rbx
setne %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5ec2
jmp 0x5ec2
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x5ed3
callq 0x2130
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x5ee1
callq 0x2130
movq %rbx, %rdi
callq 0x2240