// File: /workspace/github_c_makefile_star2_O2/ranbenbasat[P]RHHH/main2.c
// Function: main
int main(int argc, char * argv[]) {
		int m; //number of heavy hitters in output
		//scanf("%d%d%d", &counters, &threshold, &n);
		int counters = 100;
		int threshold = 1000;
		int n = 100000;
		double time, walltime;
		double epsil;
		HeavyHitter * ans;
		int i,j;
		int w, x, y, z;
		clock_t begint, endt;
		struct timeb begintb, endtb;
		unsigned long long ip, _ip;
		unsigned long long * data;
		double measurements[NRRUNS] = {0};
		double avg = 0, stdev = 0, upCI = 0, dnCI= 0, sos = 0;		
		double t_4 = 2.572; //Change if NRRUNS!=5
		FILE * fp = NULL;
		FILE * fsummary = NULL; //summary file	
		double insertProb = 1. / VMULT;	
		if (getenv("SUMMARYFILE") != NULL) fsummary = fopen(getenv("SUMMARYFILE"), "a");

		if (argc > 1) n = atoi(argv[1]);
		if (argc > 2) counters = atoi(argv[2]);
		if (argc > 3) threshold = atoi(argv[3]);
		if (argc > 4) fp = fopen(argv[4], "w");

		if(n/counters >= threshold) {
			printf("Unacceptable parameters: eps*n >= theshold\n");
			return 0;
		}

		

		data = (unsigned long long *) malloc(sizeof(unsigned long long) * n);

		for (i = 0; i < n; i++) {
			scanf("%d%d%d%d", &w, &x, &y, &z);
			ip = (unsigned long long)256*((unsigned long long)256*((unsigned long long)256*w + x) + y) + z;
			scanf("%d%d%d%d", &w, &x, &y, &z);
			_ip = (unsigned long long)256*((unsigned long long)256*((unsigned long long)256*w + x) + y) + z;
			data[i] = (ip << 32 | _ip);			
		}	
		for (j=0; j < NRRUNS; ++j){
			//printf("j = %d\n", j);
#ifdef RANDHHH
		init((double)1/(double)counters, insertProb);
#else
		init((double)1/(double)counters);
#endif
			begint = clock();
			ftime(&begintb);
			for (i = 0; i < n; i++) {
				update(data[i]);
			}
			endt = clock();
			ftime(&endtb);

			time = ((double)(endt-begint))/CLK_PER_SEC;
			walltime = ((double) (endtb.time-begintb.time))+((double)endtb.millitm-(double)begintb.millitm)/1000;
			measurements[j] = time;
			avg += time;
			if (j < NRRUNS - 1)
				deinit();
		}
		
		avg /= NRRUNS;
		
		for (j=0; j < NRRUNS; ++j){
			sos += pow(measurements[j] - avg, 2);
		}
		stdev = pow(sos / (NRRUNS - 1), 0.5);
		
		upCI = avg + stdev * t_4 / pow(NRRUNS, 0.5) ;
		dnCI = avg - stdev * t_4 / pow(NRRUNS, 0.5) ;

		free(data);

		printf("%d pairs took %fs (%fs-%fs) ", n, avg, dnCI, upCI);
		ans = output2(threshold, &m);

		printf("%d HHHs\n", m);

		deinit();

		if (fp != NULL) {
			//fprintf(fp, "%d\n", m);
			fprintf(fp, "%s %d %d %d %d %lf\n", argv[0], n, counters, threshold, m, time);
			for (i = 0; i < m; i++) {
				fprintf(fp, "%d %d.%d.%d.%d %d.%d.%d.%d %d %d\n",
				ans[i].mask,
				(int)((ans[i].item >> 56) & (LCLitem_t)255),
				(int)((ans[i].item >> 48) & (LCLitem_t)255),
				(int)((ans[i].item >> 40) & (LCLitem_t)255),
				(int)((ans[i].item >> 32) & (LCLitem_t)255),
				(int)((ans[i].item >> 24)& (LCLitem_t)255),
				(int)((ans[i].item >> 16) & (LCLitem_t)255),
				(int)((ans[i].item >> 8) & (LCLitem_t)255),
				(int)((ans[i].item >> 0) & (LCLitem_t)255),
#ifdef RANDHHH				
				ans[i].lower * ( (int) (NUM_COUNTERS / insertProb)), ans[i].upper * ( (int) (NUM_COUNTERS / insertProb)));
#else
				ans[i].lower, ans[i].upper);
#endif		
			}
			fclose(fp);
		}

		epsil = -1;
		for (i = 0; i < m; i++) {
			epsil = dblmainmax(epsil, ((double)(ans[i].upper-ans[i].lower))/n);
		}

		free(ans);

		if (fsummary != NULL) {
			fprintf(fsummary, "check=false algorithm=%-14s nitems=%-10d counters=%-5d threshold=%-9d outputsize=%-3d time=%lf walltime=%lf epsilon=%lf",
		                           argv[0],        n,           counters,     threshold,     m,              time, walltime,   epsil);
			fprintf(fsummary, "\n");
			fclose(fsummary);       
		}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, 0x30(%rsp)
movl %edi, %ebx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xb0(%rsp)
andq $0x0, 0xd0(%rsp)
leaq 0xa30(%rip), %rdi       # 0x30d0
callq 0x1030
testq %rax, %rax
je 0x26bb
leaq 0xa2b(%rip), %rsi       # 0x30dc
movq %rax, %rdi
callq 0x1170
jmp 0x26bd
xorl %eax, %eax
movq %rax, 0x68(%rsp)
pushq $0x64
popq %rax
movl %eax, 0x4(%rsp)
movl $0x3e8, (%rsp)          # imm = 0x3E8
cmpl $0x2, %ebx
movq 0x30(%rsp), %r14
jl 0x2722
movq 0x8(%r14), %rdi
callq 0x11a0
movl %eax, %ebp
cmpl $0x2, %ebx
je 0x2727
movq 0x10(%r14), %rdi
callq 0x11a0
movl %eax, 0x4(%rsp)
cmpl $0x4, %ebx
jb 0x2727
movq 0x18(%r14), %rdi
callq 0x11a0
movl %eax, (%rsp)
cmpl $0x4, %ebx
je 0x2727
movq 0x20(%r14), %rdi
leaq 0x9c6(%rip), %rsi       # 0x30de
callq 0x1170
movq %rax, %rcx
jmp 0x2729
movl $0x186a0, %ebp          # imm = 0x186A0
xorl %ecx, %ecx
movl %ebp, %eax
cltd
idivl 0x4(%rsp)
cmpl (%rsp), %eax
jge 0x280b
movq %rcx, 0x38(%rsp)
movslq %ebp, %rdi
shlq $0x3, %rdi
callq 0x1120
movq %rax, %rbx
testl %ebp, %ebp
movl $0x0, %r14d
movl %ebp, 0xc(%rsp)
cmovgl %ebp, %r14d
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x281c
leaq 0x971(%rip), %r13       # 0x30e0
movq %r13, %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x1190
movl 0x1c(%rsp), %eax
movl 0x18(%rsp), %r15d
shll $0x10, %eax
shll $0x8, %r15d
addl %eax, %r15d
addl 0x14(%rsp), %r15d
movl 0x10(%rsp), %r12d
movq %r13, %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x1190
movslq 0x1c(%rsp), %rax
movslq 0x18(%rsp), %rcx
shlq $0x10, %rax
shlq $0x8, %rcx
addq %rax, %rcx
movslq 0x14(%rsp), %rax
addq %rcx, %rax
shlq $0x8, %rax
movslq 0x10(%rsp), %rcx
addq %rax, %rcx
shlq $0x28, %r15
shlq $0x20, %r12
addq %r15, %r12
orq %rcx, %r12
movq %r12, (%rbx,%rbp,8)
incq %rbp
jmp 0x275f
leaq 0x9ab(%rip), %rdi       # 0x31bd
callq 0x1070
jmp 0x2bd3
cvtsi2sdl 0x4(%rsp), %xmm0
movsd 0x7de(%rip), %xmm1      # 0x3008
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
xorpd %xmm0, %xmm0
leaq 0x80(%rsp), %rbp
xorl %r13d, %r13d
cmpq $0x5, %r13
movapd %xmm0, 0x70(%rsp)
je 0x291f
movsd 0x20(%rsp), %xmm0
movsd 0x7a7(%rip), %xmm1      # 0x3008
callq 0x132f
callq 0x1090
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x11c0
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x2891
movq (%rbx,%r12,8), %rdi
callq 0x13a3
incq %r12
jmp 0x287e
callq 0x1090
movq %rax, %r12
movq %rbp, %rdi
callq 0x11c0
subq %r15, %r12
cvtsi2sd %r12, %xmm0
movq 0x80(%rsp), %r15
subq 0x90(%rsp), %r15
movzwl 0x88(%rsp), %eax
cvtsi2sd %eax, %xmm2
movzwl 0x98(%rsp), %eax
cvtsi2sd %eax, %xmm1
subsd %xmm1, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd 0x7df(%rip), %xmm2      # 0x30c0
movhpd %xmm2, 0xb0(%rsp,%r13,8)
cmpq $0x4, %r13
je 0x2904
xorl %eax, %eax
movapd %xmm2, 0x40(%rsp)
callq 0x1376
movapd 0x40(%rsp), %xmm2
movapd 0x70(%rsp), %xmm0
cvtsi2sd %r15, %xmm0
addpd %xmm2, %xmm0
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
incq %r13
jmp 0x2843
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
divsd 0x771(%rip), %xmm3      # 0x30a0
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x5, %rax
je 0x2955
movsd 0xb0(%rsp,%rax,8), %xmm1
subsd %xmm3, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x2935
movapd %xmm2, 0x40(%rsp)
mulsd 0x745(%rip), %xmm0      # 0x30a8
movsd 0x6a5(%rip), %xmm1      # 0x3010
movapd %xmm3, 0x20(%rsp)
callq 0x1080
mulsd 0x732(%rip), %xmm0      # 0x30b0
divsd 0x732(%rip), %xmm0      # 0x30b8
movapd 0x20(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm0, %xmm2
movapd %xmm2, 0x50(%rsp)
subsd %xmm0, %xmm1
movapd %xmm1, 0xa0(%rsp)
movq %rbx, %rdi
callq 0x1050
leaq 0x733(%rip), %rdi       # 0x30e9
movl 0xc(%rsp), %ebx
movl %ebx, %esi
movaps 0x20(%rsp), %xmm0
movapd 0xa0(%rsp), %xmm1
movapd 0x50(%rsp), %xmm2
movb $0x3, %al
callq 0x10b0
leaq 0x8(%rsp), %r14
movl (%rsp), %edi
movq %r14, %rsi
callq 0x1404
movq %rax, %r15
movl (%r14), %esi
leaq 0x712(%rip), %rdi       # 0x3106
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x10b0
xorl %eax, %eax
callq 0x1376
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x2b0e
movq 0x30(%rsp), %rax
movq (%rax), %rdx
movl 0x8(%rsp), %r10d
subq $0x8, %rsp
leaq 0x6e4(%rip), %rsi       # 0x310f
movl %ebx, %ecx
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
movaps 0x48(%rsp), %xmm0
movb $0x1, %al
pushq %r10
callq 0x1110
addq $0x10, %rsp
movq %r15, 0x50(%rsp)
leaq 0x10(%r15), %r12
xorl %ecx, %ecx
movslq 0x8(%rsp), %rax
cmpq %rax, %rcx
jge 0x2af8
movl -0x8(%r12), %edx
movq -0x10(%r12), %r10
movq %rcx, 0x20(%rsp)
movq %r10, %rcx
shrq $0x38, %rcx
movq %r10, %r8
shrq $0x30, %r8
movl $0xff, %eax
andl %eax, %r8d
movq %r10, %r9
shrq $0x28, %r9
andl %eax, %r9d
movq %r10, %r11
shrq $0x20, %r11
andl %eax, %r11d
movl %r10d, %r14d
shrl $0x18, %r14d
movl %r10d, %ebx
shrl $0x10, %ebx
andl %eax, %ebx
movl %r10d, %ebp
shrl $0x8, %ebp
andl %eax, %ebp
andl %eax, %r10d
imull $0x19, (%r12), %r13d
imull $0x19, -0x4(%r12), %r15d
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
leaq 0x653(%rip), %rsi       # 0x3123
xorl %eax, %eax
pushq %r15
pushq %r13
pushq %r10
pushq %rbp
pushq %rbx
pushq %r14
pushq %r11
callq 0x1110
movq 0x60(%rsp), %rcx
addq $0x40, %rsp
incq %rcx
addq $0x18, %r12
jmp 0x2a54
movq 0x38(%rsp), %rdi
callq 0x10a0
movl 0xc(%rsp), %ebx
movq 0x50(%rsp), %r15
xorl %r14d, %r14d
movl 0x8(%rsp), %eax
cvtsi2sd %ebx, %xmm0
testl %eax, %eax
cmovlel %r14d, %eax
imulq $0x18, %rax, %rax
movsd 0x518(%rip), %xmm4      # 0x3040
cmpq %r14, %rax
je 0x2b62
movl 0xc(%r15,%r14), %ecx
subl 0x10(%r15,%r14), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm2
cmplesd %xmm4, %xmm2
movapd %xmm2, %xmm3
andnpd %xmm1, %xmm3
andpd %xmm4, %xmm2
orpd %xmm3, %xmm2
addq $0x18, %r14
movapd %xmm2, %xmm4
jmp 0x2b28
movapd %xmm4, 0x20(%rsp)
movq %r15, %rdi
callq 0x1050
movq 0x68(%rsp), %rbx
testq %rbx, %rbx
je 0x2bd3
movq 0x30(%rsp), %rax
movq (%rax), %rdx
movl 0x8(%rsp), %r10d
subq $0x8, %rsp
leaq 0x5b3(%rip), %rsi       # 0x3145
movq %rbx, %rdi
movl 0x14(%rsp), %ecx
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
movapd 0x48(%rsp), %xmm0
movapd 0x78(%rsp), %xmm1
movapd 0x28(%rsp), %xmm2
movb $0x3, %al
pushq %r10
callq 0x1110
popq %rcx
popq %rdx
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x10e0
movq %rbx, %rdi
callq 0x10a0
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq