// File: /workspace/github_cpp_makefile_star2_O2/derekzwy[P]an-optimal-tcam-encoding-algorithm-for-packet-classification/cps-tss.cc
// Function: tuples_num(std::vector<pc_rule*, std::allocator<pc_rule*>>&)
void tuples_num(vector<pc_rule*> &pc) 
{ 
    array<int, MAXDIMENSIONS> tuple;
    map<array<int, MAXDIMENSIONS>, int> tuple_map;


    for(auto rule = pc.begin(); rule != pc.end(); rule++) {
        for(int i = 0; i< MAXDIMENSIONS; i++) {
            int prefixlen;
            if(i == 0 || i == 1) 
                prefixlen = 32 - __builtin_popcount((*rule)->field[i].high - (*rule)->field[i].low);
            if(i == 2 || i == 3)
                prefixlen = 16 - __builtin_popcount((*rule)->field[i].high - (*rule)->field[i].low);
            if( i == 4) 
                prefixlen = 8 - __builtin_popcount((*rule)->field[i].high - (*rule)->field[i].low);
            tuple[i] = prefixlen;
        }

        tuple_map[tuple] ++;
    }

    cout <<"tuple numbers: "<< tuple_map.size()<< endl;

    /*for(auto tuple_pair = tuple_map.begin(); tuple_pair != tuple_map.end(); tuple_pair ++) {
        for_each(tuple_pair->first.begin(), tuple_pair->first.end(), [](int i ){cout<<i<<" ";});
        cout<< "  " << tuple_pair->second<<endl;
    }*/
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
leaq -0x58(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq %rdi, -0x30(%rbp)
movq (%rdi), %r12
movl $0x33333333, %r13d      # imm = 0x33333333
pushq $0x10
popq %rbx
pushq $0x8
popq %r15
movq -0x30(%rbp), %rax
cmpq 0x8(%rax), %r12
je 0x277c
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x14, %rax
je 0x2764
cmpq $0x1, %rcx
ja 0x26c3
movq (%r12), %rdx
movl 0x10(%rdx,%rax,4), %esi
subl 0x8(%rdx,%rax,4), %esi
notl %esi
movl %esi, %edx
shrl %edx
andl $0x55555555, %edx       # imm = 0x55555555
subl %edx, %esi
movl %esi, %edx
andl %r13d, %edx
shrl $0x2, %esi
andl %r13d, %esi
addl %edx, %esi
movl %esi, %edx
shrl $0x4, %edx
addl %esi, %edx
andl $0xf0f0f0f, %edx        # imm = 0xF0F0F0F
imull $0x1010101, %edx, %r14d # imm = 0x1010101
shrl $0x18, %r14d
movl %ecx, %edx
andl $0x6, %edx
cmpl $0x2, %edx
jne 0x270e
movq (%r12), %rdx
movl 0x10(%rdx,%rax,4), %esi
subl 0x8(%rdx,%rax,4), %esi
movl %esi, %edx
shrl %edx
andl $0x55555555, %edx       # imm = 0x55555555
subl %edx, %esi
movl %esi, %edx
andl %r13d, %edx
shrl $0x2, %esi
andl %r13d, %esi
addl %edx, %esi
movl %esi, %edx
shrl $0x4, %edx
addl %esi, %edx
andl $0xf0f0f0f, %edx        # imm = 0xF0F0F0F
imull $0x1010101, %edx, %edx  # imm = 0x1010101
shrl $0x18, %edx
movl %ebx, %r14d
subl %edx, %r14d
cmpq $0x10, %rax
jne 0x2753
movq (%r12), %rdx
movl 0x50(%rdx), %esi
subl 0x48(%rdx), %esi
movl %esi, %edx
shrl %edx
andl $0x55555555, %edx       # imm = 0x55555555
subl %edx, %esi
movl %esi, %edx
andl %r13d, %edx
shrl $0x2, %esi
andl %r13d, %esi
addl %edx, %esi
movl %esi, %edx
shrl $0x4, %edx
addl %esi, %edx
andl $0xf0f0f0f, %edx        # imm = 0xF0F0F0F
imull $0x1010101, %edx, %edx  # imm = 0x1010101
shrl $0x18, %edx
movl %r15d, %r14d
subl %edx, %r14d
movl %r14d, -0x74(%rbp,%rax)
incq %rcx
addq $0x4, %rax
jmp 0x2674
leaq -0x60(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x2b1e
incl (%rax)
addq $0x8, %r12
jmp 0x2662
movq 0x6855(%rip), %rdi      # 0x8fd8
leaq 0x38c5(%rip), %rsi      # 0x604f
callq 0x2130
movq -0x38(%rbp), %rsi
movq %rax, %rdi
callq 0x20c0
movq %rax, %rdi
callq 0x2090
leaq -0x60(%rbp), %rdi
callq 0x2bb0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27bd
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2bb0
movq %rbx, %rdi
callq 0x2230