// File: derekzwy[P]an-optimal-tcam-encoding-algorithm-for-packet-classification/rtrie.cc
// Function: rt_query_or(rnode*, rg, std::vector<pc_rule*, std::allocator<pc_rule*>>&, int, int)
void rt_query_or(rnode *n, rg r, vector<pc_rule*> &set, int l, int h) 
{
    if(!n){ 
        return;
    }
    vector<pc_rule*> set_left;
    vector<pc_rule*> set_right;
    

    copy_if(n->rlist.begin(), n->rlist.end(), back_inserter(set), [&l, &h](pc_rule *r){return r->priority >=l && r->priority < h;});
    uint32_t m = ((uint64_t)n->b.high -  (uint64_t)n->b.low + 1ULL)/2 + n->b.low - 1;

    if(r.low <= m) {
        rt_query_or(n->left, r, set_left, l,h);
    }
    if(r.high > m) {
        rt_query_or(n->right, r, set_right,l,h);
    }

    if(!set_left.empty() && !set_right.empty()) {
        sort(set_left.begin(), set_left.end());
        sort(set_right.begin(), set_right.end());

        set_union(set_left.begin(), set_left.end(),
                set_right.begin(), set_right.end(),
                back_inserter(set));
    }
    else if(!set_left.empty()) {
        copy(set_left.begin(), set_left.end(), back_inserter(set));
    }
    else if(!set_right.empty()) {
        copy(set_right.begin(), set_right.end(), back_inserter(set));
    }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rsi, -0x140(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
cmpq $0x0, -0x8(%rbp)
jne 0xc301
jmp 0xc71b
leaq -0x38(%rbp), %rdi
callq 0x3300
leaq -0x50(%rbp), %rdi
callq 0x3300
movq -0x8(%rbp), %rdi
addq $0x8, %rdi
callq 0x3460
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
addq $0x8, %rdi
callq 0x34d0
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9350
movq %rax, -0x148(%rbp)
jmp 0xc347
movq -0x148(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x1c(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq -0x80(%rbp), %r8
callq 0xc730
movq %rax, -0x150(%rbp)
jmp 0xc38a
movq -0x140(%rbp), %rax
movq -0x150(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movq -0x8(%rbp), %rcx
movl 0x4(%rcx), %ecx
movq -0x8(%rbp), %rdx
movl (%rdx), %edx
subq %rdx, %rcx
addq $0x1, %rcx
shrq %rcx
movq -0x8(%rbp), %rdx
movl (%rdx), %edx
addq %rdx, %rcx
subq $0x1, %rcx
movl %ecx, -0x94(%rbp)
movl (%rax), %eax
cmpl -0x94(%rbp), %eax
ja 0xc43c
movq -0x140(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x158(%rbp)
leaq -0x9c(%rbp), %rdi
callq 0xc870
jmp 0xc3f7
movq -0x158(%rbp), %rdi
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
leaq -0x9c(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0xc2d0
jmp 0xc417
jmp 0xc43c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x33e0
leaq -0x38(%rbp), %rdi
callq 0x33e0
jmp 0xc724
movq -0x140(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x94(%rbp), %eax
jbe 0xc494
movq -0x140(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x160(%rbp)
leaq -0xa4(%rbp), %rdi
callq 0xc870
jmp 0xc472
movq -0x160(%rbp), %rdi
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
leaq -0xa4(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0xc2d0
jmp 0xc492
jmp 0xc494
leaq -0x38(%rbp), %rdi
callq 0x9380
testb $0x1, %al
jne 0xc5ec
leaq -0x50(%rbp), %rdi
callq 0x9380
testb $0x1, %al
jne 0xc5ec
leaq -0x38(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x3460
movq -0x168(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x34d0
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x92a0
jmp 0xc4f5
leaq -0x50(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x3460
movq -0x170(%rbp), %rdi
movq %rax, -0xc0(%rbp)
callq 0x34d0
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x92a0
jmp 0xc534
leaq -0x38(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x3460
movq -0x188(%rbp), %rdi
movq %rax, -0xd0(%rbp)
callq 0x34d0
movq %rax, -0xd8(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x3460
movq -0x180(%rbp), %rdi
movq %rax, -0xe0(%rbp)
callq 0x34d0
movq %rax, -0xe8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9350
movq %rax, -0x178(%rbp)
jmp 0xc59a
movq -0x178(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
movq -0xe8(%rbp), %rcx
movq -0xf0(%rbp), %r8
callq 0xc910
movq %rax, -0x190(%rbp)
jmp 0xc5d9
movq -0x190(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0xc709
leaq -0x38(%rbp), %rdi
callq 0x9380
testb $0x1, %al
jne 0xc67d
leaq -0x38(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x3460
movq -0x1a0(%rbp), %rdi
movq %rax, -0x100(%rbp)
callq 0x34d0
movq %rax, -0x108(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9350
movq %rax, -0x198(%rbp)
jmp 0xc639
movq -0x198(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x100(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq -0x110(%rbp), %rdx
callq 0xc8a0
movq %rax, -0x1a8(%rbp)
jmp 0xc66a
movq -0x1a8(%rbp), %rax
movq %rax, -0x118(%rbp)
jmp 0xc707
leaq -0x50(%rbp), %rdi
callq 0x9380
testb $0x1, %al
jne 0xc705
leaq -0x50(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x3460
movq -0x1b8(%rbp), %rdi
movq %rax, -0x120(%rbp)
callq 0x34d0
movq %rax, -0x128(%rbp)
movq -0x18(%rbp), %rdi
callq 0x9350
movq %rax, -0x1b0(%rbp)
jmp 0xc6c6
movq -0x1b0(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x120(%rbp), %rdi
movq -0x128(%rbp), %rsi
movq -0x130(%rbp), %rdx
callq 0xc8a0
movq %rax, -0x1c0(%rbp)
jmp 0xc6f7
movq -0x1c0(%rbp), %rax
movq %rax, -0x138(%rbp)
jmp 0xc707
jmp 0xc709
leaq -0x50(%rbp), %rdi
callq 0x33e0
leaq -0x38(%rbp), %rdi
callq 0x33e0
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x2220
nopl (%rax)