// File: /workspace/github_cpp_makefile_star2_O1/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 0x15f2
movq 0x8(%rsi), %rax
movq %rax, 0x4f16(%rip)      # 0x6508
leaq 0xe4e6b4f(%rip), %rbx   # 0xe4e8148
movq %rbx, %rdi
callq 0x1bee
leaq 0xe4e6b78(%rip), %rdi   # 0xe4e8180
callq 0x1bee
leaq 0x1af6(%rip), %rdi      # 0x310a
callq 0x1180
leaq 0x1b3a(%rip), %rdi      # 0x315a
callq 0x1180
movq 0x4edc(%rip), %rdi      # 0x6508
leaq 0x1a48(%rip), %rsi      # 0x307b
callq 0x1140
movq %rax, %r14
leaq 0x4efe(%rip), %r15      # 0x6540
movl $0xf4240, %ebp          # imm = 0xF4240
leaq 0x40(%rsp), %r13
leaq 0x30(%rsp), %r12
movq %r15, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x1190
testq %rax, %rax
je 0x16aa
movq %r13, 0x30(%rsp)
movq %r15, %rdi
callq 0x1090
leaq (%rax,%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x1f86
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2016
incl (%rax)
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x169e
callq 0x1100
addq $0xc8, %r15
decq %rbp
jne 0x1651
movq %r14, %rdi
callq 0x1110
leaq 0x4e87(%rip), %rbx      # 0x6540
leaq 0x2625a00(%rbx), %r14
movl $0x493e0, %ebp          # imm = 0x493E0
leaq 0x30(%rsp), %r15
leaq 0xe4e6aaf(%rip), %r12   # 0xe4e8180
movq %r13, 0x30(%rsp)
movq %r14, %rdi
callq 0x1090
leaq (%rax,%r14), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1f86
movq %r12, %rdi
movq %r15, %rsi
callq 0x2016
incl (%rax)
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x1709
callq 0x1100
addq $0xc8, %r14
decq %rbp
jne 0x16d1
movq 0xe4e6a7c(%rip), %r15   # 0xe4e8198
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, 0xe4e69f8(%rip)   # 0xe4e8140
testq %r15, %r15
je 0x176d
xorl %r12d, %r12d
movl $0xc8, %edi
callq 0x1030
movq 0xe4e69df(%rip), %rcx   # 0xe4e8140
movq %rax, (%rcx,%r12,8)
incq %r12
cmpq %r12, %r15
jne 0x1750
movq 0xe4e6a1c(%rip), %r15   # 0xe4e8190
testq %r15, %r15
je 0x17ac
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0xe4e69bb(%rip), %rax   # 0xe4e8140
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 0x177e
leaq 0x18ca(%rip), %rdi      # 0x307d
movl %r14d, %esi
xorl %eax, %eax
callq 0x1040
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x1120
movq %rax, %r14
movq %rax, %rdi
movl $0x20000, %esi          # imm = 0x20000
movl $0x4, %edx
callq 0x19f4
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x1ab0
incq %r15
addq $0xc8, %rbx
cmpq $0x7a120, %r15          # imm = 0x7A120
jne 0x17df
movq 0xe4e6989(%rip), %r12   # 0xe4e8190
testq %r12, %r12
je 0x18c4
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r13d, %r13d
movsd %xmm0, (%rsp)
movq %r14, 0x10(%rsp)
movq 0x8(%r12), %rsi
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x10f0
movl 0x28(%r12), %r15d
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movl $0x7a11f, %edx          # imm = 0x7A11F
callq 0x1b3a
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
subl %r15d, %eax
setl %cl
setg %dl
sete %sil
addl %ecx, %ebp
addl %edx, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
andpd 0x17c6(%rip), %xmm0     # 0x3040
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
divsd %xmm1, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
addl %esi, %r13d
movq (%r12), %r12
testq %r12, %r12
jne 0x182b
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0x18dd
xorl %ebp, %ebp
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x28(%rsp)
xorl %ebx, %ebx
movsd 0xe4e68b3(%rip), %xmm0  # 0xe4e8198
unpcklps 0x1764(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x176c(%rip), %xmm0     # 0x3060
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
leaq 0x177f(%rip), %rdi      # 0x308c
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
leaq 0x177f(%rip), %rdi      # 0x309c
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
movsd 0x28(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x1774(%rip), %rdi      # 0x30ad
movb $0x1, %al
callq 0x1040
leaq 0x1828(%rip), %rbx      # 0x316f
movq %rbx, %rdi
callq 0x1180
movsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x1763(%rip), %rdi      # 0x30c4
movb $0x1, %al
callq 0x1040
movq %rbx, %rdi
callq 0x1180
movsd 0x8(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x174f(%rip), %rdi      # 0x30d2
movb $0x1, %al
callq 0x1040
leaq 0x1805(%rip), %rdi      # 0x3196
callq 0x1180
leaq 0x1821(%rip), %rdi      # 0x31be
callq 0x1180
leaq 0x1826(%rip), %rdi      # 0x31cf
callq 0x1180
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 0x19e0
jmp 0x19d3
jmp 0x19ce
movq %rax, %rbx
jmp 0x19e5
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x19e5
callq 0x1100
movq %rbx, %rdi
callq 0x11b0
nop