// File: /workspace/github_c_makefile_star2_O1/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 $0xd8, %rsp
movq %rsi, %r15
movl %edi, %ebx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xb0(%rsp)
movq $0x0, 0xd0(%rsp)
leaq 0x288f(%rip), %rdi      # 0x4068
callq 0x1030
testq %rax, %rax
je 0x17f7
leaq 0x288a(%rip), %rsi      # 0x4074
movq %rax, %rdi
callq 0x1190
movq %rax, %r14
jmp 0x17fa
xorl %r14d, %r14d
movl $0x186a0, %ebp          # imm = 0x186A0
cmpl $0x2, %ebx
jl 0x180f
movq 0x8(%r15), %rdi
callq 0x11c0
movl %eax, %ebp
movl $0x64, %r12d
cmpl $0x3, %ebx
jl 0x1826
movq 0x10(%r15), %rdi
callq 0x11c0
movl %eax, %r12d
movl $0x3e8, %r13d           # imm = 0x3E8
cmpl $0x4, %ebx
jl 0x183d
movq 0x18(%r15), %rdi
callq 0x11c0
movl %eax, %r13d
cmpl $0x5, %ebx
jl 0x1857
movq 0x20(%r15), %rdi
leaq 0x2829(%rip), %rsi      # 0x4076
callq 0x1190
movq %rax, %rcx
jmp 0x1859
xorl %ecx, %ecx
movl %ebp, %eax
cltd
idivl %r12d
cmpl %r13d, %eax
jge 0x1c25
movq %rcx, 0x88(%rsp)
movl %r13d, 0x24(%rsp)
movl %r12d, 0x1c(%rsp)
movq %r15, 0x70(%rsp)
movq %r14, 0x68(%rsp)
movslq %ebp, %rdi
shlq $0x2, %rdi
callq 0x1140
movq %rax, %rbx
movl %ebp, %r14d
movl %ebp, 0x20(%rsp)
testl %ebp, %ebp
jle 0x18f3
leaq 0x48(%rsp), %r12
leaq 0x44(%rsp), %r13
leaq 0x40(%rsp), %rbp
xorl %r15d, %r15d
leaq 0x27c1(%rip), %rdi      # 0x4078
leaq 0x4c(%rsp), %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x11b0
movl 0x4c(%rsp), %eax
movl 0x48(%rsp), %ecx
shll $0x10, %eax
shll $0x8, %ecx
addl %eax, %ecx
addl 0x44(%rsp), %ecx
shll $0x8, %ecx
addl 0x40(%rsp), %ecx
movl %ecx, (%rbx,%r15,4)
incq %r15
cmpq %r15, %r14
jne 0x18b0
xorps %xmm0, %xmm0
cvtsi2sdl 0x1c(%rsp), %xmm0
movsd 0x2704(%rip), %xmm1     # 0x4008
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
movsd %xmm2, 0x50(%rsp)
movsd 0x38(%rsp), %xmm0
movsd 0x26e0(%rip), %xmm1     # 0x4008
callq 0x134d
callq 0x10a0
movq %rax, %r12
leaq 0xa0(%rsp), %rdi
callq 0x11e0
cmpl $0x0, 0x20(%rsp)
jle 0x1962
xorl %r15d, %r15d
movl (%rbx,%r15,4), %edi
movl $0x1, %esi
callq 0x141a
incq %r15
cmpq %r15, %r14
jne 0x194c
callq 0x10a0
movq %rax, %r13
leaq 0x90(%rsp), %rdi
callq 0x11e0
subq %r12, %r13
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
divsd 0x26a6(%rip), %xmm0     # 0x4030
movq 0x90(%rsp), %r13
movq 0xa0(%rsp), %r12
movzwl 0x98(%rsp), %eax
movzwl 0xa8(%rsp), %r15d
movsd %xmm0, 0xb0(%rsp,%rbp,8)
cmpq $0x4, %rbp
je 0x19d7
movw %ax, 0x30(%rsp)
xorl %eax, %eax
movsd %xmm0, 0x28(%rsp)
callq 0x13f2
movzwl 0x30(%rsp), %eax
movsd 0x28(%rsp), %xmm0
movsd 0x50(%rsp), %xmm2
addsd %xmm0, %xmm2
incq %rbp
cmpq $0x5, %rbp
jne 0x1914
movsd %xmm0, 0x28(%rsp)
movzwl %ax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movzwl %r15w, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
subq %r12, %r13
movq %r13, 0x80(%rsp)
subsd %xmm0, %xmm1
divsd 0x2618(%rip), %xmm1     # 0x4038
movsd %xmm1, 0x78(%rsp)
divsd 0x2612(%rip), %xmm2     # 0x4040
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd 0xb0(%rsp,%rax,8), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x5, %rax
jne 0x1a34
mulsd 0x25ee(%rip), %xmm0     # 0x4048
movsd 0x25ae(%rip), %xmm1     # 0x4010
movsd %xmm2, 0x50(%rsp)
callq 0x1090
mulsd 0x25db(%rip), %xmm0     # 0x4050
divsd 0x25db(%rip), %xmm0     # 0x4058
movsd 0x50(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x38(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
movq %rbx, %rdi
callq 0x1050
leaq 0x25d7(%rip), %rdi      # 0x4081
movl 0x20(%rsp), %ebp
movl %ebp, %esi
movsd 0x50(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
movb $0x3, %al
callq 0x10c0
leaq 0x18(%rsp), %r14
movl 0x24(%rsp), %r15d
movl %r15d, %edi
movq %r14, %rsi
movl %ebp, %edx
callq 0x1490
movq %rax, %rbx
movl (%r14), %esi
leaq 0x25af(%rip), %rdi      # 0x409c
xorl %eax, %eax
callq 0x10c0
xorl %eax, %eax
callq 0x13f2
movq 0x88(%rsp), %r12
testq %r12, %r12
movq 0x70(%rsp), %r13
je 0x1bc6
movq (%r13), %rdx
movl 0x18(%rsp), %eax
movl %eax, (%rsp)
leaq 0x2582(%rip), %rsi      # 0x40a5
movq %r12, %rdi
movl %ebp, %ecx
movl 0x1c(%rsp), %r8d
movl %r15d, %r9d
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x1130
cmpl $0x0, 0x18(%rsp)
jle 0x1bb0
leaq 0xc(%rbx), %r15
movl $0xff, %ebp
leaq 0x2565(%rip), %r14      # 0x40b9
xorl %r13d, %r13d
movl -0xc(%r15), %eax
movl -0x8(%r15), %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
movl (%r15), %esi
leal (%rsi,%rsi,4), %esi
movl -0x4(%r15), %edi
leal (%rdi,%rdi,4), %edi
movl %edi, 0x10(%rsp)
movl %esi, 0x8(%rsp)
movl %eax, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1130
incq %r13
movslq 0x18(%rsp), %rax
addq $0x10, %r15
cmpq %rax, %r13
jl 0x1b57
movq %r12, %rdi
callq 0x10b0
movl 0x20(%rsp), %ebp
movl 0x24(%rsp), %r15d
movq 0x70(%rsp), %r13
movslq 0x18(%rsp), %rax
testq %rax, %rax
jle 0x1c33
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
shlq $0x4, %rax
movsd 0x247d(%rip), %xmm1     # 0x4060
xorl %ecx, %ecx
movq 0x68(%rsp), %r14
movl 0x8(%rbx,%rcx), %edx
subl 0xc(%rbx,%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 $0x10, %rcx
movapd %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x1bea
jmp 0x1c40
leaq 0x251c(%rip), %rdi      # 0x4148
callq 0x1070
jmp 0x1cb1
movsd 0x2425(%rip), %xmm4     # 0x4060
movq 0x68(%rsp), %r14
movapd %xmm4, 0x50(%rsp)
movq %rbx, %rdi
callq 0x1050
testq %r14, %r14
je 0x1cb1
xorps %xmm0, %xmm0
cvtsi2sdq 0x80(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
addsd %xmm0, %xmm1
movq (%r13), %rdx
movl 0x18(%rsp), %eax
movl %eax, (%rsp)
leaq 0x2453(%rip), %rsi      # 0x40cf
movq %r14, %rdi
movl %ebp, %ecx
movl 0x1c(%rsp), %r8d
movl %r15d, %r9d
movsd 0x28(%rsp), %xmm0
movapd 0x50(%rsp), %xmm2
movb $0x3, %al
callq 0x1130
movl $0xa, %edi
movq %r14, %rsi
callq 0x1100
movq %r14, %rdi
callq 0x10b0
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)