// File: derekzwy[P]an-optimal-tcam-encoding-algorithm-for-packet-classification/rulesutils.cc
// Function: remove_redund_rt(std::vector<pc_rule*, std::allocator<pc_rule*>>&, rule_boundary&, bool)
void remove_redund_rt(vector<pc_rule*> &pr, rule_boundary &b, bool large) 
{
    vector<pc_rule*> rulelist;
    
    range br;
    
    rnode rt[2];
    vector<pc_rule*> set[2];
    //map<int, int> sect;
    vector<pc_rule*> sect;

    init_rnode(rt, b);
    //int count = 0;

    for(auto rule = pr.begin(); rule != pr.end(); ++ rule) {
        sect.clear();
        for(int i = 0; i < 2; i++) {
            br = range_in_boundary_1D((*rule)->field[i], b.field[i]); 
            set[i].clear();
            rt_qry_insert(rt+i, rg(br.low, 
                    br.high), set[i], (*rule));
        }

        sort(set[0].begin(), set[0].end());
        sort(set[1].begin(), set[1].end());
        set_intersection(set[0].begin(), set[0].end(),
                set[1].begin(), set[1].end(),
                back_inserter(sect));

        if(sect.empty()) {
            rulelist.push_back(*rule);
        }
        else {
            bool found = false;
            for(auto check_rule = sect.begin();
                    check_rule != sect.end();
                    check_rule++) {
                if(is_equal(**check_rule, **rule, b)) {
                    found = true;
                    //count++;
                    //cout<<(*rule)->priority<<" was hide by " <<*check_rule<<endl;
                    break;
                }
            }

            if(!found) {
                rulelist.push_back(*rule);
            }
        }

    }

    rt_destory(rt[0].left);
    rt_destory(rt[0].right);
    rt_destory(rt[1].left);
    rt_destory(rt[1].right);

    pr = move(rulelist);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5a0, %rsp            # imm = 0x5A0
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3300
leaq -0x40(%rbp), %rdi
callq 0x37e0
jmp 0x695e
leaq -0xb0(%rbp), %rax
movq %rax, -0x330(%rbp)
movq %rax, %rcx
addq $0x60, %rcx
movq %rcx, -0x328(%rbp)
movq %rax, -0x320(%rbp)
movq -0x320(%rbp), %rdi
movq %rdi, -0x338(%rbp)
callq 0x9210
jmp 0x6996
movq -0x328(%rbp), %rcx
movq -0x338(%rbp), %rax
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, -0x320(%rbp)
jne 0x6981
leaq -0xe0(%rbp), %rax
movq %rax, %rcx
addq $0x30, %rcx
movq %rcx, -0x348(%rbp)
movq %rax, -0x340(%rbp)
movq -0x340(%rbp), %rdi
movq %rdi, -0x350(%rbp)
callq 0x3300
movq -0x350(%rbp), %rax
movq -0x348(%rbp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0x340(%rbp)
jne 0x69d0
leaq -0xf8(%rbp), %rdi
callq 0x3300
movq -0x10(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x68c0
jmp 0x6a1f
movq -0x8(%rbp), %rdi
callq 0x3460
movq %rax, -0x100(%rbp)
movq -0x8(%rbp), %rdi
callq 0x34d0
movq %rax, -0x108(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3490
testb $0x1, %al
jne 0x6a5b
jmp 0x6f9c
leaq -0xf8(%rbp), %rdi
callq 0x9280
movl $0x0, -0x10c(%rbp)
cmpl $0x2, -0x10c(%rbp)
jge 0x6c5c
leaq -0x100(%rbp), %rdi
callq 0x3500
movq (%rax), %rax
movslq -0x10c(%rbp), %rcx
shlq $0x4, %rcx
movups 0x8(%rax,%rcx), %xmm0
movaps %xmm0, -0x130(%rbp)
movq -0x10(%rbp), %rax
movslq -0x10c(%rbp), %rcx
shlq $0x4, %rcx
movups (%rax,%rcx), %xmm0
movaps %xmm0, -0x140(%rbp)
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rsi
movq -0x140(%rbp), %rdx
movq -0x138(%rbp), %rcx
callq 0x66b0
movq %rdx, -0x360(%rbp)
movq %rax, -0x358(%rbp)
jmp 0x6aef
movq -0x360(%rbp), %rax
movq -0x358(%rbp), %rcx
movq %rcx, -0x120(%rbp)
movq %rax, -0x118(%rbp)
movups -0x120(%rbp), %xmm0
movaps %xmm0, -0x40(%rbp)
movslq -0x10c(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq -0xe0(%rbp,%rax,8), %rdi
callq 0x9280
movslq -0x10c(%rbp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq -0xb0(%rbp,%rax), %rax
movq %rax, -0x368(%rbp)
movl -0x40(%rbp), %esi
movl -0x38(%rbp), %edx
leaq -0x148(%rbp), %rdi
callq 0x91d0
jmp 0x6b60
movslq -0x10c(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq -0xe0(%rbp,%rax,8), %rax
movq %rax, -0x370(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x3500
movq -0x368(%rbp), %rdi
movq -0x370(%rbp), %rdx
movq (%rax), %rcx
leaq -0x148(%rbp), %rsi
callq 0xb7e0
jmp 0x6ba5
jmp 0x6ba7
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x6a71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x710a
movq -0x330(%rbp), %rcx
movq %rax, %rsi
movq -0x338(%rbp), %rax
movq %rsi, -0x48(%rbp)
movl %edx, -0x4c(%rbp)
cmpq %rax, %rcx
movq %rax, -0x378(%rbp)
je 0x6c21
movq -0x378(%rbp), %rdi
addq $-0x30, %rdi
movq %rdi, -0x380(%rbp)
callq 0x9260
movq -0x330(%rbp), %rcx
movq -0x380(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x378(%rbp)
jne 0x6bf0
jmp 0x710a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x33e0
leaq -0xe0(%rbp), %rax
movq %rax, -0x390(%rbp)
addq $0x30, %rax
movq %rax, -0x388(%rbp)
jmp 0x708d
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x398(%rbp)
callq 0x3460
movq -0x398(%rbp), %rdi
movq %rax, -0x150(%rbp)
callq 0x34d0
movq %rax, -0x158(%rbp)
movq -0x150(%rbp), %rdi
movq -0x158(%rbp), %rsi
callq 0x92a0
jmp 0x6c9e
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
callq 0x3460
movq -0x3a0(%rbp), %rdi
movq %rax, -0x160(%rbp)
callq 0x34d0
movq %rax, -0x168(%rbp)
movq -0x160(%rbp), %rdi
movq -0x168(%rbp), %rsi
callq 0x92a0
jmp 0x6ce0
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
callq 0x3460
movq -0x3b8(%rbp), %rdi
movq %rax, -0x170(%rbp)
callq 0x34d0
movq %rax, -0x178(%rbp)
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x3b0(%rbp)
callq 0x3460
movq -0x3b0(%rbp), %rdi
movq %rax, -0x180(%rbp)
callq 0x34d0
movq %rax, -0x188(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x9350
movq %rax, -0x3a8(%rbp)
jmp 0x6d4f
movq -0x3a8(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x170(%rbp), %rdi
movq -0x178(%rbp), %rsi
movq -0x180(%rbp), %rdx
movq -0x188(%rbp), %rcx
movq -0x190(%rbp), %r8
callq 0x92e0
movq %rax, -0x3c0(%rbp)
jmp 0x6d8e
movq -0x3c0(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x9380
testb $0x1, %al
jne 0x6dae
jmp 0x6dcd
leaq -0x100(%rbp), %rdi
callq 0x3500
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x93c0
jmp 0x6dc8
jmp 0x6f89
movb $0x0, -0x199(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3460
movq %rax, -0x1a8(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x34d0
movq %rax, -0x1b0(%rbp)
leaq -0x1a8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3490
testb $0x1, %al
jne 0x6e16
jmp 0x6f62
leaq -0x1a8(%rbp), %rdi
callq 0x3500
movq (%rax), %rsi
leaq -0x238(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
movl $0x88, %edx
movq %rdx, -0x3e8(%rbp)
callq 0x20e0
leaq -0x100(%rbp), %rdi
callq 0x3500
movq -0x3e8(%rbp), %rdx
movq (%rax), %rsi
leaq -0x2c0(%rbp), %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x20e0
movq -0x3e0(%rbp), %rsi
movq -0x10(%rbp), %rax
movups 0x40(%rax), %xmm0
movaps %xmm0, -0x2d0(%rbp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups 0x30(%rax), %xmm3
movaps %xmm3, -0x2e0(%rbp)
movaps %xmm2, -0x2f0(%rbp)
movaps %xmm1, -0x300(%rbp)
movaps %xmm0, -0x310(%rbp)
movq %rsp, %rax
movl $0x11, %ecx
movq %rcx, -0x3d0(%rbp)
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq -0x3d8(%rbp), %rsi
movq -0x3d0(%rbp), %rcx
movq %rax, %rdi
addq $0x88, %rdi
rep  movsq (%rsi), %es:(%rdi)
movaps -0x2d0(%rbp), %xmm0
movups %xmm0, 0x150(%rax)
movaps -0x310(%rbp), %xmm0
movaps -0x300(%rbp), %xmm1
movaps -0x2f0(%rbp), %xmm2
movaps -0x2e0(%rbp), %xmm3
movups %xmm3, 0x140(%rax)
movups %xmm2, 0x130(%rax)
movups %xmm1, 0x120(%rax)
movups %xmm0, 0x110(%rax)
callq 0x6720
movb %al, -0x3c1(%rbp)
jmp 0x6f31
movb -0x3c1(%rbp), %al
testb $0x1, %al
jne 0x6f3d
jmp 0x6f46
movb $0x1, -0x199(%rbp)
jmp 0x6f62
jmp 0x6f48
leaq -0x1a8(%rbp), %rdi
xorl %esi, %esi
callq 0x3630
movq %rax, -0x318(%rbp)
jmp 0x6de7
testb $0x1, -0x199(%rbp)
jne 0x6f87
leaq -0x100(%rbp), %rdi
callq 0x3500
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x93c0
jmp 0x6f85
jmp 0x6f87
jmp 0x6f89
jmp 0x6f8b
leaq -0x100(%rbp), %rdi
callq 0x9430
jmp 0x6a2f
movq -0x88(%rbp), %rdi
callq 0xb750
jmp 0x6faa
movq -0x90(%rbp), %rdi
callq 0xb750
jmp 0x6fb8
movq -0x58(%rbp), %rdi
callq 0xb750
jmp 0x6fc3
movq -0x60(%rbp), %rdi
callq 0xb750
jmp 0x6fce
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x9450
leaq -0xf8(%rbp), %rdi
callq 0x33e0
leaq -0xe0(%rbp), %rax
movq %rax, -0x3f8(%rbp)
addq $0x30, %rax
movq %rax, -0x3f0(%rbp)
movq -0x3f0(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x400(%rbp)
callq 0x33e0
movq -0x3f8(%rbp), %rcx
movq -0x400(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x3f0(%rbp)
jne 0x7000
leaq -0xb0(%rbp), %rax
movq %rax, -0x410(%rbp)
addq $0x60, %rax
movq %rax, -0x408(%rbp)
movq -0x408(%rbp), %rdi
addq $-0x30, %rdi
movq %rdi, -0x418(%rbp)
callq 0x9260
movq -0x410(%rbp), %rcx
movq -0x418(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x408(%rbp)
jne 0x704a
leaq -0x30(%rbp), %rdi
callq 0x33e0
addq $0x5a0, %rsp            # imm = 0x5A0
popq %rbp
retq
movq -0x388(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x420(%rbp)
callq 0x33e0
movq -0x390(%rbp), %rcx
movq -0x420(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x388(%rbp)
jne 0x708d
leaq -0xb0(%rbp), %rax
movq %rax, -0x430(%rbp)
addq $0x60, %rax
movq %rax, -0x428(%rbp)
movq -0x428(%rbp), %rdi
addq $-0x30, %rdi
movq %rdi, -0x438(%rbp)
callq 0x9260
movq -0x430(%rbp), %rcx
movq -0x438(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x428(%rbp)
jne 0x70d7
jmp 0x710a
leaq -0x30(%rbp), %rdi
callq 0x33e0
movq -0x48(%rbp), %rdi
callq 0x2220
nopl (%rax)