// File: 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:
subq $0x278, %rsp            # imm = 0x278
movl $0x0, 0x274(%rsp)
movl %edi, 0x270(%rsp)
movq %rsi, 0x268(%rsp)
cmpl $0x2, 0x270(%rsp)
jne 0x29ae
movq 0x268(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x7b7a(%rip)      # 0xa528
leaq 0xe4e97b3(%rip), %rdi   # 0xe4ec168
callq 0x32e0
leaq 0xe4e97df(%rip), %rdi   # 0xe4ec1a0
callq 0x32e0
movl $0x30d40, 0x260(%rsp) # imm = 0x30D40
movl $0x7a120, 0x25c(%rsp) # imm = 0x7A120
movsd 0x366c(%rip), %xmm0     # 0x6050
movsd %xmm0, 0x250(%rsp)
movsd 0x3653(%rip), %xmm0     # 0x6048
mulsd 0x250(%rsp), %xmm0
movsd 0x3642(%rip), %xmm1     # 0x6048
mulsd %xmm1, %xmm0
movsd 0x362e(%rip), %xmm1     # 0x6040
mulsd %xmm1, %xmm0
movsd 0x361a(%rip), %xmm1     # 0x6038
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x24c(%rsp)
leaq 0x366b(%rip), %rdi      # 0x609f
movb $0x0, %al
callq 0x2040
leaq 0x36ae(%rip), %rdi      # 0x60f0
movb $0x0, %al
callq 0x2040
movl $0x0, 0x23c(%rsp)
movq 0x7acd(%rip), %rdi      # 0xa528
leaq 0x36a4(%rip), %rsi      # 0x6106
callq 0x2170
movq %rax, 0x230(%rsp)
movslq 0x23c(%rsp), %rax
leaq 0x7ae2(%rip), %rdi      # 0xa560
imulq $0xc8, %rax, %rax
addq %rax, %rdi
movq 0x230(%rsp), %rdx
movl $0xc8, %esi
callq 0x21e0
cmpq $0x0, %rax
je 0x2b9c
movslq 0x23c(%rsp), %rax
imulq $0xc8, %rax, %rcx
leaq 0x7aa6(%rip), %rax      # 0xa560
addq %rcx, %rax
movq %rax, 0x50(%rsp)
leaq 0x20f(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x2210
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x210(%rsp), %rdi
callq 0x21a0
jmp 0x2aed
leaq 0xe4e9674(%rip), %rdi   # 0xe4ec168
leaq 0x210(%rsp), %rsi
callq 0x32f0
movq %rax, 0x48(%rsp)
jmp 0x2b08
movq 0x48(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq 0x210(%rsp), %rdi
callq 0x20e0
leaq 0x20f(%rsp), %rdi
callq 0x2160
movl 0x23c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x23c(%rsp)
cmpl $0xf4240, 0x23c(%rsp) # imm = 0xF4240
jne 0x2b97
jmp 0x2b9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
jmp 0x2b85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
leaq 0x210(%rsp), %rdi
callq 0x20e0
leaq 0x20f(%rsp), %rdi
callq 0x2160
jmp 0x32ae
jmp 0x2a6f
movq 0x230(%rsp), %rdi
callq 0x2140
movl 0x260(%rsp), %eax
movl %eax, 0x1f8(%rsp)
movl 0x1f8(%rsp), %eax
cmpl 0x25c(%rsp), %eax
jge 0x2cb4
movslq 0x1f8(%rsp), %rax
imulq $0xc8, %rax, %rcx
leaq 0x797f(%rip), %rax      # 0xa560
addq %rcx, %rax
movq %rax, 0x38(%rsp)
leaq 0x1d7(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x2210
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0x1d8(%rsp), %rdi
callq 0x21a0
jmp 0x2c14
leaq 0xe4e9585(%rip), %rdi   # 0xe4ec1a0
leaq 0x1d8(%rsp), %rsi
callq 0x32f0
movq %rax, 0x30(%rsp)
jmp 0x2c2f
movq 0x30(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq 0x1d8(%rsp), %rdi
callq 0x20e0
leaq 0x1d7(%rsp), %rdi
callq 0x2160
movl 0x1f8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1f8(%rsp)
jmp 0x2bb7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
jmp 0x2ca2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
leaq 0x1d8(%rsp), %rdi
callq 0x20e0
leaq 0x1d7(%rsp), %rdi
callq 0x2160
jmp 0x32ae
leaq 0xe4e94e5(%rip), %rdi   # 0xe4ec1a0
callq 0x3320
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, 0xe4e9478(%rip)   # 0xe4ec160
movl $0x0, 0x1d0(%rsp)
movslq 0x1d0(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xe4e9499(%rip), %rdi   # 0xe4ec1a0
callq 0x3320
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x2d4c
movl $0xc8, %edi
callq 0x2030
movq %rax, %rdx
movq 0xe4e9433(%rip), %rax   # 0xe4ec160
movslq 0x1d0(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x1d0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1d0(%rsp)
jmp 0x2cf3
movl $0x0, 0x1cc(%rsp)
movl $0x0, 0x1c8(%rsp)
leaq 0xe4e9437(%rip), %rdi   # 0xe4ec1a0
callq 0x3330
movq %rax, 0x1c0(%rsp)
leaq 0xe4e9423(%rip), %rdi   # 0xe4ec1a0
callq 0x3380
movq %rax, 0x1b8(%rsp)
leaq 0x1c0(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x3360
testb $0x1, %al
jne 0x2da8
jmp 0x2e59
movq 0xe4e93b1(%rip), %rax   # 0xe4ec160
movslq 0x1cc(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x33b0
movq %rax, %rdi
callq 0x2090
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x2120
leaq 0x1c0(%rsp), %rdi
callq 0x33b0
movl 0x20(%rax), %eax
movl %eax, 0x1b4(%rsp)
movl 0x1c8(%rsp), %eax
cmpl 0x1b4(%rsp), %eax
jle 0x2e16
movl 0x1c8(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x2e21
movl 0x1b4(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x1c8(%rsp)
leaq 0x1c0(%rsp), %rdi
xorl %esi, %esi
callq 0x33d0
movq %rax, 0x1a8(%rsp)
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
jmp 0x2d76
movl 0x1c8(%rsp), %esi
leaq 0x32a1(%rip), %rdi      # 0x6108
xorl %eax, %eax
callq 0x2040
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x2150
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movl 0x24c(%rsp), %esi
movl %esi, %eax
addl $0x3, %eax
testl %esi, %esi
cmovsl %eax, %esi
sarl $0x2, %esi
movl $0x4, %edx
callq 0x3400
jmp 0x2ea3
movq 0x10(%rsp), %rax
movq %rax, 0x240(%rsp)
movl $0x0, 0x1a4(%rsp)
movl 0x1a4(%rsp), %eax
cmpl 0x25c(%rsp), %eax
jge 0x2f2e
movq 0x240(%rsp), %rdi
movslq 0x1a4(%rsp), %rax
leaq 0x767e(%rip), %rsi      # 0xa560
imulq $0xc8, %rax, %rax
addq %rax, %rsi
movl 0x1a4(%rsp), %edx
callq 0x3570
movl 0x1a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a4(%rsp)
jmp 0x2ebb
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
callq 0x2130
jmp 0x32ae
xorps %xmm0, %xmm0
movsd %xmm0, 0x198(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x190(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x188(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x180(%rsp)
movl $0x0, 0x17c(%rsp)
movl $0x0, 0x178(%rsp)
movl $0x0, 0x174(%rsp)
movl $0x0, 0x170(%rsp)
movl $0x0, 0x16c(%rsp)
movl $0x0, 0x168(%rsp)
movl $0x0, 0x164(%rsp)
movl $0x0, 0x160(%rsp)
movl $0x0, 0x8c(%rsp)
movl $0x0, 0x88(%rsp)
leaq 0xe4e91cd(%rip), %rdi   # 0xe4ec1a0
callq 0x3330
movq %rax, 0x80(%rsp)
leaq 0xe4e91b9(%rip), %rdi   # 0xe4ec1a0
callq 0x3380
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x3360
testb $0x1, %al
jne 0x300c
jmp 0x317d
leaq 0x90(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x33b0
movq %rax, %rdi
callq 0x2090
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x2120
leaq 0x80(%rsp), %rdi
callq 0x33b0
movl 0x20(%rax), %eax
movl %eax, 0x264(%rsp)
movq 0x240(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movl 0x25c(%rsp), %edx
subl $0x1, %edx
callq 0x3660
movl %eax, 0x17c(%rsp)
movl 0x17c(%rsp), %eax
cmpl 0x264(%rsp), %eax
jge 0x3099
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
movl 0x17c(%rsp), %eax
cmpl 0x264(%rsp), %eax
jle 0x30ba
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
movl 0x17c(%rsp), %edi
subl 0x264(%rsp), %edi
callq 0x3770
cvtsi2sdl 0x264(%rsp), %xmm1
movsd 0x2f72(%rip), %xmm2     # 0x6050
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x198(%rsp)
movl 0x17c(%rsp), %edi
subl 0x264(%rsp), %edi
callq 0x3770
movsd %xmm0, 0x188(%rsp)
movsd 0x198(%rsp), %xmm0
addsd 0x190(%rsp), %xmm0
movsd %xmm0, 0x190(%rsp)
movsd 0x188(%rsp), %xmm0
addsd 0x180(%rsp), %xmm0
movsd %xmm0, 0x180(%rsp)
movl 0x264(%rsp), %eax
cmpl 0x17c(%rsp), %eax
jne 0x3162
movl 0x16c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x16c(%rsp)
jmp 0x3164
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x33d0
movq %rax, 0x70(%rsp)
jmp 0x2fe0
leaq 0xe4e901c(%rip), %rdi   # 0xe4ec1a0
callq 0x3320
movq %rax, %xmm0
movaps 0x2ecb(%rip), %xmm1     # 0x6060
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x2ecf(%rip), %xmm1     # 0x6070
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x2e98(%rip), %xmm1     # 0x6050
mulsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
cvtsi2sdl 0x23c(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movl 0x8c(%rsp), %esi
leaq 0x2f38(%rip), %rdi      # 0x6117
movb $0x0, %al
callq 0x2040
movl 0x88(%rsp), %esi
leaq 0x2f33(%rip), %rdi      # 0x6127
movb $0x0, %al
callq 0x2040
cvtsi2sdl 0x16c(%rsp), %xmm0
movsd 0x2e44(%rip), %xmm1     # 0x6050
mulsd %xmm1, %xmm0
divsd 0x68(%rsp), %xmm0
leaq 0x2f1b(%rip), %rdi      # 0x6138
movb $0x1, %al
callq 0x2040
leaq 0x2f24(%rip), %rdi      # 0x614f
movb $0x0, %al
callq 0x2040
movsd 0x190(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
leaq 0x2f2f(%rip), %rdi      # 0x6177
movb $0x1, %al
callq 0x2040
leaq 0x2ef9(%rip), %rdi      # 0x614f
movb $0x0, %al
callq 0x2040
movsd 0x180(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
leaq 0x2f12(%rip), %rdi      # 0x6185
movb $0x1, %al
callq 0x2040
leaq 0x2f12(%rip), %rdi      # 0x6193
movb $0x0, %al
callq 0x2040
leaq 0x2f2d(%rip), %rdi      # 0x61bc
movb $0x0, %al
callq 0x2040
leaq 0x2f31(%rip), %rdi      # 0x61ce
movb $0x0, %al
callq 0x2040
xorl %eax, %eax
addq $0x278, %rsp            # imm = 0x278
retq
movq 0x200(%rsp), %rdi
callq 0x2200
nopl (%rax,%rax)