// File: /workspace/github_c_makefile_star2_O3/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 $0xc8, %rsp
movq %rsi, %r15
movl %edi, %ebx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
movq $0x0, 0xc0(%rsp)
leaq 0x6eb(%rip), %rdi       # 0x3100
callq 0x1030
testq %rax, %rax
je 0x2a33
leaq 0x6e6(%rip), %rsi       # 0x310c
movq %rax, %rdi
callq 0x1170
movq %rax, %r14
jmp 0x2a36
xorl %r14d, %r14d
movl $0x64, %r12d
movl $0x3e8, %ebp            # imm = 0x3E8
cmpl $0x2, %ebx
jl 0x2a8d
movq 0x8(%r15), %rdi
callq 0x11a0
movl %eax, %r13d
cmpl $0x2, %ebx
je 0x2a93
movq 0x10(%r15), %rdi
callq 0x11a0
movl %eax, %r12d
cmpl $0x4, %ebx
jb 0x2a93
movq 0x18(%r15), %rdi
callq 0x11a0
movl %eax, %ebp
cmpl $0x4, %ebx
je 0x2a93
movq 0x20(%r15), %rdi
leaq 0x68b(%rip), %rsi       # 0x310e
callq 0x1170
movq %rax, %rcx
jmp 0x2a95
movl $0x186a0, %r13d         # imm = 0x186A0
xorl %ecx, %ecx
movl %r13d, %eax
cltd
idivl %r12d
cmpl %ebp, %eax
jge 0x2c6a
movq %rcx, 0x48(%rsp)
movl %r12d, 0x14(%rsp)
movl %ebp, 0x2c(%rsp)
movq %r15, 0x60(%rsp)
movq %r14, 0x50(%rsp)
movslq %r13d, %rdi
shlq $0x3, %rdi
callq 0x1120
movq %rax, %rbx
movl %r13d, %r14d
movl %r13d, 0x18(%rsp)
testl %r13d, %r13d
jle 0x2b85
xorl %ebp, %ebp
leaq 0x62b(%rip), %r12       # 0x3110
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x1c(%rsp), %r8
xorl %eax, %eax
callq 0x1190
movl 0x28(%rsp), %eax
movl 0x24(%rsp), %r15d
shll $0x10, %eax
shll $0x8, %r15d
addl %eax, %r15d
addl 0x20(%rsp), %r15d
movl 0x1c(%rsp), %r13d
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x1c(%rsp), %r8
xorl %eax, %eax
callq 0x1190
movslq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rcx
shlq $0x10, %rax
shlq $0x8, %rcx
addq %rax, %rcx
movslq 0x20(%rsp), %rax
addq %rcx, %rax
shlq $0x8, %rax
movslq 0x1c(%rsp), %rcx
addq %rax, %rcx
shlq $0x28, %r15
shlq $0x20, %r13
addq %r15, %r13
orq %rcx, %r13
movq %r13, (%rbx,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x2ade
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%rsp), %xmm0
movsd 0x472(%rip), %xmm1      # 0x3008
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x80(%rsp), %rbp
xorl %r13d, %r13d
movsd 0x8(%rsp), %xmm0
movsd 0x445(%rip), %xmm1      # 0x3008
callq 0x133d
callq 0x1090
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x11c0
cmpl $0x0, 0x18(%rsp)
jle 0x2bf8
xorl %r12d, %r12d
movq (%rbx,%r12,8), %rdi
callq 0x13b0
incq %r12
cmpq %r12, %r14
jne 0x2be7
callq 0x1090
movq %rax, %r12
movq %rbp, %rdi
callq 0x11c0
subq %r15, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
divsd 0x4b5(%rip), %xmm0      # 0x30d0
movq 0x80(%rsp), %rsi
movq 0x90(%rsp), %rax
movzwl 0x88(%rsp), %edx
movzwl 0x98(%rsp), %ecx
movsd %xmm0, 0xa0(%rsp,%r13,8)
movsd 0x30(%rsp), %xmm2
addsd %xmm0, %xmm2
cmpq $0x4, %r13
je 0x2c7b
movsd %xmm2, 0x30(%rsp)
xorl %eax, %eax
callq 0x1385
incq %r13
jmp 0x2bb5
leaq 0x57c(%rip), %rdi       # 0x31ed
callq 0x1070
jmp 0x2f96
movsd %xmm0, 0x58(%rsp)
movzwl %dx, %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
subq %rax, %rsi
movq %rsi, 0x70(%rsp)
subsd %xmm0, %xmm1
divsd 0x42f(%rip), %xmm1      # 0x30d8
movsd %xmm1, 0x68(%rsp)
divsd 0x429(%rip), %xmm2      # 0x30e0
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq 0x60(%rsp), %r15
movl 0x14(%rsp), %r12d
movsd 0xa0(%rsp,%rax,8), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x5, %rax
jne 0x2cc7
mulsd 0x3fb(%rip), %xmm0      # 0x30e8
movsd 0x31b(%rip), %xmm1      # 0x3010
movsd %xmm2, 0x30(%rsp)
callq 0x1080
mulsd 0x3e8(%rip), %xmm0      # 0x30f0
divsd 0x3e8(%rip), %xmm0      # 0x30f8
movsd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, 0x78(%rsp)
movq %rbx, %rdi
callq 0x1050
leaq 0x3dc(%rip), %rdi       # 0x3119
movl 0x18(%rsp), %r13d
movl %r13d, %esi
movsd 0x30(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movb $0x3, %al
callq 0x10b0
leaq 0x4(%rsp), %r14
movl 0x2c(%rsp), %ebp
movl %ebp, %edi
movq %r14, %rsi
callq 0x142e
movq %rax, 0x8(%rsp)
movl (%r14), %esi
leaq 0x3b6(%rip), %rdi       # 0x3136
xorl %eax, %eax
callq 0x10b0
xorl %eax, %eax
callq 0x1385
movq 0x48(%rsp), %rax
testq %rax, %rax
je 0x2eaa
movq (%r15), %rdx
movl 0x4(%rsp), %r10d
subq $0x8, %rsp
leaq 0x390(%rip), %rsi       # 0x313f
movq %rax, %rdi
movl %r13d, %ecx
movl %r12d, %r8d
movl %ebp, %r9d
movsd 0x60(%rsp), %xmm0
movb $0x1, %al
pushq %r10
callq 0x1110
addq $0x10, %rsp
cmpl $0x0, 0x4(%rsp)
jle 0x2e8d
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %r12
xorl %r13d, %r13d
movl -0x8(%r12), %edx
movq -0x10(%r12), %r10
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, %r15d
shrl $0x8, %r15d
andl %eax, %r15d
andl %eax, %r10d
movl (%r12), %eax
leal (%rax,%rax,4), %eax
leal (%rax,%rax,4), %eax
movq %rax, 0x30(%rsp)
movl -0x4(%r12), %eax
leal (%rax,%rax,4), %eax
leal (%rax,%rax,4), %ebp
subq $0x8, %rsp
movq 0x50(%rsp), %rdi
leaq 0x2f4(%rip), %rsi       # 0x3153
xorl %eax, %eax
pushq %rbp
pushq 0x40(%rsp)
pushq %r10
pushq %r15
pushq %rbx
pushq %r14
pushq %r11
callq 0x1110
addq $0x40, %rsp
incq %r13
movslq 0x4(%rsp), %rax
addq $0x18, %r12
cmpq %rax, %r13
jl 0x2de5
movq 0x48(%rsp), %rdi
callq 0x10a0
movq 0x60(%rsp), %r15
movl 0x2c(%rsp), %ebp
movl 0x14(%rsp), %r12d
movl 0x18(%rsp), %r13d
movslq 0x4(%rsp), %rax
testq %rax, %rax
jle 0x2f13
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movsd 0x1a4(%rip), %xmm1      # 0x3070
xorl %ecx, %ecx
movq 0x50(%rsp), %rbx
movq 0x8(%rsp), %rdi
movl 0xc(%rdi,%rcx), %edx
subl 0x10(%rdi,%rcx), %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
divsd %xmm0, %xmm2
movapd %xmm2, %xmm4
cmplesd %xmm1, %xmm4
movapd %xmm4, %xmm3
andnpd %xmm2, %xmm3
andpd %xmm1, %xmm4
orpd %xmm3, %xmm4
addq $0x18, %rcx
movapd %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x2ed8
jmp 0x2f25
movsd 0x155(%rip), %xmm4      # 0x3070
movq 0x50(%rsp), %rbx
movq 0x8(%rsp), %rdi
movapd %xmm4, 0x30(%rsp)
callq 0x1050
testq %rbx, %rbx
je 0x2f96
xorps %xmm0, %xmm0
cvtsi2sdq 0x70(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
addsd %xmm0, %xmm1
movq (%r15), %rdx
movl 0x4(%rsp), %r10d
subq $0x8, %rsp
leaq 0x219(%rip), %rsi       # 0x3175
movq %rbx, %rdi
movl %r13d, %ecx
movl %r12d, %r8d
movl %ebp, %r9d
movsd 0x60(%rsp), %xmm0
movapd 0x38(%rsp), %xmm2
movb $0x3, %al
pushq %r10
callq 0x1110
addq $0x10, %rsp
movl $0xa, %edi
movq %rbx, %rsi
callq 0x10e0
movq %rbx, %rdi
callq 0x10a0
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq