// File: /workspace/github_c_makefile_star2_O1/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 0x161b(%rip), %rdi      # 0x4100
callq 0x1030
testq %rax, %rax
je 0x2b03
leaq 0x1616(%rip), %rsi      # 0x410c
movq %rax, %rdi
callq 0x1180
movq %rax, %r14
jmp 0x2b06
xorl %r14d, %r14d
movl $0x186a0, %ebp          # imm = 0x186A0
cmpl $0x2, %ebx
jl 0x2b1b
movq 0x8(%r15), %rdi
callq 0x11b0
movl %eax, %ebp
movl $0x64, %r12d
cmpl $0x3, %ebx
jl 0x2b32
movq 0x10(%r15), %rdi
callq 0x11b0
movl %eax, %r12d
movl $0x3e8, %r13d           # imm = 0x3E8
cmpl $0x4, %ebx
jl 0x2b49
movq 0x18(%r15), %rdi
callq 0x11b0
movl %eax, %r13d
cmpl $0x5, %ebx
jl 0x2b63
movq 0x20(%r15), %rdi
leaq 0x15b5(%rip), %rsi      # 0x410e
callq 0x1180
movq %rax, %rcx
jmp 0x2b65
xorl %ecx, %ecx
movl %ebp, %eax
cltd
idivl %r12d
cmpl %r13d, %eax
jge 0x2fe3
movq %rcx, 0x50(%rsp)
movl %r13d, 0x34(%rsp)
movl %r12d, 0x10(%rsp)
movq %r15, 0x68(%rsp)
movq %r14, 0x60(%rsp)
movslq %ebp, %rdi
shlq $0x3, %rdi
callq 0x1130
movq %rax, %rbx
movl %ebp, %r14d
movl %ebp, 0x14(%rsp)
testl %ebp, %ebp
jle 0x2c55
xorl %r15d, %r15d
leaq 0x155b(%rip), %rbp      # 0x4110
movq %rbp, %rdi
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x11a0
movl 0x24(%rsp), %eax
movl 0x20(%rsp), %r12d
shll $0x10, %eax
shll $0x8, %r12d
addl %eax, %r12d
addl 0x1c(%rsp), %r12d
movl 0x18(%rsp), %r13d
movq %rbp, %rdi
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x11a0
movslq 0x24(%rsp), %rax
movslq 0x20(%rsp), %rcx
shlq $0x10, %rax
shlq $0x8, %rcx
addq %rax, %rcx
movslq 0x1c(%rsp), %rax
addq %rcx, %rax
shlq $0x8, %rax
movslq 0x18(%rsp), %rcx
addq %rax, %rcx
shlq $0x28, %r12
shlq $0x20, %r13
addq %r12, %r13
orq %rcx, %r13
movq %r13, (%rbx,%r15,8)
incq %r15
cmpq %r15, %r14
jne 0x2bae
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rsp), %xmm0
movsd 0x13a2(%rip), %xmm1     # 0x4008
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
movsd %xmm2, 0x40(%rsp)
movsd 0x28(%rsp), %xmm0
movsd 0x137e(%rip), %xmm1     # 0x4008
callq 0x134d
callq 0x1090
movq %rax, %r12
leaq 0x90(%rsp), %rdi
callq 0x11d0
cmpl $0x0, 0x14(%rsp)
jle 0x2cbf
xorl %r15d, %r15d
movq (%rbx,%r15,8), %rdi
callq 0x13c0
incq %r15
cmpq %r15, %r14
jne 0x2cae
callq 0x1090
movq %rax, %r13
leaq 0x80(%rsp), %rdi
callq 0x11d0
subq %r12, %r13
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
divsd 0x13e9(%rip), %xmm0     # 0x40d0
movq 0x80(%rsp), %r12
movq 0x90(%rsp), %r13
movzwl 0x88(%rsp), %eax
movzwl 0x98(%rsp), %r15d
movsd %xmm0, 0xa0(%rsp,%rbp,8)
cmpq $0x4, %rbp
je 0x2d34
movw %ax, 0x58(%rsp)
xorl %eax, %eax
movsd %xmm0, 0x38(%rsp)
callq 0x1395
movzwl 0x58(%rsp), %eax
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm2
addsd %xmm0, %xmm2
incq %rbp
cmpq $0x5, %rbp
jne 0x2c76
movsd %xmm0, 0x38(%rsp)
movzwl %ax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movzwl %r15w, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
subq %r13, %r12
movq %r12, 0x78(%rsp)
subsd %xmm0, %xmm1
divsd 0x135e(%rip), %xmm1     # 0x40d8
movsd %xmm1, 0x70(%rsp)
divsd 0x1358(%rip), %xmm2     # 0x40e0
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd 0xa0(%rsp,%rax,8), %xmm1
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq $0x5, %rax
jne 0x2d8e
mulsd 0x1334(%rip), %xmm0     # 0x40e8
movsd 0x1254(%rip), %xmm1     # 0x4010
movsd %xmm2, 0x40(%rsp)
callq 0x1080
mulsd 0x1321(%rip), %xmm0     # 0x40f0
divsd 0x1321(%rip), %xmm0     # 0x40f8
movsd 0x40(%rsp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x28(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, 0x58(%rsp)
movq %rbx, %rdi
callq 0x1050
leaq 0x1315(%rip), %rdi      # 0x4119
movl 0x14(%rsp), %ebp
movl %ebp, %esi
movsd 0x40(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
movb $0x3, %al
callq 0x10b0
leaq 0xc(%rsp), %r14
movl 0x34(%rsp), %r12d
movl %r12d, %edi
movq %r14, %rsi
callq 0x143e
movq %rax, %r13
movl (%r14), %esi
leaq 0x12f1(%rip), %rdi      # 0x4136
xorl %eax, %eax
callq 0x10b0
xorl %eax, %eax
callq 0x1395
movq 0x50(%rsp), %rax
testq %rax, %rax
movq 0x68(%rsp), %r14
movl 0x10(%rsp), %r15d
je 0x2f7e
movq %r13, 0x28(%rsp)
movq (%r14), %rdx
movl 0xc(%rsp), %r10d
subq $0x8, %rsp
leaq 0x12bc(%rip), %rsi      # 0x413f
movq %rax, %rdi
movl %ebp, %ecx
movl %r15d, %r8d
movl %r12d, %r9d
movsd 0x40(%rsp), %xmm0
movb $0x1, %al
pushq %r10
callq 0x1120
addq $0x10, %rsp
cmpl $0x0, 0xc(%rsp)
jle 0x2f5c
movq 0x28(%rsp), %rax
leaq 0x10(%rax), %r15
xorl %r13d, %r13d
movl -0x8(%r15), %edx
movq -0x10(%r15), %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, %r12d
shrl $0x10, %r12d
andl %eax, %r12d
movl %r10d, %ebx
shrl $0x8, %ebx
andl %eax, %ebx
andl %eax, %r10d
movl (%r15), %eax
leal (%rax,%rax,4), %eax
leal (%rax,%rax,4), %eax
movq %rax, 0x40(%rsp)
movl -0x4(%r15), %eax
leal (%rax,%rax,4), %eax
leal (%rax,%rax,4), %ebp
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
leaq 0x1225(%rip), %rsi      # 0x4153
xorl %eax, %eax
pushq %rbp
pushq 0x50(%rsp)
pushq %r10
pushq %rbx
pushq %r12
pushq %r14
pushq %r11
callq 0x1120
addq $0x40, %rsp
incq %r13
movslq 0xc(%rsp), %rax
addq $0x18, %r15
cmpq %rax, %r13
jl 0x2eb8
movq 0x50(%rsp), %rdi
callq 0x10a0
movq 0x68(%rsp), %r14
movl 0x14(%rsp), %ebp
movl 0x10(%rsp), %r15d
movl 0x34(%rsp), %r12d
movq 0x28(%rsp), %r13
movslq 0xc(%rsp), %rax
testq %rax, %rax
jle 0x2ff4
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movsd 0x10d1(%rip), %xmm1     # 0x4070
xorl %ecx, %ecx
movq 0x60(%rsp), %rbx
movl 0xc(%r13,%rcx), %edx
subl 0x10(%r13,%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 0x2fa6
jmp 0x3001
leaq 0x1203(%rip), %rdi      # 0x41ed
callq 0x1070
jmp 0x3074
movsd 0x1074(%rip), %xmm4     # 0x4070
movq 0x60(%rsp), %rbx
movapd %xmm4, 0x40(%rsp)
movq %r13, %rdi
callq 0x1050
testq %rbx, %rbx
je 0x3074
xorps %xmm0, %xmm0
cvtsi2sdq 0x78(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
addsd %xmm0, %xmm1
movq (%r14), %rdx
movl 0xc(%rsp), %r10d
subq $0x8, %rsp
leaq 0x113a(%rip), %rsi      # 0x4175
movq %rbx, %rdi
movl %ebp, %ecx
movl %r15d, %r8d
movl %r12d, %r9d
movsd 0x40(%rsp), %xmm0
movapd 0x48(%rsp), %xmm2
movb $0x3, %al
pushq %r10
callq 0x1120
addq $0x10, %rsp
movl $0xa, %edi
movq %rbx, %rsi
callq 0x10f0
movq %rbx, %rdi
callq 0x10a0
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq