// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
leaq -0x50(%rbp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq (%rdi), %r12
cmpq 0x8(%rdi), %r12
je 0x2862
movq %rdi, %rbx
movl $0x33333333, %r13d      # imm = 0x33333333
leaq -0x6c(%rbp), %r15
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x1, %rcx
ja 0x279a
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 0x27e8
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 $0x10, %r14d
subl %edx, %r14d
cmpq $0x10, %rax
jne 0x2830
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 $0x8, %r14d
subl %edx, %r14d
movl %r14d, -0x6c(%rbp,%rax)
incq %rcx
addq $0x4, %rax
cmpq $0x14, %rax
jne 0x2755
leaq -0x58(%rbp), %rdi
movq %r15, %rsi
callq 0x2d0a
incl (%rax)
addq $0x8, %r12
cmpq 0x8(%rbx), %r12
jne 0x2751
movq 0x776f(%rip), %rdi      # 0x9fd8
leaq 0x47df(%rip), %rsi      # 0x704f
movl $0xf, %edx
callq 0x2190
movq -0x30(%rbp), %rsi
movq 0x7753(%rip), %rdi      # 0x9fd8
callq 0x20c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
leaq -0x58(%rbp), %rdi
callq 0x2dc2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x28ce
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x2dc2
movq %rbx, %rdi
callq 0x2240