// File: /workspace/github_cpp_makefile_star2_O2/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, -0x60(%rbp)
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq -0xc8(%rbp), %rax
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movups %xmm0, -0x10(%rax)
movups %xmm0, -0x20(%rax)
leaq -0xf0(%rbp), %rbx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0xffff, %r15d          # imm = 0xFFFF
xorl %r12d, %r12d
movq %r14, %rdi
leaq 0x2e83(%rip), %rsi      # 0x616b
leaq -0x58(%rbp), %rdx
leaq -0x54(%rbp), %rcx
leaq -0x50(%rbp), %r8
leaq -0x4c(%rbp), %r9
xorl %eax, %eax
leaq -0x64(%rbp), %r10
pushq %r10
leaq -0x68(%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 0x2240
addq $0x70, %rsp
cmpl $0x12, %eax
jne 0x34b3
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 %rbx, %r9
pushq $0x0
callq 0x31b6
popq %rax
popq %rcx
movl -0x3c(%rbp), %edi
movl -0x38(%rbp), %esi
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x2c(%rbp), %r8d
subq $0x8, %rsp
movq %rbx, %r9
pushq $0x1
callq 0x31b6
popq %rax
popq %rcx
movl -0x40(%rbp), %eax
movl $0xff, %edx
testq %rax, %rax
je 0x340e
cmpl $0xff, %eax
jne 0x34a4
movl -0x44(%rbp), %eax
xorl %ecx, %ecx
movq %rax, %rdx
jmp 0x3411
pushq $0x1
popq %rcx
movq %rax, -0xa8(%rbp)
movq %rdx, -0xa0(%rbp)
movl %r12d, -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 %r15, %rcx
xorl %esi, %esi
orq -0xc8(%rbp), %rcx
sete %sil
addl %edx, %esi
addl %eax, %esi
movq -0xb0(%rbp), %rax
xorq %r15, %rax
xorl %ecx, %ecx
orq -0xb8(%rbp), %rax
sete %cl
addl %esi, %ecx
cmpl $0x5, %ecx
je 0x32de
movq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0x4c34
incl %r12d
jmp 0x32de
leaq 0x2d65(%rip), %rdi      # 0x6210
callq 0x21f0
xorl %r12d, %r12d
movl %r12d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq