// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/bam2bedGraph.c
// Function: hash2BedGraph
void hash2BedGraph(DataBuf *databuf,int ref,FILE *stream){
	unsigned long all_keys_count = databuf->Start->used + databuf->End->used;
	if (!all_keys_count) return;
	uint32_t i=0,prevkey=0,last_start=0,last_end=0,last_depth=0,Count=0,pos=0;
	sds *all_keys=union_hashed_keys(databuf->Start,databuf->End);
	for (i = 0; i < all_keys_count; ++i) {
		pos=sds2int(all_keys[i]);
		if (pos && pos==prevkey) continue;
		if (prevkey) {
			if (last_depth==Count) {
				prevkey=last_start;
			}
			else{
				if (last_depth) fprintf(stream,"%s\t%u\t%u\t%u\n",databuf->fp->header->target_name[ref],last_start,last_end,last_depth);
			}
		}
		last_start=prevkey;
		last_end=pos;
		last_depth=Count;
		dictEntry *data=dictFind(databuf->Start,all_keys[i]);
		if (data!=NULL) Count+= *((int*)data->val);
		data=dictFind(databuf->End,all_keys[i]);
		if (data!=NULL) Count-= *((int*)data->val);
		prevkey=pos;
	}
	if (last_depth){
		fprintf(stream,"%s\t%u\t%u\t%u\n",databuf->fp->header->target_name[ref],last_start,last_end,last_depth);
	}
	fprintf(stdout,"%s\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu64 "\n",databuf->fp->header->target_name[ref],databuf->fp->header->target_len[ref],databuf->chrRC[ref],databuf->chrReadLen[ref]);
	free(all_keys);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x18(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
movq 0x10(%rbx), %rsi
movq 0x20(%rsi), %rax
addq 0x20(%rdi), %rax
movq %rax, 0x20(%rsp)
je 0x4d36
callq 0x4aa7
movslq %ebp, %rcx
movq %rcx, 0x10(%rsp)
movq %rax, %rbp
xorl %r14d, %r14d
xorl %edi, %edi
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rbx, 0x8(%rsp)
movq (%rbp,%r14,8), %rax
movb -0x8(%rax), %dl
decb %dl
movl $0x0, %r13d
js 0x4bef
movzbl %dl, %edx
incq %rdx
xorl %r13d, %r13d
shll $0x8, %r13d
movzbl -0x1(%rax,%rdx), %esi
orl %esi, %r13d
decq %rdx
jg 0x4bde
testl %r13d, %r13d
setne %al
cmpl %r12d, %r13d
sete %dl
testb %dl, %al
je 0x4c07
movl %r12d, %r13d
jmp 0x4ca1
testl %r12d, %r12d
movl %edi, 0x4(%rsp)
je 0x4c1a
cmpl %edi, %r9d
jne 0x4c1f
movl %ecx, %r12d
jmp 0x4c4b
xorl %r12d, %r12d
jmp 0x4c4b
testl %r9d, %r9d
je 0x4c4b
movq (%rbx), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq 0x10(%rsp), %rdx
movq (%rax,%rdx,8), %rdx
movq 0x18(%rsp), %rdi
leaq 0x1683c(%rip), %rsi     # 0x1b480
xorl %eax, %eax
callq 0x3370
movq 0x8(%rbx), %rdi
movq %rbp, %rbx
movq (%rbp,%r14,8), %rsi
callq 0x6776
movl 0x4(%rsp), %ecx
movl %ecx, %ebp
testq %rax, %rax
je 0x4c6f
movq 0x8(%rax), %rax
movl (%rax), %ebp
addl %ecx, %ebp
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rbx,%r14,8), %rsi
callq 0x6776
testq %rax, %rax
je 0x4c8c
movq 0x8(%rax), %rax
subl (%rax), %ebp
movl %r12d, %ecx
movl %r13d, %r8d
movl 0x4(%rsp), %r9d
movl %ebp, %edi
movq %rbx, %rbp
movq 0x8(%rsp), %rbx
incl %r15d
movq %r15, %r14
movl %r13d, %r12d
cmpq %r15, 0x20(%rsp)
ja 0x4bc3
testl %r9d, %r9d
movq 0x10(%rsp), %r14
je 0x4ce1
movq (%rbx), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax,%r14,8), %rdx
leaq 0x167ab(%rip), %rsi     # 0x1b480
movq 0x18(%rsp), %rdi
xorl %eax, %eax
callq 0x3370
movq 0x1f2d8(%rip), %rax     # 0x23fc0
movq (%rax), %rdi
movq (%rbx), %rax
movq 0x18(%rbx), %rsi
movq 0x10(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rax
movq (%rcx,%r14,8), %rdx
movl (%rax,%r14,4), %ecx
movl (%rsi,%r14,4), %r8d
movq 0x20(%rbx), %rax
movq (%rax,%r14,8), %r9
leaq 0x16774(%rip), %rsi     # 0x1b48d
xorl %eax, %eax
callq 0x3370
movq %rbp, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3060
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq