// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
cvtsi2ss %edi, %xmm0
movq %rsi, 0x28(%rsp)
divss 0x1b89(%rip), %xmm0     # 0x3028
movss %xmm0, 0x8(%rsp)
movl $0x10, %edi
movl $0x5, %esi
callq 0x1110
movq %rax, %r14
movsd 0x1b49(%rip), %xmm0     # 0x3008
divsd 0x3c99(%rip), %xmm0     # 0x5160
cvttsd2si %xmm0, %edi
incl %edi
orl $0x1, %edi
movl %edi, 0x4(%rsp)
callq 0x2140
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r12d, %r12d
movl $0x5, %r13d
xorl %ebx, %ebx
leaq 0x3c71(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
cmpl $0x0, 0x8(%rax)
jle 0x16f3
leaq 0x1(%r12), %rcx
movq %rcx, 0x18(%rsp)
xorl %r15d, %r15d
movq %r12, 0x20(%rsp)
movq 0x28(%rax), %rcx
imulq $0x38, %r15, %rdx
movq %rdx, 0x8(%rsp)
movl (%rcx,%rdx), %esi
cmpq $0x7fffffff, %rsi       # imm = 0x7FFFFFFF
je 0x16d9
movl %ebx, (%rsp)
shlq $0x20, %rsi
orq %r12, %rsi
movq 0x10(%rsp), %rdi
movl 0x4(%rsp), %edx
callq 0x2215
testq %rax, %rax
je 0x157b
movq %rax, %r12
movl 0x8(%rax), %ebp
movq 0x10(%rax), %rbx
movq 0x18(%rax), %rax
testq %rbx, %rbx
je 0x1570
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x157f
addq $0x10, %rax
jmp 0x15b0
xorl %ebp, %ebp
jmp 0x15bb
movq 0x20(%rsp), %rcx
leaq 0x3be5(%rip), %rax      # 0x5170
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movq 0x8(%rsp), %rdx
movl (%rax,%rdx), %esi
shlq $0x20, %rsi
orq %rcx, %rsi
movq 0x10(%rsp), %rdi
movl 0x4(%rsp), %edx
callq 0x2254
movq %rbx, (%rax)
movq %r12, %rdi
callq 0x1050
movq 0x20(%rsp), %r12
leaq 0x3ba9(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
movq 0x28(%rax), %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rax,%rcx), %rax
movl (%rax), %ebx
leal (%rbx,%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
ucomisd 0x1a33(%rip), %xmm0     # 0x3020
jb 0x15f5
sqrtsd %xmm0, %xmm0
jmp 0x15fa
callq 0x11c0
subl %ebp, %ebx
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd 0x1a0d(%rip), %xmm0     # 0x3018
addsd %xmm1, %xmm0
ucomisd 0x30(%rsp), %xmm0
jae 0x1624
leaq 0x3b52(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
jmp 0x168d
movl (%rsp), %ebp
cmpl %ebp, %r13d
jg 0x1648
addl %r13d, %r13d
movslq %r13d, %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x1160
movq %rax, %r14
cmpl %ebp, %ebx
jle 0x162c
leaq 0x3b21(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
movq 0x28(%rax), %rcx
movq 0x8(%rsp), %r8
movl (%rcx,%r8), %edx
movslq %ebp, %rsi
shlq $0x4, %rsi
movl %edx, (%r14,%rsi)
movl %r12d, 0x4(%r14,%rsi)
movq 0x10(%rcx,%r8), %rdx
movl %ebp, %edi
movl (%rdx), %ebp
movl %ebp, 0x8(%r14,%rsi)
subl 0x8(%rcx,%r8), %ebp
movl %ebp, 0xc(%r14,%rsi)
incl %edi
movl %edi, (%rsp)
cmpq $0x4, %r12
je 0x16d6
testl %ebp, %ebp
je 0x16d6
movq 0x28(%rax), %rax
leaq 0x3a6e(%rip), %rcx      # 0x5110
movl 0x4(%rcx,%r12,4), %esi
movq 0x8(%rsp), %rcx
andl (%rax,%rcx), %esi
shlq $0x20, %rsi
orq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
xorl %edx, %edx
movl 0x4(%rsp), %ecx
callq 0x219f
addl %ebp, 0x8(%rax)
leaq 0x3a9e(%rip), %rax      # 0x5170
movq (%rax,%r12,8), %rax
movl (%rsp), %ebx
incq %r15
movslq 0x8(%rax), %rcx
cmpq %rcx, %r15
jl 0x151f
movq 0x18(%rsp), %rax
movq %rax, %r12
jmp 0x16f9
incq %r12
movq %r12, %rax
cmpq $0x5, %rax
jne 0x14f8
movq 0x10(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x214d
movslq %ebx, %r15
movq %r15, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x1160
movq %rax, %r14
leaq -0x2df(%rip), %rcx      # 0x144e
movl $0x10, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x1080
movq 0x28(%rsp), %rax
movl %r15d, (%rax)
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop