// File: /workspace/github_c_makefile_star2_O0/ranbenbasat[P]RHHH/randhhh2D.c
// Function: output2
HeavyHitter * output2(int threshold, int * numhitters) {
	HeavyHitter * output; //This will be the heavy hitters to output
	int n = 0; //the number of items in output
	int outputspace = 5, hpspace = 5;
#ifdef PROB
	float adjustedThreshold = (1-ignoreProb) * threshold / ((float) NUM_COUNTERS);
#else
	float adjustedThreshold = threshold / ((float) NUM_COUNTERS);
#endif	
	int i, j, k, l, m, im;
	LCLitem_t newIP;
	int s;
	Descendant * Hp; //the direct descendants
	int numdescendants;
	int z = 4; //TODO: replace with a function of delta_s

	output = (HeavyHitter*) calloc(sizeof(HeavyHitter), outputspace); //ensure that it is sufficient
	Hp = (Descendant*) calloc(hpspace, sizeof(Descendant));
	
	for (i = 0; i < NUM_COUNTERS; i++) {
		for (j = 0; j < counters[i]->COUNTERSIZE; j++) {
			if (counters[i]->COUNTERS[j].item != LCL_NULLITEM) {
				//Now we just have to check that the counts are sufficient
				//s = amount that needs to be subtracted from the count
				
				if (counters[i]->COUNTERS[j].COUNT < adjustedThreshold) continue; // no point continuing

				numdescendants = 0; // |Hp| = 0
				for (k = 0; k < n; k++) {
					if ((masks[i] & masks[output[k].mask]) == masks[i] && (counters[i]->COUNTERS[j].item & masks[i]) == (output[k].item & masks[i])) {
						//output[k] is a descendant---is it direct?
						//assume it is for now, but check to make sure the assumption was valid for the previous ones
						l = 0;
						for (m = 0; m < numdescendants; m++) {
							Hp[l] = Hp[m];
							if ((masks[i] & masks[Hp[m].mask]) != masks[i] || (Hp[m].id & masks[output[k].mask]) != (output[k].item & masks[output[k].mask])) l++;
						}	
						numdescendants = l;
						Hp[numdescendants].mask = output[k].mask;
						Hp[numdescendants].id = output[k].item;
						numdescendants++;
						while (numdescendants >= hpspace) {hpspace *= 2; Hp = (Descendant*) realloc(Hp, sizeof(Descendant)*hpspace);}
					}
				}

				//now compute s
				s = 0;
				for (k = 0; k < numdescendants; k++) {
					s += LCU_PointEstLow(counters[Hp[k].mask], Hp[k].id);
				}
				for (k = 0; k < numdescendants; k++) {
					for (l = k + 1; l < numdescendants; l++) {

						if ((Hp[k].id & (masks[Hp[k].mask] & masks[Hp[l].mask])) != (Hp[l].id & (masks[Hp[k].mask] & masks[Hp[l].mask]))) continue; //there is no ip common to the subnets k and l

						newIP = (Hp[k].id | Hp[l].id);
						//compute the right mask
						im = 0; while(masks[im] != (masks[Hp[k].mask] | masks[Hp[l].mask])) im++;
						assert(masks[im] == (masks[Hp[k].mask] | masks[Hp[l].mask]));
						s -= LCU_PointEstUpp(counters[im], newIP);
					}
				}

				//now compare to the threshold
#ifdef PROB				
				if ((counters[i]->COUNTERS[j].COUNT - s  + 2*z*sqrt((1-ignoreProb) * 2 * counters[i]->COUNTERS[j].COUNT ) >= adjustedThreshold ) ){
#else
				if ((counters[i]->COUNTERS[j].COUNT - s  + 2*z*sqrt( 2 * counters[i]->COUNTERS[j].COUNT ) >= adjustedThreshold ) ){	
#endif	
					//Add this item to our list of heavy hitters
					while (n >= outputspace) {outputspace *= 2; output = (HeavyHitter *) realloc(output, outputspace * sizeof(HeavyHitter));}
					output[n].item = counters[i]->COUNTERS[j].item;
					output[n].mask = i;
					output[n].upper = counters[i]->COUNTERS[j].COUNT;
					output[n].lower = output[n].upper - counters[i]->COUNTERS[j].delta;
					n++;
				}

			}
		}
	}
	
	free(Hp);

	//now clean up the output
	output = (HeavyHitter*) realloc(output, n * sizeof(HeavyHitter));
	//qsort(output, n, sizeof(HeavyHitter), &cmpHH);

	*numhitters = n;

	return output;
}

// Assembly:
subq $0x78, %rsp
movl %edi, 0x74(%rsp)
movq %rsi, 0x68(%rsp)
movl $0x0, 0x5c(%rsp)
movl $0x5, 0x58(%rsp)
movl $0x5, 0x54(%rsp)
cvtsi2ssl 0x74(%rsp), %xmm0
movss 0x3a55(%rip), %xmm1     # 0x5018
divss %xmm1, %xmm0
movss %xmm0, 0x50(%rsp)
movl $0x4, 0x18(%rsp)
movslq 0x58(%rsp), %rsi
movl $0x18, %edi
callq 0x10f0
movq %rax, 0x60(%rsp)
movslq 0x54(%rsp), %rdi
movl $0x10, %esi
callq 0x10f0
movq %rax, 0x20(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x19, 0x4c(%rsp)
jge 0x1d96
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
movslq 0x4c(%rsp), %rdx
leaq 0x5bc8(%rip), %rcx      # 0x71f0
movq (%rcx,%rdx,8), %rcx
cmpl 0x8(%rcx), %eax
jge 0x1d84
movslq 0x4c(%rsp), %rcx
leaq 0x5baf(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpq $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
je 0x1d72
movslq 0x4c(%rsp), %rcx
leaq 0x5b82(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
cvtsi2ssl (%rax), %xmm1
movss 0x50(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x169a
jmp 0x1d74
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x191d
movslq 0x4c(%rsp), %rcx
leaq 0x5a3c(%rip), %rax      # 0x7100
movq (%rax,%rcx,8), %rax
movq 0x60(%rsp), %rcx
movslq 0x44(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movslq 0x8(%rcx), %rdx
leaq 0x5a1c(%rip), %rcx      # 0x7100
andq (%rcx,%rdx,8), %rax
movslq 0x4c(%rsp), %rdx
leaq 0x5a0c(%rip), %rcx      # 0x7100
cmpq (%rcx,%rdx,8), %rax
jne 0x190b
movslq 0x4c(%rsp), %rcx
leaq 0x5ae6(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq 0x4c(%rsp), %rdx
leaq 0x59d3(%rip), %rcx      # 0x7100
andq (%rcx,%rdx,8), %rax
movq 0x60(%rsp), %rcx
movslq 0x44(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq 0x4c(%rsp), %rsi
leaq 0x59af(%rip), %rdx      # 0x7100
andq (%rdx,%rsi,8), %rcx
cmpq %rcx, %rax
jne 0x190b
movl $0x0, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x1878
movq 0x20(%rsp), %rax
movslq 0x40(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x20(%rsp), %rcx
movslq 0x3c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movslq 0x4c(%rsp), %rcx
leaq 0x5948(%rip), %rax      # 0x7100
movq (%rax,%rcx,8), %rax
movq 0x20(%rsp), %rcx
movslq 0x3c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movslq 0x8(%rcx), %rdx
leaq 0x5928(%rip), %rcx      # 0x7100
andq (%rcx,%rdx,8), %rax
movslq 0x4c(%rsp), %rdx
leaq 0x5918(%rip), %rcx      # 0x7100
cmpq (%rcx,%rdx,8), %rax
jne 0x185b
movq 0x20(%rsp), %rax
movslq 0x3c(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x60(%rsp), %rcx
movslq 0x44(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movslq 0x8(%rcx), %rdx
leaq 0x58e2(%rip), %rcx      # 0x7100
andq (%rcx,%rdx,8), %rax
movq 0x60(%rsp), %rcx
movslq 0x44(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq 0x60(%rsp), %rdx
movslq 0x44(%rsp), %rsi
imulq $0x18, %rsi, %rsi
addq %rsi, %rdx
movslq 0x8(%rdx), %rsi
leaq 0x58ae(%rip), %rdx      # 0x7100
andq (%rdx,%rsi,8), %rcx
cmpq %rcx, %rax
je 0x1866
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x1868
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x176e
movl 0x40(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x60(%rsp), %rax
movslq 0x44(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq 0x60(%rsp), %rax
movslq 0x44(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq 0x20(%rsp), %rax
movslq 0x1c(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jl 0x1909
movl 0x54(%rsp), %eax
shll %eax
movl %eax, 0x54(%rsp)
movq 0x20(%rsp), %rdi
movslq 0x54(%rsp), %rsi
shlq $0x4, %rsi
callq 0x1140
movq %rax, 0x20(%rsp)
jmp 0x18db
jmp 0x190b
jmp 0x190d
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x16aa
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x1985
movq 0x20(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rcx
leaq 0x589d(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rdi
movq 0x20(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x2870
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x192d
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x1bc1
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x1baf
movq 0x20(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movslq 0x44(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movslq 0x8(%rcx), %rdx
leaq 0x571c(%rip), %rcx      # 0x7100
movq (%rcx,%rdx,8), %rcx
movq 0x20(%rsp), %rdx
movslq 0x40(%rsp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
movslq 0x8(%rdx), %rsi
leaq 0x56fc(%rip), %rdx      # 0x7100
andq (%rdx,%rsi,8), %rcx
andq %rcx, %rax
movq 0x20(%rsp), %rcx
movslq 0x40(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq 0x20(%rsp), %rdx
movslq 0x44(%rsp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
movslq 0x8(%rdx), %rsi
leaq 0x56c5(%rip), %rdx      # 0x7100
movq (%rdx,%rsi,8), %rdx
movq 0x20(%rsp), %rsi
movslq 0x40(%rsp), %rdi
shlq $0x4, %rdi
addq %rdi, %rsi
movslq 0x8(%rsi), %rdi
leaq 0x56a5(%rip), %rsi      # 0x7100
andq (%rsi,%rdi,8), %rdx
andq %rdx, %rcx
cmpq %rcx, %rax
je 0x1a6c
jmp 0x1b9f
movq 0x20(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movslq 0x40(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
orq (%rcx), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
movslq 0x38(%rsp), %rcx
leaq 0x5653(%rip), %rax      # 0x7100
movq (%rax,%rcx,8), %rax
movq 0x20(%rsp), %rcx
movslq 0x44(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movslq 0x8(%rcx), %rdx
leaq 0x5633(%rip), %rcx      # 0x7100
movq (%rcx,%rdx,8), %rcx
movq 0x20(%rsp), %rdx
movslq 0x40(%rsp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
movslq 0x8(%rdx), %rsi
leaq 0x5613(%rip), %rdx      # 0x7100
orq (%rdx,%rsi,8), %rcx
cmpq %rcx, %rax
je 0x1b03
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x1aa1
movslq 0x38(%rsp), %rcx
leaq 0x55f1(%rip), %rax      # 0x7100
movq (%rax,%rcx,8), %rax
movq 0x20(%rsp), %rcx
movslq 0x44(%rsp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movslq 0x8(%rcx), %rdx
leaq 0x55d1(%rip), %rcx      # 0x7100
movq (%rcx,%rdx,8), %rcx
movq 0x20(%rsp), %rdx
movslq 0x40(%rsp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
movslq 0x8(%rdx), %rsi
leaq 0x55b1(%rip), %rdx      # 0x7100
orq (%rdx,%rsi,8), %rcx
cmpq %rcx, %rax
jne 0x1b5a
jmp 0x1b79
leaq 0x34bb(%rip), %rdi      # 0x501c
leaq 0x34e9(%rip), %rsi      # 0x5051
movl $0x9d, %edx
leaq 0x34e9(%rip), %rcx      # 0x505d
callq 0x10a0
movslq 0x38(%rsp), %rcx
leaq 0x566b(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rdi
movq 0x30(%rsp), %rsi
callq 0x2770
movl %eax, %ecx
movl 0x2c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x2c(%rsp)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x19a6
jmp 0x1bb1
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x198d
movslq 0x4c(%rsp), %rcx
leaq 0x5623(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl (%rax), %eax
subl 0x2c(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x18(%rsp), %eax
shll %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
movslq 0x4c(%rsp), %rcx
leaq 0x55df(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl (%rax), %eax
shll %eax
cvtsi2sd %eax, %xmm0
callq 0x11a0
movsd 0x8(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x10(%rsp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movss 0x50(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1d70
jmp 0x1c63
movl 0x5c(%rsp), %eax
cmpl 0x58(%rsp), %eax
jl 0x1c91
movl 0x58(%rsp), %eax
shll %eax
movl %eax, 0x58(%rsp)
movq 0x60(%rsp), %rdi
movslq 0x58(%rsp), %rax
imulq $0x18, %rax, %rsi
callq 0x1140
movq %rax, 0x60(%rsp)
jmp 0x1c63
movslq 0x4c(%rsp), %rcx
leaq 0x5553(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq 0x60(%rsp), %rax
movslq 0x5c(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl 0x4c(%rsp), %ecx
movq 0x60(%rsp), %rax
movslq 0x5c(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movslq 0x4c(%rsp), %rcx
leaq 0x5504(%rip), %rax      # 0x71f0
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl (%rax), %ecx
movq 0x60(%rsp), %rax
movslq 0x5c(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq 0x60(%rsp), %rax
movslq 0x5c(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movslq 0x4c(%rsp), %rdx
leaq 0x54b6(%rip), %rax      # 0x71f0
movq (%rax,%rdx,8), %rax
movq 0x28(%rax), %rax
movslq 0x48(%rsp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rax
subl 0xc(%rax), %ecx
movq 0x60(%rsp), %rax
movslq 0x5c(%rsp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1d72
jmp 0x1d74
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x1618
jmp 0x1d86
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x1605
movq 0x20(%rsp), %rdi
callq 0x1040
movq 0x60(%rsp), %rdi
movslq 0x5c(%rsp), %rax
imulq $0x18, %rax, %rsi
callq 0x1140
movq %rax, 0x60(%rsp)
movl 0x5c(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, (%rax)
movq 0x60(%rsp), %rax
addq $0x78, %rsp
retq
nopl (%rax)