// File: derekzwy[P]an-optimal-tcam-encoding-algorithm-for-packet-classification/cps-tss.cc
// Function: bitstring_from_rule(pc_rule*)
array<char, MAX_BITS> bitstring_from_rule(pc_rule *rule) 
{
    array<char, MAX_BITS> bitstring;
    int pos = 0;
    for(int i = 2; i < 4; 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);

        int tail = __builtin_popcount(rule->field[i].high - rule->field[i].low);

        uint32_t bits = rule->field[i].high >> tail;
        for(int j = prefixlen -1; j >= 0; j--) {
            //from high to low bits 
            if(bits & (1ULL<< j)) {
                bitstring[pos + prefixlen-j-1] = '1';
            }
            else {
                bitstring[pos + prefixlen-j-1] = '0';
            }
        }
        for(int j = 0; j< tail; j++) {
            bitstring[j+prefixlen+pos] = '*';
        }

        if(i==0 || i == 1) {
            pos += 32;
        }
        if(i==2 || i == 3) {
            pos += 16;
        }
        if(i==4)
            pos += 8;
    }
    return move(bitstring);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x58(%rbp)
movq %rdi, -0x50(%rbp)
movq %rsi, -0x8(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x2, -0x30(%rbp)
cmpl $0x4, -0x30(%rbp)
jge 0x2db8
cmpl $0x0, -0x30(%rbp)
je 0x2af8
cmpl $0x1, -0x30(%rbp)
jne 0x2b63
movq -0x8(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
addq $0x8, %rcx
movslq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movl %eax, %ecx
movl %ecx, %eax
shrl %eax
andl $0x55555555, %eax       # imm = 0x55555555
subl %eax, %ecx
movl %ecx, %eax
andl $0x33333333, %eax       # imm = 0x33333333
shrl $0x2, %ecx
andl $0x33333333, %ecx       # imm = 0x33333333
addl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
addl %ecx, %eax
andl $0xf0f0f0f, %eax        # imm = 0xF0F0F0F
imull $0x1010101, %eax, %ecx  # imm = 0x1010101
shrl $0x18, %ecx
movl $0x20, %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x30(%rbp)
je 0x2b6f
cmpl $0x3, -0x30(%rbp)
jne 0x2bda
movq -0x8(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
addq $0x8, %rcx
movslq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movl %eax, %ecx
movl %ecx, %eax
shrl %eax
andl $0x55555555, %eax       # imm = 0x55555555
subl %eax, %ecx
movl %ecx, %eax
andl $0x33333333, %eax       # imm = 0x33333333
shrl $0x2, %ecx
andl $0x33333333, %ecx       # imm = 0x33333333
addl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
addl %ecx, %eax
andl $0xf0f0f0f, %eax        # imm = 0xF0F0F0F
imull $0x1010101, %eax, %ecx  # imm = 0x1010101
shrl $0x18, %ecx
movl $0x10, %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x4, -0x30(%rbp)
jne 0x2c4b
movq -0x8(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
addq $0x8, %rcx
movslq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movl %eax, %ecx
movl %ecx, %eax
shrl %eax
andl $0x55555555, %eax       # imm = 0x55555555
subl %eax, %ecx
movl %ecx, %eax
andl $0x33333333, %eax       # imm = 0x33333333
shrl $0x2, %ecx
andl $0x33333333, %ecx       # imm = 0x33333333
addl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
addl %ecx, %eax
andl $0xf0f0f0f, %eax        # imm = 0xF0F0F0F
imull $0x1010101, %eax, %ecx  # imm = 0x1010101
shrl $0x18, %ecx
movl $0x8, %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
addq $0x8, %rcx
movslq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movl %eax, %ecx
movl %ecx, %eax
shrl %eax
andl $0x55555555, %eax       # imm = 0x55555555
subl %eax, %ecx
movl %ecx, %eax
andl $0x33333333, %eax       # imm = 0x33333333
shrl $0x2, %ecx
andl $0x33333333, %ecx       # imm = 0x33333333
addl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
addl %ecx, %eax
andl $0xf0f0f0f, %eax        # imm = 0xF0F0F0F
imull $0x1010101, %eax, %eax  # imm = 0x1010101
shrl $0x18, %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl -0x38(%rbp), %ecx
shrq %cl, %rax
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jl 0x2d3d
movl -0x3c(%rbp), %eax
movl -0x40(%rbp), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2d15
movl -0x2c(%rbp), %eax
addl -0x34(%rbp), %eax
subl -0x40(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x36b0
movb $0x31, (%rax)
jmp 0x2d30
movl -0x2c(%rbp), %eax
addl -0x34(%rbp), %eax
subl -0x40(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x36b0
movb $0x30, (%rax)
jmp 0x2d32
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2cd8
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x2d6f
movl -0x44(%rbp), %eax
addl -0x34(%rbp), %eax
addl -0x2c(%rbp), %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x36b0
movb $0x2a, (%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2d44
cmpl $0x0, -0x30(%rbp)
je 0x2d7b
cmpl $0x1, -0x30(%rbp)
jne 0x2d84
movl -0x2c(%rbp), %eax
addl $0x20, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x2, -0x30(%rbp)
je 0x2d90
cmpl $0x3, -0x30(%rbp)
jne 0x2d99
movl -0x2c(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x4, -0x30(%rbp)
jne 0x2da8
movl -0x2c(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2daa
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2ae2
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x28(%rbp), %rdx
movq %rdx, (%rcx)
movq -0x20(%rbp), %rdx
movq %rdx, 0x8(%rcx)
movq -0x18(%rbp), %rdx
movq %rdx, 0x10(%rcx)
movq -0x10(%rbp), %rdx
movq %rdx, 0x18(%rcx)
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)