// File: /workspace/github_c_makefile_star2_O2/ranbenbasat[P]RHHH/randhhh1D.c
// Function: output
HeavyHitter * output(int threshold, int * numhitters, int streamLen) {
	LL ** hashtable; //for transferring counts to parents
	int htsize; //size of hash table;
#ifdef PROB
	float adjustedThreshold = (1-ignoreProb) * threshold / ((float) NUM_COUNTERS);
#else
	float adjustedThreshold = threshold / ((float) NUM_COUNTERS);
#endif
	HeavyHitter * output; //This will be the heavy hitters to output
	int n = 0; //the number of items in output
	int outputspace = 5;
	int i, j;
	LL * tmp;
	int s; //double count
	int z = 4; //TODO: replace with a function of delta_s

	output = (HeavyHitter *) calloc(sizeof(HeavyHitter), outputspace); 

	htsize = (((int)(1.0/SSepsval)) + 1) | 1;
	hashtable = HT_Init(htsize);
	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
				//first calculate the doublecount s
				tmp = HT_Find(hashtable, ((int64__t) counters[i]->COUNTERS[j].item) << 32 | ((int64__t)i), htsize);
				s = (tmp ? tmp->val : 0); //if there are no doublecounts recoreded assume s=0
				//now erase tmp, we don't need it again
				if (tmp) {
					if (tmp->next) tmp->next->prev = tmp->prev;

					if (tmp->prev) tmp->prev->next = tmp->next; //if prev just update it
					else *(HT_FindEntry(hashtable, ((int64__t)counters[i]->COUNTERS[j].item) << 32 | ((int64__t)i), htsize)) = tmp->next; //else update the start of the list
					free(tmp);
				}

				//now compare to the adjustedThreshold 
#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	
					while (outputspace <= n) {outputspace *= 2; output = (HeavyHitter *) realloc(output, sizeof(HeavyHitter) * outputspace);}
					//Add this item to our list of heavy hitters
					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;
					
					//update s
					s = output[n].lower;
					n++;
				}

				//passs up the doublecount s
				if (s != 0 && i + 1 < NUM_COUNTERS) { //check that there is a positive doublecount and someone to pass up to
					tmp = HT_Insert(hashtable, ((int64__t)(counters[i]->COUNTERS[j].item & masks[i+1])) << 32 | ((int64__t) (i+1)), 0, htsize);
					tmp->val += s;
				}
			}
		}
	}

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

	*numhitters = n;

	return output;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cvtsi2ss %edi, %xmm0
movq %rsi, 0x30(%rsp)
divss 0x1bd7(%rip), %xmm0     # 0x3028
movss %xmm0, 0x10(%rsp)
pushq $0x10
popq %rdi
pushq $0x5
popq %rsi
callq 0x1110
movq %rax, %r14
movsd 0x1b9b(%rip), %xmm0     # 0x3008
divsd 0x3ceb(%rip), %xmm0     # 0x5160
cvttsd2si %xmm0, %edi
incl %edi
orl $0x1, %edi
movl %edi, 0xc(%rsp)
callq 0x2038
movq %rax, 0x18(%rsp)
cvtss2sd 0x10(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
pushq $0x5
popq %r13
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq $0x5, %r12
je 0x168a
leaq 0x1(%r12), %rax
movq %rax, 0x28(%rsp)
xorl %edx, %edx
movq %r12, 0x10(%rsp)
leaq 0x3cad(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %rdx
jge 0x1680
movq 0x28(%rax), %rax
imulq $0x38, %rdx, %rbp
movl (%rax,%rbp), %esi
cmpq $0x7fffffff, %rsi       # imm = 0x7FFFFFFF
je 0x1678
movq %rdx, 0x20(%rsp)
shlq $0x20, %rsi
orq %r12, %rsi
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %edx
callq 0x2105
testq %rax, %rax
je 0x152e
movq %rax, %r12
movl 0x8(%rax), %r15d
movq 0x10(%rax), %rcx
movq 0x18(%rax), %rax
testq %rcx, %rcx
je 0x1523
movq %rax, 0x18(%rcx)
testq %rax, %rax
je 0x1533
addq $0x10, %rax
jmp 0x1569
xorl %r15d, %r15d
jmp 0x1574
movq %rcx, 0x38(%rsp)
movq 0x10(%rsp), %rcx
leaq 0x3c2c(%rip), %rax      # 0x5170
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movl (%rax,%rbp), %esi
shlq $0x20, %rsi
orq %rcx, %rsi
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %edx
callq 0x2145
movq 0x38(%rsp), %rcx
movq %rcx, (%rax)
movq %r12, %rdi
callq 0x1050
movq 0x10(%rsp), %rax
leaq 0x3bf0(%rip), %rcx      # 0x5170
movq (%rcx,%rax,8), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax,%rbp), %rax
movl (%rax), %r12d
leal (%r12,%r12), %eax
cvtsi2sd %eax, %xmm0
ucomisd 0x1a80(%rip), %xmm0     # 0x3020
jb 0x15a8
sqrtsd %xmm0, %xmm0
jmp 0x15ad
callq 0x11c0
subl %r15d, %r12d
cvtsi2sd %r12d, %xmm1
mulsd 0x1a5b(%rip), %xmm0     # 0x3018
addsd %xmm1, %xmm0
ucomisd 0x40(%rsp), %xmm0
jae 0x15d0
movq 0x10(%rsp), %r12
jmp 0x162c
movq 0x10(%rsp), %r12
cmpl %ebx, %r13d
jg 0x15f1
addl %r13d, %r13d
movslq %r13d, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x1160
movq %rax, %r14
jmp 0x15d5
leaq 0x3b78(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
movq 0x28(%rax), %rax
movl (%rax,%rbp), %ecx
movslq %ebx, %rdx
shlq $0x4, %rdx
movl %ecx, (%r14,%rdx)
movl %r12d, 0x4(%r14,%rdx)
movq 0x10(%rax,%rbp), %rcx
movl (%rcx), %r15d
movl %r15d, 0x8(%r14,%rdx)
subl 0x8(%rax,%rbp), %r15d
movl %r15d, 0xc(%r14,%rdx)
incl %ebx
cmpq $0x4, %r12
je 0x1673
testl %r15d, %r15d
je 0x1673
leaq 0x3b32(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
movq 0x28(%rax), %rax
leaq 0x3ac3(%rip), %rcx      # 0x5110
movq 0x28(%rsp), %rdx
movl (%rcx,%rdx,4), %esi
andl (%rax,%rbp), %esi
shlq $0x20, %rsi
orq %rdx, %rsi
movq 0x18(%rsp), %rdi
xorl %edx, %edx
movl 0xc(%rsp), %ecx
callq 0x2093
addl %r15d, 0x8(%rax)
movq 0x20(%rsp), %rdx
incq %rdx
jmp 0x14bc
movq 0x28(%rsp), %r12
jmp 0x14a1
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x2043
movslq %ebx, %r15
movq %r15, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x1160
movq %rax, %r14
leaq -0x2af(%rip), %rcx      # 0x1405
pushq $0x10
popq %rdx
movq %rax, %rdi
movq %r15, %rsi
callq 0x1080
movq 0x30(%rsp), %rax
movl %r15d, (%rax)
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq