// File: /workspace/github_c_makefile_star2_O3/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 $0xc8, %rsp
movq %rsi, %r15
movl %edi, %ebx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
movq $0x0, 0xc0(%rsp)
leaq 0x18bb(%rip), %rdi      # 0x3068
callq 0x1030
testq %rax, %rax
je 0x17cb
leaq 0x18b6(%rip), %rsi      # 0x3074
movq %rax, %rdi
callq 0x1180
movq %rax, %r14
jmp 0x17ce
xorl %r14d, %r14d
movl $0x64, %r12d
movl $0x3e8, %ebp            # imm = 0x3E8
cmpl $0x2, %ebx
jl 0x1825
movq 0x8(%r15), %rdi
callq 0x11b0
movl %eax, %r13d
cmpl $0x2, %ebx
je 0x182b
movq 0x10(%r15), %rdi
callq 0x11b0
movl %eax, %r12d
cmpl $0x4, %ebx
jb 0x182b
movq 0x18(%r15), %rdi
callq 0x11b0
movl %eax, %ebp
cmpl $0x4, %ebx
je 0x182b
movq 0x20(%r15), %rdi
leaq 0x185b(%rip), %rsi      # 0x3076
callq 0x1180
movq %rax, %rcx
jmp 0x182d
movl $0x186a0, %r13d         # imm = 0x186A0
xorl %ecx, %ecx
movl %r13d, %eax
cltd
idivl %r12d
cmpl %ebp, %eax
jge 0x19ad
movq %rcx, 0x78(%rsp)
movl %r12d, 0x1c(%rsp)
movl %ebp, 0x24(%rsp)
movq %r15, 0x58(%rsp)
movq %r14, 0x50(%rsp)
movslq %r13d, %rdi
shlq $0x2, %rdi
callq 0x1130
movq %rax, %rbx
movl %r13d, %r14d
movl %r13d, 0x20(%rsp)
testl %r13d, %r13d
jle 0x18c3
leaq 0x48(%rsp), %r15
leaq 0x44(%rsp), %r13
leaq 0x40(%rsp), %r12
xorl %ebp, %ebp
leaq 0x17f0(%rip), %rdi      # 0x3078
leaq 0x4c(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x11a0
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,%rbp,4)
incq %rbp
cmpq %rbp, %r14
jne 0x1881
xorps %xmm0, %xmm0
cvtsi2sdl 0x1c(%rsp), %xmm0
movsd 0x1734(%rip), %xmm1     # 0x3008
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x80(%rsp), %rbp
xorl %r13d, %r13d
movsd 0x28(%rsp), %xmm0
movsd 0x1707(%rip), %xmm1     # 0x3008
callq 0x133d
callq 0x10a0
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x11d0
cmpl $0x0, 0x20(%rsp)
jle 0x193b
xorl %r12d, %r12d
movl (%rbx,%r12,4), %edi
movl $0x1, %esi
callq 0x140a
incq %r12
cmpq %r12, %r14
jne 0x1925
callq 0x10a0
movq %rax, %r12
movq %rbp, %rdi
callq 0x11d0
subq %r15, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
divsd 0x16d2(%rip), %xmm0     # 0x3030
movq 0x80(%rsp), %r15
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 0x19be
movsd %xmm2, 0x30(%rsp)
xorl %eax, %eax
callq 0x13e2
incq %r13
jmp 0x18f3
leaq 0x1794(%rip), %rdi      # 0x3148
callq 0x1070
jmp 0x1c6b
movsd %xmm0, 0x28(%rsp)
movzwl %dx, %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
subq %rax, %r15
subsd %xmm0, %xmm1
divsd 0x1651(%rip), %xmm1     # 0x3038
movsd %xmm1, 0x60(%rsp)
divsd 0x164b(%rip), %xmm2     # 0x3040
xorpd %xmm0, %xmm0
xorl %eax, %eax
movl 0x1c(%rsp), %r12d
movsd 0xa0(%rsp,%rax,8), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x5, %rax
jne 0x1a00
mulsd 0x1622(%rip), %xmm0     # 0x3048
movsd 0x15e2(%rip), %xmm1     # 0x3010
movsd %xmm2, 0x30(%rsp)
callq 0x1090
mulsd 0x160f(%rip), %xmm0     # 0x3050
divsd 0x160f(%rip), %xmm0     # 0x3058
movsd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x70(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rsp)
movq %rbx, %rdi
callq 0x1050
leaq 0x160b(%rip), %rdi      # 0x3081
movl 0x20(%rsp), %r13d
movl %r13d, %esi
movsd 0x30(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
movb $0x3, %al
callq 0x10c0
leaq 0x18(%rsp), %r14
movl 0x24(%rsp), %ebp
movl %ebp, %edi
movq %r14, %rsi
movl %r13d, %edx
callq 0x1480
movq %rax, %rbx
movl (%r14), %esi
leaq 0x15e2(%rip), %rdi      # 0x309c
xorl %eax, %eax
callq 0x10c0
xorl %eax, %eax
callq 0x13e2
movq 0x78(%rsp), %r14
testq %r14, %r14
je 0x1b8f
movq 0x58(%rsp), %rax
movq (%rax), %rdx
movl 0x18(%rsp), %eax
movl %eax, (%rsp)
leaq 0x15b9(%rip), %rsi      # 0x30a5
movq %r14, %rdi
movl %r13d, %ecx
movl %r12d, %r8d
movl %ebp, %r9d
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x1120
cmpl $0x0, 0x18(%rsp)
jle 0x1b79
leaq 0xc(%rbx), %r12
movl $0xff, %ebp
xorl %r13d, %r13d
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
movl (%r12), %esi
leal (%rsi,%rsi,4), %esi
movl -0x4(%r12), %edi
leal (%rdi,%rdi,4), %edi
movl %edi, 0x10(%rsp)
movl %esi, 0x8(%rsp)
movl %eax, (%rsp)
movq %r14, %rdi
leaq 0x1558(%rip), %rsi      # 0x30b9
xorl %eax, %eax
callq 0x1120
incq %r13
movslq 0x18(%rsp), %rax
addq $0x10, %r12
cmpq %rax, %r13
jl 0x1b18
movq %r14, %rdi
callq 0x10b0
movl 0x24(%rsp), %ebp
movl 0x1c(%rsp), %r12d
movl 0x20(%rsp), %r13d
movslq 0x18(%rsp), %rax
testq %rax, %rax
jle 0x1bef
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
shlq $0x4, %rax
movsd 0x14b3(%rip), %xmm1     # 0x3060
xorl %ecx, %ecx
movq 0x50(%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 0x1bb4
jmp 0x1bfc
movsd 0x1469(%rip), %xmm4     # 0x3060
movq 0x50(%rsp), %r14
movapd %xmm4, 0x30(%rsp)
movq %rbx, %rdi
callq 0x1050
testq %r14, %r14
je 0x1c6b
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
movsd 0x60(%rsp), %xmm1
addsd %xmm0, %xmm1
movq 0x58(%rsp), %rax
movq (%rax), %rdx
movl 0x18(%rsp), %eax
movl %eax, (%rsp)
leaq 0x1498(%rip), %rsi      # 0x30cf
movq %r14, %rdi
movl %r13d, %ecx
movl %r12d, %r8d
movl %ebp, %r9d
movsd 0x28(%rsp), %xmm0
movapd 0x30(%rsp), %xmm2
movb $0x3, %al
callq 0x1120
movl $0xa, %edi
movq %r14, %rsi
callq 0x10f0
movq %r14, %rdi
callq 0x10b0
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop