// File: 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 $0x188, %rsp            # imm = 0x188
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x9110
movl $0x0, -0x48(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x108(%rbp), %r13
addq $0x8, %r13
addq $0x20, %r13
leaq -0x108(%rbp), %r12
addq $0x8, %r12
addq $0x20, %r12
addq $0x8, %r12
leaq -0x108(%rbp), %r15
addq $0x8, %r15
addq $0x30, %r15
leaq -0x108(%rbp), %r14
addq $0x8, %r14
addq $0x30, %r14
addq $0x8, %r14
leaq 0x6d78(%rip), %rsi      # 0xd164
leaq -0x4c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
leaq -0x54(%rbp), %r8
leaq -0x58(%rbp), %r9
leaq -0x5c(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x64(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x68(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x6c(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x74(%rbp), %rbx
leaq -0x78(%rbp), %r11
leaq -0x7c(%rbp), %r10
leaq -0x80(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x140(%rbp), %rax
movq %rax, (%rsp)
movq -0x138(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x130(%rbp), %rax
movq %rax, 0x10(%rsp)
movq -0x128(%rbp), %rax
movq %rax, 0x18(%rsp)
movq -0x120(%rbp), %rax
movq %rax, 0x20(%rsp)
movq -0x118(%rbp), %rax
movq %rax, 0x28(%rsp)
movq -0x110(%rbp), %rax
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq %r15, 0x40(%rsp)
movq %r14, 0x48(%rsp)
movq %rbx, 0x50(%rsp)
movq %r11, 0x58(%rsp)
movq %r10, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movb $0x0, %al
callq 0x2230
cmpl $0x12, %eax
je 0x64dc
jmp 0x668f
movl -0x5c(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x4c(%rbp), %edi
movl -0x50(%rbp), %esi
movl -0x54(%rbp), %edx
movl -0x58(%rbp), %ecx
movl -0x5c(%rbp), %r8d
leaq -0x108(%rbp), %r9
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x60f0
movl -0x60(%rbp), %edi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
movl -0x6c(%rbp), %ecx
movl -0x70(%rbp), %r8d
leaq -0x108(%rbp), %r9
movl $0x1, (%rsp)
callq 0x60f0
cmpl $0xff, -0x78(%rbp)
jne 0x659d
movl -0x74(%rbp), %eax
movq %rax, -0xc0(%rbp)
movl -0x74(%rbp), %eax
movq %rax, -0xb8(%rbp)
jmp 0x65e0
cmpl $0x0, -0x78(%rbp)
jne 0x65c4
movq $0x0, -0xc0(%rbp)
movq $0xff, -0xb8(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x65de
leaq 0x6bdd(%rip), %rdi      # 0xd1a8
movb $0x0, %al
callq 0x2030
movl $0x0, -0x2c(%rbp)
jmp 0x6695
jmp 0x65e0
movl -0x44(%rbp), %eax
movl %eax, -0x108(%rbp)
cmpq $0x0, -0x100(%rbp)
jne 0x660a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0xf8(%rbp)
jne 0x660a
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
cmpq $0x0, -0xf0(%rbp)
jne 0x662b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0xe8(%rbp)
jne 0x662b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
cmpq $0x0, -0xe0(%rbp)
jne 0x664b
cmpq $0xffff, -0xd8(%rbp) # imm = 0xFFFF
jne 0x664b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
cmpq $0x0, -0xd0(%rbp)
jne 0x666b
cmpq $0xffff, -0xc8(%rbp) # imm = 0xFFFF
jne 0x666b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
cmpl $0x5, -0x48(%rbp)
je 0x668a
movq -0x40(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x9160
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x6396
movl -0x44(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)