// File: 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
subq $0x220, %rsp            # imm = 0x220
movq %rsi, -0x170(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3300
leaq -0x58(%rbp), %rdi
callq 0x3300
movb $0x1, -0x59(%rbp)
movb $0x1, -0x5a(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
callq 0x3460
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
callq 0x34d0
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rdi
callq 0x9350
movq %rax, -0x168(%rbp)
jmp 0xb850
movq -0x168(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0xc8a0
movq %rax, -0x178(%rbp)
jmp 0xb875
movq -0x170(%rbp), %rax
movq -0x178(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jb 0xb8ed
movq -0x170(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
ja 0xb8ed
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
leaq -0x28(%rbp), %rsi
callq 0x93c0
jmp 0xb8ba
movq -0x20(%rbp), %rdi
callq 0x9380
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xbe6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xbe8a
movq -0x170(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
movq -0x10(%rbp), %rdx
subl (%rdx), %ecx
movl %ecx, %ecx
addq $0x1, %rcx
shrq %rcx
movq -0x10(%rbp), %rdx
movl (%rdx), %edx
addq %rdx, %rcx
subq $0x1, %rcx
movl %ecx, -0x98(%rbp)
movl (%rax), %eax
cmpl -0x98(%rbp), %eax
ja 0xba59
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0xba07
movl $0x30, %edi
callq 0x2130
movq %rax, -0x180(%rbp)
jmp 0xb94d
movq -0x10(%rbp), %rax
movl (%rax), %esi
movl -0x98(%rbp), %edx
leaq -0xa0(%rbp), %rdi
callq 0x91d0
jmp 0xb967
movq -0x180(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xc820
jmp 0xb97c
movq -0x170(%rbp), %rsi
movq -0x180(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x188(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xc870
jmp 0xb9af
movq -0x188(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq -0xa8(%rbp), %rsi
callq 0xb590
jmp 0xb9c8
movq -0x20(%rbp), %rdi
callq 0x9380
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xbe6a
movq -0x180(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
callq 0x2100
jmp 0xbe8a
movq -0x170(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x190(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xc870
jmp 0xba2b
movq -0x190(%rbp), %rdi
movq -0x28(%rbp), %rcx
leaq -0xb0(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0xb7e0
movb %al, -0x191(%rbp)
jmp 0xba4e
movb -0x191(%rbp), %al
andb $0x1, %al
movb %al, -0x59(%rbp)
movq -0x170(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x98(%rbp), %eax
jbe 0xbba0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xbb4e
movl $0x30, %edi
callq 0x2130
movq %rax, -0x1a0(%rbp)
jmp 0xba91
movl -0x98(%rbp), %esi
incl %esi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
leaq -0xb8(%rbp), %rdi
callq 0x91d0
jmp 0xbaae
movq -0x1a0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0xc820
jmp 0xbac3
movq -0x170(%rbp), %rsi
movq -0x1a0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x1a8(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0xc870
jmp 0xbaf6
movq -0x1a8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq -0xc0(%rbp), %rsi
callq 0xb590
jmp 0xbb0f
movq -0x20(%rbp), %rdi
callq 0x9380
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xbe6a
movq -0x1a0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
callq 0x2100
jmp 0xbe8a
movq -0x170(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x1b0(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0xc870
jmp 0xbb72
movq -0x1b0(%rbp), %rdi
movq -0x28(%rbp), %rcx
leaq -0xc8(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0xb7e0
movb %al, -0x1b1(%rbp)
jmp 0xbb95
movb -0x1b1(%rbp), %al
andb $0x1, %al
movb %al, -0x5a(%rbp)
movl $0x0, -0xcc(%rbp)
testb $0x1, -0x5a(%rbp)
je 0xbe50
testb $0x1, -0x59(%rbp)
je 0xbe50
leaq -0x40(%rbp), %rdi
callq 0x9380
testb $0x1, %al
jne 0xbc19
leaq -0x40(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x3460
movq -0x1c0(%rbp), %rdi
movq %rax, -0xd8(%rbp)
callq 0x34d0
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x92a0
jmp 0xbc0a
movl -0xcc(%rbp), %eax
orl $0x1, %eax
movl %eax, -0xcc(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x9380
testb $0x1, %al
jne 0xbc74
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x3460
movq -0x1c8(%rbp), %rdi
movq %rax, -0xe8(%rbp)
callq 0x34d0
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x92a0
jmp 0xbc65
movl -0xcc(%rbp), %eax
orl $0x2, %eax
movl %eax, -0xcc(%rbp)
cmpl $0x3, -0xcc(%rbp)
jne 0xbd39
leaq -0x40(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x3460
movq -0x1e0(%rbp), %rdi
movq %rax, -0xf8(%rbp)
callq 0x34d0
movq %rax, -0x100(%rbp)
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x3460
movq -0x1d8(%rbp), %rdi
movq %rax, -0x108(%rbp)
callq 0x34d0
movq %rax, -0x110(%rbp)
movq -0x20(%rbp), %rdi
callq 0x9350
movq %rax, -0x1d0(%rbp)
jmp 0xbce7
movq -0x1d0(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0xf8(%rbp), %rdi
movq -0x100(%rbp), %rsi
movq -0x108(%rbp), %rdx
movq -0x110(%rbp), %rcx
movq -0x118(%rbp), %r8
callq 0x92e0
movq %rax, -0x1e8(%rbp)
jmp 0xbd26
movq -0x1e8(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0xbe4e
cmpl $0x1, -0xcc(%rbp)
jne 0xbdc6
leaq -0x40(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x3460
movq -0x1f8(%rbp), %rdi
movq %rax, -0x128(%rbp)
callq 0x34d0
movq %rax, -0x130(%rbp)
movq -0x20(%rbp), %rdi
callq 0x9350
movq %rax, -0x1f0(%rbp)
jmp 0xbd82
movq -0x1f0(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x128(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq -0x138(%rbp), %rdx
callq 0xc8a0
movq %rax, -0x200(%rbp)
jmp 0xbdb3
movq -0x200(%rbp), %rax
movq %rax, -0x140(%rbp)
jmp 0xbe4c
cmpl $0x2, -0xcc(%rbp)
jne 0xbe4a
leaq -0x58(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x3460
movq -0x210(%rbp), %rdi
movq %rax, -0x148(%rbp)
callq 0x34d0
movq %rax, -0x150(%rbp)
movq -0x20(%rbp), %rdi
callq 0x9350
movq %rax, -0x208(%rbp)
jmp 0xbe0b
movq -0x208(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x148(%rbp), %rdi
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdx
callq 0xc8a0
movq %rax, -0x218(%rbp)
jmp 0xbe3c
movq -0x218(%rbp), %rax
movq %rax, -0x160(%rbp)
jmp 0xbe4c
jmp 0xbe4e
jmp 0xbe50
movq -0x20(%rbp), %rdi
callq 0x9380
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0x94(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x33e0
leaq -0x40(%rbp), %rdi
callq 0x33e0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x33e0
leaq -0x40(%rbp), %rdi
callq 0x33e0
movq -0x80(%rbp), %rdi
callq 0x2220
nopw %cs:(%rax,%rax)