// File: /workspace/github_c_makefile_star2_O2/ranbenbasat[P]RHHH/randmain1.c
// Function: main
int main(int argc, char * argv[]) {
		int m; //number of heavy hitters in output
		int counters = 100;
		int threshold = 1000;
		int n = 100000;
		double time, walltime;
		double epsil;
		HeavyHitter * ans;
		int i,j;
		unsigned int w, x, y, z;
		clock_t begint, endt;
		struct timeb begintb, endtb;
		unsigned int ip;
		unsigned int * data;
		double insertProb = 1. / VMULT;	
		double measurements[NRRUNS] = {0};
		double t_4 = 2.572; //Change if NRRUNS!=5
		double avg = 0, stdev = 0, upCI = 0, dnCI= 0, sos = 0;
		FILE * fp = NULL; //the file for the output
		FILE * fsummary = NULL;
		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 int *) malloc(sizeof(unsigned int) * n);

		for (i = 0; i < n; i++) {
			scanf("%d%d%d%d", &w, &x, &y, &z);
			ip = (unsigned int)256*((unsigned int)256*((unsigned int)256*w + x) + y) + z;
			data[i] = ip;
		}

		for (j=0; j < NRRUNS; ++j){
			init((double)1/(double)counters, insertProb);
			begint = clock();
			ftime(&begintb);
			for (i = 0; i < n; i++){
				update(data[i], 1);
			}
			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 ips took %fs (%fs-%fs) ", n, avg, dnCI, upCI);

		ans = output(threshold, &m, n);

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


		if (fp != NULL) {
			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\n",
				ans[i].mask,
				(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),
				ans[i].lower * ( (int) (NUM_MASKS / insertProb)), ans[i].upper * ( (int) (NUM_MASKS / insertProb)));				
			}
			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 $0xf8, %rsp
movq %rsi, %r15
movl %edi, %ebx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xe0(%rsp)
movapd %xmm0, 0xd0(%rsp)
andq $0x0, 0xf0(%rsp)
leaq 0x1942(%rip), %rdi      # 0x3070
callq 0x1030
testq %rax, %rax
je 0x174c
leaq 0x193d(%rip), %rsi      # 0x307c
movq %rax, %rdi
callq 0x1180
movq %rax, %r14
jmp 0x174f
xorl %r14d, %r14d
pushq $0x64
popq %rax
movl %eax, 0x18(%rsp)
movl $0x3e8, %ebp            # imm = 0x3E8
cmpl $0x2, %ebx
jl 0x17a8
movq 0x8(%r15), %rdi
callq 0x11b0
movl %eax, %r12d
cmpl $0x2, %ebx
je 0x17ae
movq 0x10(%r15), %rdi
callq 0x11b0
movl %eax, 0x18(%rsp)
cmpl $0x4, %ebx
jb 0x17ae
movq 0x18(%r15), %rdi
callq 0x11b0
movl %eax, %ebp
cmpl $0x4, %ebx
je 0x17ae
movq 0x20(%r15), %rdi
leaq 0x18e0(%rip), %rsi      # 0x307e
callq 0x1180
movq %rax, %rcx
jmp 0x17b0
movl $0x186a0, %r12d         # imm = 0x186A0
xorl %ecx, %ecx
movl %r12d, %eax
cltd
idivl 0x18(%rsp)
cmpl %ebp, %eax
jge 0x184b
movq %rcx, 0x28(%rsp)
movl %ebp, 0x24(%rsp)
movq %r15, 0x58(%rsp)
movq %r14, 0x78(%rsp)
movslq %r12d, %rdi
shlq $0x2, %rdi
callq 0x1130
movq %rax, %rbx
testl %r12d, %r12d
movl $0x0, %r14d
movl %r12d, 0x20(%rsp)
cmovgl %r12d, %r14d
leaq 0x38(%rsp), %rbp
leaq 0x34(%rsp), %r12
leaq 0x30(%rsp), %r13
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x185c
leaq 0x186e(%rip), %rdi      # 0x3080
leaq 0x3c(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x11a0
movl 0x3c(%rsp), %eax
movl 0x38(%rsp), %ecx
shll $0x10, %eax
shll $0x8, %ecx
addl %eax, %ecx
addl 0x34(%rsp), %ecx
shll $0x8, %ecx
addl 0x30(%rsp), %ecx
movl %ecx, (%rbx,%r15,4)
incq %r15
jmp 0x1806
leaq 0x18fe(%rip), %rdi      # 0x3150
callq 0x1070
jmp 0x1bd9
cvtsi2sdl 0x18(%rsp), %xmm0
movsd 0x179e(%rip), %xmm1     # 0x3008
divsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
xorpd %xmm0, %xmm0
pushq $0x1
popq %r15
xorl %ebp, %ebp
cmpq $0x5, %rbp
movapd %xmm0, 0x80(%rsp)
je 0x1967
movsd 0x40(%rsp), %xmm0
movsd 0x1769(%rip), %xmm1     # 0x3008
callq 0x132f
callq 0x10a0
movq %rax, %r12
leaq 0xa0(%rsp), %rdi
callq 0x11d0
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x18d2
movl (%rbx,%r13,4), %edi
movl %r15d, %esi
callq 0x13d6
incq %r13
jmp 0x18bc
callq 0x10a0
movq %rax, %r13
leaq 0x90(%rsp), %rdi
callq 0x11d0
subq %r12, %r13
cvtsi2sd %r13, %xmm0
movq 0x90(%rsp), %r12
subq 0xa0(%rsp), %r12
movzwl 0x98(%rsp), %eax
cvtsi2sd %eax, %xmm2
movzwl 0xa8(%rsp), %eax
cvtsi2sd %eax, %xmm1
subsd %xmm1, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd 0x1739(%rip), %xmm2     # 0x3060
movhpd %xmm2, 0xd0(%rsp,%rbp,8)
cmpq $0x4, %rbp
je 0x1949
xorl %eax, %eax
movapd %xmm2, 0x60(%rsp)
callq 0x13ac
movapd 0x60(%rsp), %xmm2
movapd 0x80(%rsp), %xmm0
cvtsi2sd %r12, %xmm0
addpd %xmm2, %xmm0
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
incq %rbp
jmp 0x187e
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
divsd 0x16b9(%rip), %xmm3     # 0x3030
xorpd %xmm0, %xmm0
xorl %eax, %eax
movl 0x24(%rsp), %ebp
movl 0x20(%rsp), %r12d
cmpq $0x5, %rax
je 0x19a6
movsd 0xd0(%rsp,%rax,8), %xmm1
subsd %xmm3, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x1986
movapd %xmm2, 0x60(%rsp)
mulsd 0x1684(%rip), %xmm0     # 0x3038
movsd 0x1654(%rip), %xmm1     # 0x3010
movapd %xmm3, 0x40(%rsp)
callq 0x1090
mulsd 0x1671(%rip), %xmm0     # 0x3040
divsd 0x1671(%rip), %xmm0     # 0x3048
movapd 0x40(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm0, %xmm2
movapd %xmm2, 0xc0(%rsp)
subsd %xmm0, %xmm1
movapd %xmm1, 0xb0(%rsp)
movq %rbx, %rdi
callq 0x1050
leaq 0x167f(%rip), %rdi      # 0x3089
movl %r12d, %esi
movaps 0x40(%rsp), %xmm0
movapd 0xb0(%rsp), %xmm1
movapd 0xc0(%rsp), %xmm2
movb $0x3, %al
callq 0x10c0
leaq 0x1c(%rsp), %r14
movl %ebp, %edi
movq %r14, %rsi
movl %r12d, %edx
callq 0x1432
movq %rax, %rbx
movl (%r14), %esi
leaq 0x165a(%rip), %rdi      # 0x30a4
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x10c0
xorl %eax, %eax
callq 0x13ac
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x1b19
movq 0x58(%rsp), %rax
movq (%rax), %rdx
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
leaq 0x162e(%rip), %rsi      # 0x30ad
movl %r12d, %ecx
movl 0x18(%rsp), %r8d
movl %ebp, %r9d
movaps 0x60(%rsp), %xmm0
movb $0x1, %al
callq 0x1120
leaq 0xc(%rbx), %r12
movl $0xff, %ebp
leaq 0x161b(%rip), %r14      # 0x30c1
xorl %r13d, %r13d
movslq 0x1c(%rsp), %rax
cmpq %rax, %r13
jge 0x1b06
movl -0xc(%r12), %eax
movl -0x8(%r12), %edx
movl %eax, %ecx
shrl $0x18, %ecx
movl %eax, %r8d
shrl $0x10, %r8d
andl %ebp, %r8d
movl %eax, %r9d
shrl $0x8, %r9d
andl %ebp, %r9d
andl %ebp, %eax
imull $0x5, (%r12), %esi
imull $0x5, -0x4(%r12), %edi
movl %edi, 0x10(%rsp)
movl %esi, 0x8(%rsp)
movl %eax, (%rsp)
movq 0x28(%rsp), %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1120
incq %r13
addq $0x10, %r12
jmp 0x1aa9
movq 0x28(%rsp), %rdi
callq 0x10b0
movl 0x24(%rsp), %ebp
movl 0x20(%rsp), %r12d
movl 0x1c(%rsp), %eax
cvtsi2sd %r12d, %xmm0
testl %eax, %eax
cmovlel %r15d, %eax
shlq $0x4, %rax
movsd 0x151c(%rip), %xmm4     # 0x3050
movq 0x78(%rsp), %r14
cmpq %r15, %rax
je 0x1b73
movl 0x8(%rbx,%r15), %ecx
subl 0xc(%rbx,%r15), %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 $0x10, %r15
movapd %xmm2, %xmm4
jmp 0x1b39
movapd %xmm4, 0x40(%rsp)
movq %rbx, %rdi
callq 0x1050
testq %r14, %r14
je 0x1bd9
movq 0x58(%rsp), %rax
movq (%rax), %rdx
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
leaq 0x153b(%rip), %rsi      # 0x30d7
movq %r14, %rdi
movl %r12d, %ecx
movl 0x18(%rsp), %r8d
movl %ebp, %r9d
movapd 0x60(%rsp), %xmm0
movapd 0x80(%rsp), %xmm1
movapd 0x40(%rsp), %xmm2
movb $0x3, %al
callq 0x1120
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x10f0
movq %r14, %rdi
callq 0x10b0
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)