// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, -0x30(%rbp)
addq $0x8, %rsi
xorl %r10d, %r10d
movl $0x2, %r8d
movl $0x10, %r9d
movl $0x33333333, %r11d      # imm = 0x33333333
movl $0xf0f0f0f, %ebx        # imm = 0xF0F0F0F
movq %r8, %rcx
shlq $0x4, %rcx
movl 0x8(%rsi,%rcx), %edx
subl (%rsi,%rcx), %edx
movl %edx, %edi
shrl %edi
movl $0x55555555, %eax       # imm = 0x55555555
andl %eax, %edi
subl %edi, %edx
movl %edx, %edi
andl %r11d, %edi
shrl $0x2, %edx
andl %r11d, %edx
addl %edi, %edx
movl %edx, %edi
shrl $0x4, %edi
addl %edx, %edi
andl %ebx, %edi
imull $0x1010101, %edi, %r14d # imm = 0x1010101
shrl $0x18, %r14d
movq 0x8(%rsi,%rcx), %r12
movl (%rsi,%rcx), %ecx
movl %r12d, %r15d
subl %ecx, %r15d
movl %r15d, %ecx
shrl %ecx
andl %eax, %ecx
movl %r15d, %edx
subl %ecx, %edx
movl %edx, %ecx
andl %r11d, %ecx
shrl $0x2, %edx
andl %r11d, %edx
addl %ecx, %edx
movl %edx, %ecx
shrl $0x4, %ecx
addl %edx, %ecx
andl %ebx, %ecx
imull $0x1010101, %ecx, %edx  # imm = 0x1010101
shrl $0x18, %edx
cmpl $0xf, %r14d
ja 0x29cb
movl $0x10, %edi
subl %r14d, %edi
movl %edx, %ecx
shrq %cl, %r12
movl %r12d, %r12d
movl %edi, %r13d
incq %r13
movl %r10d, %edi
leal -0x2(%r13), %ecx
movl $0x1, %eax
shlq %cl, %rax
testl %eax, %r12d
sete %al
xorb $0x31, %al
movslq %edi, %rdi
movb %al, -0x50(%rbp,%rdi)
decq %r13
incl %edi
cmpq $0x1, %r13
jg 0x29a5
testl %r15d, %r15d
je 0x29f4
cmpl $0x1, %edx
adcl $0x0, %edx
movl %r9d, %ecx
subl %r14d, %ecx
movslq %ecx, %rcx
addq %rbp, %rcx
addq $-0x50, %rcx
xorl %edi, %edi
movb $0x2a, (%rcx,%rdi)
incq %rdi
cmpq %rdi, %rdx
jne 0x29e8
addl $0x10, %r10d
incq %r8
addl $0x10, %r9d
cmpq $0x4, %r8
jne 0x2911
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movq -0x30(%rbp), %rax
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq