// File: /workspace/github_cpp_makefile_star2_O2/shuhaosun[P]Splitter-Windowed-CountMin/main.cpp
// Function: main
int main(int argc, char** argv)
{
    if(argc == 2)
    {
        filename_FlowTraffic = argv[1];
    }

	unmp.clear();
    unmp1.clear();
	double val;


    double memory = 0.05;  //MB

    int w = memory * 1024 * 1024 * 8.0 / COUNTER_SIZE;
    printf("error_allowed = %f\n", tau*WINDOW_SIZE/((double)w / (double)LOW_HASH_NUM));


    printf("\n******************************************************************************\n\n");
    printf("Evaluation starts!\n\n");


    Splitter *splitter;

    int packageNum = 0;

 	FILE *file_FlowTraffic = fopen(filename_FlowTraffic, "r");

    while(fgets(insert[packageNum], 200, file_FlowTraffic) != NULL)
    {
        unmp[string(insert[packageNum])]++;

        packageNum++;

        if(packageNum == N_INSERT)
            break;
    }
    fclose(file_FlowTraffic);


    for (int i = 7000; i < 10000; i++)
    {
        unmp1[string(insert[i])]++;
    }

    query = new char*[unmp.size()];
    for(int i = 0; i < unmp.size(); ++i)
        query[i] = new char[200];


    int k = 0, max_freq = 0;
    for(unordered_map<string, int>::iterator it = unmp.begin(); it != unmp.end(); it++, k++)
    {
        strcpy(query[k], (it->first).c_str());

        int tmpVal = it->second;
        max_freq = max_freq > tmpVal ? max_freq : tmpVal;
    }
    printf("max_freq = %d\n", max_freq);


    splitter = new Splitter(w / LOW_HASH_NUM, LOW_HASH_NUM);
    for(int i = 0; i < 10000; i++)
    {
        splitter->Insert(insert[i], i);
    }
    printf("packageNum = %d\n", packageNum);


    double re_cm = 0.0;
    double re_cm_sum = 0.0;
    double ae_cm = 0.0;
    double ae_cm_sum = 0.0;
	double val_cm = 0.0;    
    int cm_cor_num = 00;

    char temp[200];

    int under_E = 0;
    int over_E = 0;
    for(unordered_map<string, int>::iterator it = unmp1.begin(); it != unmp1.end(); it++)
    {
        strcpy(temp, (it->first).c_str());
    	val = (double)it->second;

		val_cm = splitter->Query(temp, 9999);
        if (val_cm < val)
        {
            under_E++;
        }
        if (val_cm > val)
        {
            over_E++;
        }

        re_cm = fabs(val_cm - val) / (val * 1.0);
        ae_cm = fabs(val_cm - val);
        re_cm_sum += re_cm;
        ae_cm_sum += ae_cm;

        if(val == val_cm)
            cm_cor_num++;
    }

    double extra;
    extra = (double)splitter->memoryusage / 8 / 1024 / 1024;
    printf("sketch memoryusage is : %f\n", memory);
    printf("extra memoryusage is : %f\n\n", extra);

    double a = unmp1.size() * 1.0;
    double b = packageNum;

    printf("under_E = : %d\n", under_E);
    printf("over_E = : %d\n\n\n", over_E);
    printf("cm\tcorrect rate = %lf\n", cm_cor_num * 1.0 / a);
    printf("-------------------------------------\n\n");

    printf("are_cm = %lf\n", re_cm_sum / a);
    printf("-------------------------------------\n\n");

    printf("aae_cm = %lf\n", ae_cm_sum / a);
    printf("-------------------------------------\n\n");


    printf("Evaluation Ends!\n");
    printf("******************************************************************************\n\n");

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
cmpl $0x2, %edi
jne 0x1616
movq 0x8(%rsi), %rax
movq %rax, 0x4f02(%rip)      # 0x6518
leaq 0xe4e6b3b(%rip), %r14   # 0xe4e8158
movq %r14, %rdi
callq 0x1d7e
leaq 0xe4e6b64(%rip), %rdi   # 0xe4e8190
callq 0x1d7e
leaq 0x1a93(%rip), %rdi      # 0x30cb
movsd 0x19f8(%rip), %xmm0     # 0x3038
movb $0x1, %al
callq 0x1040
leaq 0x1b4d(%rip), %rdi      # 0x319b
callq 0x11b0
leaq 0x1b92(%rip), %rdi      # 0x31ec
callq 0x11b0
movq 0x4eb2(%rip), %rdi      # 0x6518
leaq 0x1a72(%rip), %rsi      # 0x30df
callq 0x1140
movq %rax, %r15
leaq 0x4ed4(%rip), %r12      # 0x6550
xorl %ebx, %ebx
leaq 0x30(%rsp), %r13
leaq 0xf(%rsp), %rbp
movq %r12, %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x11c0
testq %rax, %rax
je 0x16d8
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1080
movq %r14, %rdi
movq %r13, %rsi
callq 0x200e
incl (%rax)
movq %r13, %rdi
callq 0x10c0
incq %rbx
addq $0xc8, %r12
cmpq $0xf4240, %rbx          # imm = 0xF4240
jne 0x1688
movl $0xf4240, %ebx          # imm = 0xF4240
movq %r15, %rdi
callq 0x1120
xorl %ebp, %ebp
leaq 0x4e67(%rip), %r14      # 0x6550
leaq 0x30(%rsp), %r15
leaq 0xf(%rsp), %r12
leaq 0xe4e6a96(%rip), %r13   # 0xe4e8190
cmpq $0x927c0, %rbp          # imm = 0x927C0
je 0x1737
leaq (%r14,%rbp), %rsi
addq $0x155cc0, %rsi         # imm = 0x155CC0
movq %r15, %rdi
movq %r12, %rdx
callq 0x1080
movq %r13, %rdi
movq %r15, %rsi
callq 0x200e
incl (%rax)
movq %r15, %rdi
callq 0x10c0
addq $0xc8, %rbp
jmp 0x16fa
movq 0xe4e6a32(%rip), %r15   # 0xe4e8170
movq %r15, %rax
shrq $0x3d, %rax
leaq (,%r15,8), %rcx
xorl %r12d, %r12d
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x1030
movq %rax, 0xe4e69e6(%rip)   # 0xe4e8150
cmpq %r12, %r15
je 0x1789
movl $0xc8, %edi
callq 0x1030
movq 0xe4e69d0(%rip), %rcx   # 0xe4e8150
movq %rax, (%rcx,%r12,8)
incq %r12
jmp 0x176a
xorl %r15d, %r15d
leaq 0xe4e69d5(%rip), %r12   # 0xe4e8168
xorl %ebp, %ebp
movq (%r12), %r12
testq %r12, %r12
je 0x17c3
movq 0xe4e69ab(%rip), %rax   # 0xe4e8150
movq (%rax,%r15), %rdi
movq 0x8(%r12), %rsi
callq 0x1100
movl 0x28(%r12), %eax
cmpl %eax, %ebp
cmovlel %eax, %ebp
addq $0x8, %r15
jmp 0x1795
leaq 0x1917(%rip), %rdi      # 0x30e1
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
movl $0x7a13b0, %edi         # imm = 0x7A13B0
callq 0x1130
movq %rax, %r15
pushq $0x4
popq %rdx
movq %rax, %rdi
movl $0x1999, %esi           # imm = 0x1999
callq 0x1a44
xorl %r12d, %r12d
cmpq $0x2710, %r12           # imm = 0x2710
je 0x1816
movq %r15, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x1af2
incq %r12
addq $0xc8, %r14
jmp 0x17f3
leaq 0x18d3(%rip), %rdi      # 0x30f0
xorl %r12d, %r12d
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xe4e6966(%rip), %r13   # 0xe4e81a0
leaq 0x30(%rsp), %r14
movsd %xmm0, 0x28(%rsp)
xorl %ebp, %ebp
xorl %ebx, %ebx
movq (%r13), %r13
testq %r13, %r13
je 0x18e0
movq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x1100
cvtsi2sdl 0x28(%r13), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl $0x270f, %edx           # imm = 0x270F
callq 0x1cae
xorl %eax, %eax
movsd 0x10(%rsp), %xmm3
ucomisd %xmm0, %xmm3
seta %al
addl %eax, %ebp
xorl %eax, %eax
ucomisd %xmm3, %xmm0
seta %al
addl %eax, %ebx
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
andpd 0x17d6(%rip), %xmm1     # 0x3080
movsd 0x28(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x28(%rsp)
divsd %xmm3, %xmm1
movsd 0x20(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x20(%rsp)
cmpeqsd %xmm3, %xmm0
movq %xmm0, %rax
subl %eax, %r12d
jmp 0x1849
cvtsi2sdl 0x7a13ac(%r15), %xmm1
mulsd 0x174f(%rip), %xmm1     # 0x3040
movsd 0x174f(%rip), %xmm0     # 0x3048
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0x17f3(%rip), %rdi      # 0x3101
movsd 0x173a(%rip), %xmm0     # 0x3050
movb $0x1, %al
callq 0x1040
leaq 0x17f9(%rip), %rdi      # 0x311d
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x1040
movsd 0xe4e686f(%rip), %xmm0  # 0xe4e81a8
unpcklps 0x1750(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1758(%rip), %xmm0     # 0x30a0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
leaq 0x17d8(%rip), %rdi      # 0x3139
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
leaq 0x17d8(%rip), %rdi      # 0x3149
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
cvtsi2sd %r12d, %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x17ce(%rip), %rdi      # 0x315a
movb $0x1, %al
callq 0x1040
leaq 0x1866(%rip), %rbx      # 0x3200
movq %rbx, %rdi
callq 0x11b0
movsd 0x20(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x17bc(%rip), %rdi      # 0x3171
movb $0x1, %al
callq 0x1040
movq %rbx, %rdi
callq 0x11b0
movsd 0x28(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x17a8(%rip), %rdi      # 0x317f
movb $0x1, %al
callq 0x1040
movq %rbx, %rdi
callq 0x11b0
leaq 0x183a(%rip), %rdi      # 0x3227
callq 0x11b0
leaq 0x17a3(%rip), %rdi      # 0x319c
callq 0x11b0
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r15, %rdi
callq 0x1110
jmp 0x1a35
jmp 0x1a28
jmp 0x1a23
movq %rax, %rbx
jmp 0x1a35
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x10c0
movq %rbx, %rdi
callq 0x11e0
nop