// File: /workspace/github_cpp_makefile_star2_O2/shuhaosun[P]ECM-Sketch/main.cpp
// Function: main
int main(int argc, char** argv)
{
    if(argc == 2)
    {
        filename_FlowTraffic = argv[1];
    }

	unmp.clear();
    unmp1.clear();
	int val;

	int first = 200000;
	int last = 500000;

    double memory = 1; //MB
    int w = memory * 1024 * 1024 * 8.0 / COUNTER_SIZE;


    printf("\n******************************************************************************\n");
    printf("Evaluation starts!\n\n\n");


    ECM_Sketch *ecm_sketch;
    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 = first; i < last; i++)
    {
    	unmp1[string(insert[i])]++;
    }

    query = new char*[unmp1.size()];
    for(int i = 0; i < unmp1.size(); ++i)
        query[i] = new char[200];

    int k = 0, max_freq = 0;
    for(unordered_map<string, int>::iterator it = unmp1.begin(); it != unmp1.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);


    ecm_sketch = new ECM_Sketch(w / LOW_HASH_NUM, LOW_HASH_NUM);
    for(int i = 0; i < last; i++)
    {
        ecm_sketch->Insert(insert[i], i);
    }


    double re_cm = 0.0;
    double re_cm_sum = 0.0;
    double ae_cm = 0.0;
    double ae_cm_sum = 0.0;

	int val_cm = 0, val_cu = 0, val_oma = 0, val_a = 0;
    int cm_cor_num = 0, cu_cor_num = 0, c_cor_num = 0, oma_cor_num = 0;

    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 = it->second;

		val_cm = ecm_sketch->Query(temp, last - 1);

        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 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\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 0x15f6
movq 0x8(%rsi), %rax
movq %rax, 0x4f12(%rip)      # 0x6508
leaq 0xe4e6b4b(%rip), %rdi   # 0xe4e8148
callq 0x1b6a
leaq 0xe4e6b77(%rip), %rdi   # 0xe4e8180
callq 0x1b6a
leaq 0x1acb(%rip), %rdi      # 0x30e0
callq 0x1190
leaq 0x1b0f(%rip), %rdi      # 0x3130
callq 0x1190
movq 0x4edb(%rip), %rdi      # 0x6508
leaq 0x1a47(%rip), %rsi      # 0x307b
callq 0x1140
movq %rax, %r15
xorl %r14d, %r14d
leaq 0x4efa(%rip), %rbx      # 0x6540
leaq 0x30(%rsp), %r12
leaq 0xf(%rsp), %r13
leaq (%rbx,%r14), %rbp
movq %rbp, %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x11a0
testq %rax, %rax
je 0x16a0
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1080
leaq 0xe4e6aca(%rip), %rdi   # 0xe4e8148
movq %r12, %rsi
callq 0x1eca
incl (%rax)
movq %r12, %rdi
callq 0x10c0
addq $0xc8, %r14
cmpq $0xbebc200, %r14        # imm = 0xBEBC200
jne 0x1650
movq %r15, %rdi
callq 0x1120
xorl %r13d, %r13d
leaq 0x30(%rsp), %r14
leaq 0xf(%rsp), %r15
leaq 0xe4e6ac4(%rip), %r12   # 0xe4e8180
cmpq $0x3938700, %r13        # imm = 0x3938700
je 0x16f9
leaq (%rbx,%r13), %rsi
addq $0x2625a00, %rsi        # imm = 0x2625A00
movq %r14, %rdi
movq %r15, %rdx
callq 0x1080
movq %r12, %rdi
movq %r14, %rsi
callq 0x1eca
incl (%rax)
movq %r14, %rdi
callq 0x10c0
addq $0xc8, %r13
jmp 0x16bc
movq 0xe4e6a98(%rip), %r14   # 0xe4e8198
movq %r14, %rax
shrq $0x3d, %rax
leaq (,%r14,8), %rcx
xorl %r15d, %r15d
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x1030
movq %rax, 0xe4e6a14(%rip)   # 0xe4e8140
cmpq %r15, %r14
je 0x174b
movl $0xc8, %edi
callq 0x1030
movq 0xe4e69fe(%rip), %rcx   # 0xe4e8140
movq %rax, (%rcx,%r15,8)
incq %r15
jmp 0x172c
xorl %r14d, %r14d
leaq 0xe4e6a3b(%rip), %r15   # 0xe4e8190
xorl %ebp, %ebp
movq (%r15), %r15
testq %r15, %r15
je 0x1782
movq 0xe4e69da(%rip), %rax   # 0xe4e8140
movq (%rax,%r14), %rdi
movq 0x8(%r15), %rsi
callq 0x1100
movl 0x28(%r15), %eax
cmpl %eax, %ebp
cmovlel %eax, %ebp
addq $0x8, %r14
jmp 0x1757
leaq 0x18f4(%rip), %rdi      # 0x307d
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x1130
movq %rax, %r14
pushq $0x4
popq %rdx
movq %rax, %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0x1992
xorl %r15d, %r15d
cmpq $0x7a120, %r15          # imm = 0x7A120
je 0x17d5
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x1a3c
incq %r15
addq $0xc8, %rbx
jmp 0x17b2
movq %r14, 0x20(%rsp)
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xe4e69a2(%rip), %r13   # 0xe4e8190
movsd %xmm0, 0x18(%rsp)
xorl %ebp, %ebp
xorl %ebx, %ebx
movq (%r13), %r13
testq %r13, %r13
je 0x187c
movq 0x8(%r13), %rsi
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x1100
movl 0x28(%r13), %r15d
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movl $0x7a11f, %edx          # imm = 0x7A11F
callq 0x1abe
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
subl %r15d, %eax
setl %cl
setg %dl
sete %sil
addl %ecx, %ebp
cvtsi2sd %eax, %xmm0
addl %edx, %ebx
andpd 0x17f5(%rip), %xmm0     # 0x3040
cvtsi2sd %r15d, %xmm1
movsd 0x18(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
divsd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
addl %esi, %r12d
jmp 0x17f8
movsd 0xe4e6914(%rip), %xmm0  # 0xe4e8198
unpcklps 0x17c5(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x17cd(%rip), %xmm0     # 0x3060
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
leaq 0x17e0(%rip), %rdi      # 0x308c
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
leaq 0x17e0(%rip), %rdi      # 0x309c
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
cvtsi2sd %r12d, %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x17d6(%rip), %rdi      # 0x30ad
movb $0x1, %al
callq 0x1040
leaq 0x1860(%rip), %rbx      # 0x3145
movq %rbx, %rdi
callq 0x1190
movsd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x17c4(%rip), %rdi      # 0x30c4
movb $0x1, %al
callq 0x1040
movq %rbx, %rdi
callq 0x1190
movsd 0x18(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x17b0(%rip), %rdi      # 0x30d2
movb $0x1, %al
callq 0x1040
leaq 0x183c(%rip), %rdi      # 0x316c
callq 0x1190
leaq 0x1858(%rip), %rdi      # 0x3194
callq 0x1190
leaq 0x185d(%rip), %rdi      # 0x31a5
callq 0x1190
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
callq 0x1110
jmp 0x1984
jmp 0x1977
jmp 0x1972
movq %rax, %rbx
jmp 0x1984
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x10c0
movq %rbx, %rdi
callq 0x11c0