// File: /workspace/github_cpp_makefile_star2_O1/derekzwy[P]an-optimal-tcam-encoding-algorithm-for-packet-classification/rulesutils.cc
// Function: loadrules(_IO_FILE*, std::vector<pc_rule, std::allocator<pc_rule>>&)
int loadrules(FILE *fp, vector<pc_rule> &classifier) {
    int i = 0;
    int wild = 0;
    unsigned sip1, sip2, sip3, sip4, siplen;
    unsigned dip1, dip2, dip3, dip4, diplen;
    unsigned proto, protomask;
    unsigned junk, junkmask;

    pc_rule rule;

    while(1) {
        wild = 0;
        if(fscanf(fp,"@%u.%u.%u.%u/%u\t%u.%u.%u.%u/%u\t%llu : %llu\t%llu : %llu\t%x/%x\t%x/%x\n",
                    &sip1, &sip2, &sip3, &sip4, &siplen, &dip1, &dip2, &dip3, &dip4, &diplen, 
                    &rule.field[2].low, &rule.field[2].high, &rule.field[3].low, &rule.field[3].high,
                    &proto, &protomask, &junk, &junkmask) != 18) break;

        /*if(fscanf(fp,"@%u.%u.%u.%u/%u %u.%u.%u.%u/%u %llu : %llu %llu : %llu %x/%x\n",
                   &sip1, &sip2, &sip3, &sip4, &siplen, &dip1, &dip2, &dip3, &dip4, &diplen, 
                   &rule.field[2].low, &rule.field[2].high, &rule.field[3].low, &rule.field[3].high,
                   &proto, & protomask) != 16) break;*/
        rule.siplen = siplen;
        rule.diplen = diplen;
        rule.sip[0] = sip1;
        rule.sip[1] = sip2;
        rule.sip[2] = sip3;
        rule.sip[3] = sip4;
        rule.dip[0] = dip1;
        rule.dip[1] = dip2;
        rule.dip[2] = dip3;
        rule.dip[3] = dip4;

        IP2Range(sip1,sip2,sip3,sip4,siplen,&rule,0);
        IP2Range(dip1,dip2,dip3,dip4,diplen,&rule,1);

        if(protomask == 0xFF){
            rule.field[4].low = proto;
            rule.field[4].high = proto;
        }else if(protomask == 0){
            rule.field[4].low = 0;
            rule.field[4].high = 0xFF;
            wild++;
        }else{
            printf("Protocol mask error\n");
            return 0;
        }
        rule.priority = i;
        if ((rule.field[0].low == 0) && (rule.field[0].high == 0xffffffff)) {
            wild++;
        }
        if ((rule.field[1].low == 0) && (rule.field[1].high == 0xffffffff)) {
            wild++;
        }
        if ((rule.field[2].low == 0) && (rule.field[2].high == 65535)) {
            wild++;
        }
        if ((rule.field[3].low == 0) && (rule.field[3].high == 65535)) {
            wild++;
        }
        if (wild != 5) {
            classifier.push_back(rule);
            i++;
        }
    }
    return i;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbx
xorps %xmm0, %xmm0
leaq -0xc8(%rbp), %r10
movups %xmm0, 0x20(%r10)
movups %xmm0, 0x10(%r10)
movups %xmm0, (%r10)
movups %xmm0, -0x10(%r10)
movups %xmm0, -0x20(%r10)
leaq -0x5c(%rbp), %r13
leaq -0x60(%rbp), %r11
leaq -0x40(%rbp), %r14
leaq -0x44(%rbp), %r12
leaq 0x3d66(%rip), %rsi      # 0x716b
xorl %r15d, %r15d
leaq -0x58(%rbp), %rdx
leaq -0x54(%rbp), %rcx
leaq -0x50(%rbp), %r8
leaq -0x4c(%rbp), %r9
movq %rdi, -0x68(%rbp)
movl $0x0, %eax
pushq %r13
pushq %r11
pushq %r14
pushq %r12
leaq -0xb0(%rbp), %r11
pushq %r11
leaq -0xb8(%rbp), %r11
pushq %r11
leaq -0xc0(%rbp), %r11
pushq %r11
pushq %r10
leaq -0x2c(%rbp), %r10
pushq %r10
leaq -0x30(%rbp), %r10
pushq %r10
leaq -0x34(%rbp), %r10
pushq %r10
leaq -0x38(%rbp), %r10
pushq %r10
leaq -0x3c(%rbp), %r10
pushq %r10
leaq -0x48(%rbp), %r10
pushq %r10
callq 0x2250
addq $0x70, %rsp
cmpl $0x12, %eax
jne 0x3697
leaq -0xf0(%rbp), %r12
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0xffff, %r14d          # imm = 0xFFFF
xorl %r15d, %r15d
movl -0x48(%rbp), %r8d
movl %r8d, -0x98(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x58(%rbp), %edi
movl %edi, -0x90(%rbp)
movl -0x54(%rbp), %esi
movl %esi, -0x8c(%rbp)
movl -0x50(%rbp), %edx
movl %edx, -0x88(%rbp)
movl -0x4c(%rbp), %ecx
movl %ecx, -0x84(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x74(%rbp)
subq $0x8, %rsp
movq %r12, %r9
pushq $0x0
callq 0x32d1
addq $0x10, %rsp
movl -0x3c(%rbp), %edi
movl -0x38(%rbp), %esi
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x2c(%rbp), %r8d
subq $0x8, %rsp
movq %r12, %r9
pushq $0x1
callq 0x32d1
addq $0x10, %rsp
movl -0x40(%rbp), %eax
testl %eax, %eax
je 0x353d
cmpl $0xff, %eax
jne 0x3688
movl -0x44(%rbp), %eax
movq %rax, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
xorl %ecx, %ecx
jmp 0x3558
movq $0x0, -0xa8(%rbp)
movq $0xff, -0xa0(%rbp)
movl $0x1, %ecx
movl %r15d, -0xf0(%rbp)
movq -0xe0(%rbp), %rdx
xorq %r13, %rdx
xorl %eax, %eax
orq -0xe8(%rbp), %rdx
sete %al
addl %ecx, %eax
movq -0xd0(%rbp), %rcx
xorq %r13, %rcx
xorl %edx, %edx
orq -0xd8(%rbp), %rcx
sete %dl
movq -0xc0(%rbp), %rcx
xorq %r14, %rcx
xorl %esi, %esi
orq -0xc8(%rbp), %rcx
sete %sil
addl %edx, %esi
addl %eax, %esi
movq -0xb0(%rbp), %rax
xorq %r14, %rax
xorl %ecx, %ecx
orq -0xb8(%rbp), %rax
sete %cl
addl %esi, %ecx
cmpl $0x5, %ecx
je 0x35f7
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x35e9
movl $0x88, %edx
movq %rsi, %rdi
movq %r12, %rsi
callq 0x2100
addq $0x88, 0x8(%rbx)
jmp 0x35f4
movq %rbx, %rdi
movq %r12, %rdx
callq 0x53e4
incl %r15d
movq -0x68(%rbp), %rdi
leaq 0x3b69(%rip), %rsi      # 0x716b
leaq -0x58(%rbp), %rdx
leaq -0x54(%rbp), %rcx
leaq -0x50(%rbp), %r8
leaq -0x4c(%rbp), %r9
xorl %eax, %eax
leaq -0x5c(%rbp), %r10
pushq %r10
leaq -0x60(%rbp), %r10
pushq %r10
leaq -0x40(%rbp), %r10
pushq %r10
leaq -0x44(%rbp), %r10
pushq %r10
leaq -0xb0(%rbp), %r10
pushq %r10
leaq -0xb8(%rbp), %r10
pushq %r10
leaq -0xc0(%rbp), %r10
pushq %r10
leaq -0xc8(%rbp), %r10
pushq %r10
leaq -0x2c(%rbp), %r10
pushq %r10
leaq -0x30(%rbp), %r10
pushq %r10
leaq -0x34(%rbp), %r10
pushq %r10
leaq -0x38(%rbp), %r10
pushq %r10
leaq -0x3c(%rbp), %r10
pushq %r10
leaq -0x48(%rbp), %r10
pushq %r10
callq 0x2250
addq $0x70, %rsp
cmpl $0x12, %eax
je 0x3492
jmp 0x3697
leaq 0x3b81(%rip), %rdi      # 0x7210
callq 0x2200
xorl %r15d, %r15d
movl %r15d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq