// File: /workspace/github_c_makefile_star2_O1/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, 0x28(%rsp)
divss 0x2b79(%rip), %xmm0     # 0x4028
movss %xmm0, 0x10(%rsp)
movl $0x10, %edi
movl $0x5, %esi
callq 0x1120
movq %rax, %r14
movsd 0x2b39(%rip), %xmm0     # 0x4008
divsd 0x4c99(%rip), %xmm0     # 0x6170
cvttsd2si %xmm0, %edi
incl %edi
orl $0x1, %edi
movl %edi, 0xc(%rsp)
callq 0x21d8
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
xorl %r12d, %r12d
movl $0x5, %ebx
xorl %edi, %edi
leaq 0x4c72(%rip), %rax      # 0x6180
movq (%rax,%r12,8), %rax
cmpl $0x0, 0x8(%rax)
jle 0x1720
leaq 0x1(%r12), %rcx
movq %rcx, 0x38(%rsp)
xorl %ebp, %ebp
movq %r12, 0x20(%rsp)
movq 0x28(%rax), %rax
imulq $0x38, %rbp, %rcx
movq %rcx, 0x10(%rsp)
movl (%rax,%rcx), %esi
cmpq $0x7fffffff, %rsi       # imm = 0x7FFFFFFF
je 0x1705
movl %edi, %r13d
shlq $0x20, %rsi
orq %r12, %rsi
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %edx
callq 0x22b3
movq %rax, %r12
testq %rax, %rax
je 0x1571
movl 0x8(%r12), %r15d
jmp 0x1574
xorl %r15d, %r15d
testq %r12, %r12
je 0x15e2
movq 0x10(%r12), %rcx
testq %rcx, %rcx
je 0x158c
movq 0x18(%r12), %rax
movq %rax, 0x18(%rcx)
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x159c
addq $0x10, %rax
jmp 0x15d7
movq %rcx, 0x30(%rsp)
movq 0x20(%rsp), %rcx
leaq 0x4bd3(%rip), %rax      # 0x6180
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rax
movq 0x10(%rsp), %rdx
movl (%rax,%rdx), %esi
shlq $0x20, %rsi
orq %rcx, %rsi
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %edx
callq 0x22f2
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movq %r12, %rdi
callq 0x1050
movq 0x20(%rsp), %rax
leaq 0x4b92(%rip), %rcx      # 0x6180
movq (%rcx,%rax,8), %rax
movq 0x28(%rax), %rax
movq 0x10(%rsp), %rcx
movq 0x10(%rax,%rcx), %rax
movl (%rax), %r12d
leal (%r12,%r12), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
ucomisd 0x2a0a(%rip), %xmm0     # 0x4020
jb 0x161e
sqrtsd %xmm0, %xmm0
jmp 0x1623
callq 0x11d0
movl %r13d, %edi
subl %r15d, %r12d
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
mulsd 0x29df(%rip), %xmm0     # 0x4018
addsd %xmm1, %xmm0
ucomisd 0x40(%rsp), %xmm0
jae 0x164c
movq 0x20(%rsp), %r12
jmp 0x16b4
cmpl %edi, %ebx
movq 0x20(%rsp), %r12
jg 0x1674
addl %ebx, %ebx
movslq %ebx, %r15
movq %r15, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x1170
movl %r13d, %edi
movq %rax, %r14
cmpl %r13d, %r15d
jle 0x1655
leaq 0x4b05(%rip), %rax      # 0x6180
movq (%rax,%r12,8), %rax
movq 0x28(%rax), %rax
movq 0x10(%rsp), %rsi
movl (%rax,%rsi), %ecx
movslq %edi, %rdx
shlq $0x4, %rdx
movl %ecx, (%r14,%rdx)
movl %r12d, 0x4(%r14,%rdx)
movq 0x10(%rax,%rsi), %rcx
movl (%rcx), %r15d
movl %r15d, 0x8(%r14,%rdx)
subl 0x8(%rax,%rsi), %r15d
movl %r15d, 0xc(%r14,%rdx)
incl %edi
cmpq $0x4, %r12
je 0x1705
testl %r15d, %r15d
je 0x1705
leaq 0x4aba(%rip), %rax      # 0x6180
movq (%rax,%r12,8), %rax
movq 0x28(%rax), %rax
leaq 0x4a4b(%rip), %rcx      # 0x6120
movl 0x4(%rcx,%r12,4), %esi
movq 0x10(%rsp), %rcx
andl (%rax,%rcx), %esi
shlq $0x20, %rsi
orq 0x38(%rsp), %rsi
movl %edi, %r13d
movq 0x18(%rsp), %rdi
xorl %edx, %edx
movl 0xc(%rsp), %ecx
callq 0x2237
movl %r13d, %edi
addl %r15d, 0x8(%rax)
incq %rbp
leaq 0x4a71(%rip), %rax      # 0x6180
movq (%rax,%r12,8), %rax
movslq 0x8(%rax), %rcx
cmpq %rcx, %rbp
jl 0x152d
incq %r12
cmpq $0x5, %r12
jne 0x1507
movl %edi, %ebx
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x21e5
movslq %ebx, %r15
movq %r15, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
callq 0x1170
movq %rax, %r14
leaq -0x2fb(%rip), %rcx      # 0x145e
movl $0x10, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x1080
movq 0x28(%rsp), %rax
movl %r15d, (%rax)
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop