// File: /workspace/github_cpp_makefile_star2_O1/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 0x1612
movq 0x8(%rsi), %rax
movq %rax, 0x4f06(%rip)      # 0x6518
leaq 0xe4e6b3f(%rip), %r14   # 0xe4e8158
movq %r14, %rdi
callq 0x1e54
leaq 0xe4e6b68(%rip), %rdi   # 0xe4e8190
callq 0x1e54
leaq 0x1a97(%rip), %rdi      # 0x30cb
movsd 0x19fc(%rip), %xmm0     # 0x3038
movb $0x1, %al
callq 0x1040
leaq 0x1b7b(%rip), %rdi      # 0x31c5
callq 0x11a0
leaq 0x1bc0(%rip), %rdi      # 0x3216
callq 0x11a0
movq 0x4eb6(%rip), %rdi      # 0x6518
leaq 0x1a76(%rip), %rsi      # 0x30df
callq 0x1140
movq %rax, %r15
leaq 0x4ed8(%rip), %r12      # 0x6550
leaq 0x40(%rsp), %rbp
xorl %ebx, %ebx
leaq 0x30(%rsp), %r13
movq %r12, %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x11b0
testq %rax, %rax
je 0x16e9
movq %rbp, 0x30(%rsp)
movq %r12, %rdi
callq 0x1090
leaq (%rax,%r12), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x207e
movq %r14, %rdi
movq %r13, %rsi
callq 0x210e
incl (%rax)
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0x16d1
callq 0x1100
incq %rbx
addq $0xc8, %r12
cmpq $0xf4240, %rbx          # imm = 0xF4240
jne 0x1684
movl $0xf4240, %ebx          # imm = 0xF4240
movq %r15, %rdi
callq 0x1110
leaq 0x15ab18(%rip), %r14    # 0x15c210
movl $0xbb8, %r13d           # imm = 0xBB8
leaq 0x30(%rsp), %r15
leaq 0xe4e6a86(%rip), %r12   # 0xe4e8190
movq %rbp, 0x30(%rsp)
movq %r14, %rdi
callq 0x1090
leaq (%rax,%r14), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x207e
movq %r12, %rdi
movq %r15, %rsi
callq 0x210e
incl (%rax)
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1742
callq 0x1100
addq $0xc8, %r14
decq %r13
jne 0x170a
movq 0xe4e6a1b(%rip), %r15   # 0xe4e8170
movq %r15, %rax
shrq $0x3d, %rax
leaq (,%r15,8), %rcx
xorl %r14d, %r14d
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x1030
movq %rax, 0xe4e69cf(%rip)   # 0xe4e8150
testq %r15, %r15
je 0x17a6
xorl %r12d, %r12d
movl $0xc8, %edi
callq 0x1030
movq 0xe4e69b6(%rip), %rcx   # 0xe4e8150
movq %rax, (%rcx,%r12,8)
incq %r12
cmpq %r12, %r15
jne 0x1789
movq 0xe4e69bb(%rip), %r15   # 0xe4e8168
testq %r15, %r15
je 0x17e5
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0xe4e6992(%rip), %rax   # 0xe4e8150
movq (%rax,%r12), %rdi
movq 0x8(%r15), %rsi
callq 0x10f0
movl 0x28(%r15), %r14d
cmpl %r14d, %ebp
cmovgl %ebp, %r14d
movq (%r15), %r15
addq $0x8, %r12
movl %r14d, %ebp
testq %r15, %r15
jne 0x17b7
leaq 0x18f5(%rip), %rdi      # 0x30e1
movl %r14d, %esi
xorl %eax, %eax
callq 0x1040
movl $0x7a13b0, %edi         # imm = 0x7A13B0
callq 0x1120
movq %rax, %r14
movq %rax, %rdi
movl $0x1999, %esi           # imm = 0x1999
movl $0x4, %edx
callq 0x1a9c
leaq 0x4d34(%rip), %r15      # 0x6550
xorl %r12d, %r12d
movq %r14, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x1b50
incq %r12
addq $0xc8, %r15
cmpq $0x2710, %r12           # imm = 0x2710
jne 0x181f
leaq 0x18a9(%rip), %rdi      # 0x30f0
xorl %ebp, %ebp
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
movq 0xe4e6947(%rip), %r12   # 0xe4e81a0
xorpd %xmm0, %xmm0
testq %r12, %r12
je 0x1922
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0x30(%rsp), %r15
xorl %ebp, %ebp
xorl %r13d, %r13d
movsd %xmm0, (%rsp)
movq 0x8(%r12), %rsi
movq %r15, %rdi
callq 0x10f0
xorps %xmm0, %xmm0
cvtsi2sdl 0x28(%r12), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movl $0x270f, %edx           # imm = 0x270F
callq 0x1d6c
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 0x17aa(%rip), %xmm1     # 0x3080
movsd 0x8(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
divsd %xmm3, %xmm1
movsd (%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
cmpeqsd %xmm3, %xmm0
movq %xmm0, %rax
subl %eax, %r13d
movq (%r12), %r12
testq %r12, %r12
jne 0x187d
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0x1935
movsd %xmm0, (%rsp)
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x28(%rsp)
xorl %ebx, %ebx
xorps %xmm1, %xmm1
cvtsi2sdl 0x7a13ac(%r14), %xmm1
mulsd 0x16f7(%rip), %xmm1     # 0x3040
movsd 0x16f7(%rip), %xmm0     # 0x3048
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0x179b(%rip), %rdi      # 0x3101
movsd 0x16e2(%rip), %xmm0     # 0x3050
movb $0x1, %al
callq 0x1040
leaq 0x17a1(%rip), %rdi      # 0x311d
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x1040
movsd 0xe4e6817(%rip), %xmm0  # 0xe4e81a8
unpcklps 0x16f8(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1700(%rip), %xmm0     # 0x30a0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
leaq 0x1780(%rip), %rdi      # 0x3139
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
leaq 0x1780(%rip), %rdi      # 0x3149
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
movsd 0x28(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x1775(%rip), %rdi      # 0x315a
movb $0x1, %al
callq 0x1040
leaq 0x1837(%rip), %rbx      # 0x322a
movq %rbx, %rdi
callq 0x11a0
movsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x1764(%rip), %rdi      # 0x3171
movb $0x1, %al
callq 0x1040
movq %rbx, %rdi
callq 0x11a0
movsd 0x8(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x1750(%rip), %rdi      # 0x317f
movb $0x1, %al
callq 0x1040
movq %rbx, %rdi
callq 0x11a0
leaq 0x180c(%rip), %rdi      # 0x3251
callq 0x11a0
leaq 0x1775(%rip), %rdi      # 0x31c6
callq 0x11a0
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r14, %rdi
jmp 0x1a88
jmp 0x1a7b
jmp 0x1a76
movq %rax, %rbx
jmp 0x1a8d
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1a8d
callq 0x1100
movq %rbx, %rdi
callq 0x11d0
nop