// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_pileup.c
// Function: bam_plbuf_push
int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
{
	if (b) { // fill buffer
		if (b->core.tid < 0) return 0;
		if (b->core.flag & buf->flag_mask) return 0;
		bam_copy1(&buf->tail->b, b);
		buf->tail->beg = b->core.pos; buf->tail->end = bam_calend(&b->core, bam1_cigar(b));
		if (b->core.tid < buf->max_tid) {
			fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
			return -1;
		}
		if ((b->core.tid == buf->max_tid) && (buf->tail->beg < buf->max_pos)) {
			fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n");
			return -1;
		}
		buf->max_tid = b->core.tid; buf->max_pos = buf->tail->beg;
		if (buf->tail->end > buf->pos || buf->tail->b.core.tid > buf->tid) {
			buf->tail->next = mp_alloc(buf->mp);
			buf->tail = buf->tail->next;
		}
	} else buf->is_eof = 1;
	while (buf->is_eof || buf->max_tid > buf->tid || (buf->max_tid == buf->tid && buf->max_pos > buf->pos)) {
		int n_pu = 0;
		lbnode_t *p, *q;
		buf->dummy->next = buf->head;
		for (p = buf->head, q = buf->dummy; p->next; q = p, p = p->next) {
			if (p->b.core.tid < buf->tid || (p->b.core.tid == buf->tid && p->end <= buf->pos)) { // then remove from the list
				q->next = p->next; mp_free(buf->mp, p); p = q;
			} else if (p->b.core.tid == buf->tid && p->beg <= buf->pos) { // here: p->end > pos; then add to pileup
				if (n_pu == buf->max_pu) { // then double the capacity
					buf->max_pu = buf->max_pu? buf->max_pu<<1 : 256;
					buf->pu = (bam_pileup1_t*)realloc(buf->pu, sizeof(bam_pileup1_t) * buf->max_pu);
				}
				buf->pu[n_pu].b = &p->b;
				if (resolve_cigar(buf->pu + n_pu, buf->pos)) ++n_pu; // skip the read if we are looking at BAM_CREF_SKIP
			}
		}
		buf->head = buf->dummy->next; // dummy->next may be changed
		if (n_pu) { // then call user defined function
			buf->func(buf->tid, buf->pos, n_pu, buf->pu, buf->func_data);
		}
		// update tid and pos
		if (buf->head->next) {
			if (buf->tid > buf->head->b.core.tid) {
				fprintf(stderr, "[bam_plbuf_push] unsorted input. Pileup aborts.\n");
				return 1;
			}
		}
		if (buf->tid < buf->head->b.core.tid) { // come to a new reference sequence
			buf->tid = buf->head->b.core.tid; buf->pos = buf->head->beg; // jump to the next reference
		} else if (buf->pos < buf->head->beg) { // here: tid == head->b.core.tid
			buf->pos = buf->head->beg; // jump to the next position
		} else ++buf->pos; // scan contiguously
		if (buf->is_eof && buf->head->next == 0) break;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
testq %rdi, %rdi
je 0x17a6f
movq %rdi, %r14
xorl %eax, %eax
cmpl $0x0, (%rdi)
js 0x17e43
movl 0xc(%r14), %ecx
movl 0x50(%rbx), %edx
testw %cx, %dx
jne 0x17e43
movq 0x10(%rbx), %r12
movq 0x30(%r12), %r15
movl 0x28(%r12), %ebp
movl 0x28(%r14), %eax
cmpl %eax, %ebp
jge 0x179c4
decl %eax
movl %eax, %ecx
sarl %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x8, %eax
orl %ecx, %eax
movl %eax, %ebp
sarl $0x10, %ebp
orl %eax, %ebp
incl %ebp
movslq %ebp, %rsi
movq %r15, %rdi
callq 0x3610
movq %rax, %r15
movq 0x30(%r14), %rsi
movslq 0x24(%r14), %rdx
movq %r15, %rdi
callq 0x3680
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%r12)
movups %xmm0, (%r12)
movups %xmm1, 0x10(%r12)
movl %ebp, 0x28(%r12)
movq %r15, 0x30(%r12)
movl 0x4(%r14), %eax
movq 0x10(%rbx), %rcx
movl %eax, 0x38(%rcx)
movzbl 0xb(%r14), %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0x12268
movq 0x10(%rbx), %r15
movl %eax, 0x3c(%r15)
movl (%r14), %ecx
cmpl 0x38(%rbx), %ecx
jl 0x17e5e
movl 0x38(%r15), %edx
jne 0x17a3c
cmpl 0x3c(%rbx), %edx
jb 0x17e65
movl %ecx, 0x38(%rbx)
movl %edx, 0x3c(%rbx)
cmpl 0x34(%rbx), %eax
ja 0x17a4f
movl (%r15), %eax
cmpl 0x30(%rbx), %eax
jle 0x17a8f
movq (%rbx), %rax
incl (%rax)
movslq 0x4(%rax), %rcx
testq %rcx, %rcx
je 0x17a78
movq 0x10(%rax), %rdx
leaq -0x1(%rcx), %rsi
movl %esi, 0x4(%rax)
movq -0x8(%rdx,%rcx,8), %rax
jmp 0x17a87
movl $0x1, 0x44(%rbx)
jmp 0x17a8f
movl $0x1, %edi
movl $0x48, %esi
callq 0x3530
movq %rax, 0x40(%r15)
movq %rax, 0x10(%rbx)
movl 0x44(%rbx), %eax
movl $0x100, %ebp            # imm = 0x100
leaq 0xe272(%rip), %r12      # 0x25d10
testl %eax, %eax
jne 0x17ac2
movl 0x30(%rbx), %ecx
movl 0x38(%rbx), %edx
cmpl %ecx, %edx
jg 0x17ac2
xorl %eax, %eax
cmpl %ecx, %edx
jne 0x17e43
movl 0x3c(%rbx), %ecx
cmpl 0x34(%rbx), %ecx
jle 0x17e43
movq 0x8(%rbx), %r15
movq 0x18(%rbx), %r13
movq %r15, 0x40(%r13)
movq 0x40(%r15), %rax
testq %rax, %rax
je 0x17de9
leaq 0x40(%r15), %rdx
movq $0x0, 0x8(%rsp)
movl 0x30(%rbx), %ecx
cmpl %ecx, (%r15)
jge 0x17b38
movq %rax, 0x40(%r13)
movq (%rbx), %r14
decl (%r14)
movq $0x0, (%rdx)
movl 0x4(%r14), %ecx
cmpl 0x8(%r14), %ecx
jne 0x17d7a
leal (%rcx,%rcx), %eax
testl %ecx, %ecx
cmovel %ebp, %eax
movl %eax, 0x8(%r14)
movq 0x10(%r14), %rdi
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x3610
movq %rax, 0x10(%r14)
movl 0x4(%r14), %ecx
jmp 0x17d7e
jne 0x17d8f
movl 0x34(%rbx), %ecx
cmpl %ecx, 0x3c(%r15)
jbe 0x17af0
cmpl %ecx, 0x38(%r15)
ja 0x17d8f
movq 0x48(%rbx), %rax
movq 0x8(%rsp), %r14
cmpl 0x40(%rbx), %r14d
jne 0x17b87
leal (%r14,%r14), %ecx
testl %r14d, %r14d
cmovel %ebp, %ecx
movl %ecx, 0x40(%rbx)
movslq %ecx, %rcx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
callq 0x3610
movq %rax, 0x48(%rbx)
movl 0x34(%rbx), %ecx
movslq %r14d, %rdx
leaq (%rdx,%rdx,2), %rdx
movq %r15, (%rax,%rdx,8)
testb $0x4, 0xc(%r15)
jne 0x17d8f
movl 0x4(%r15), %r11d
cmpl %ecx, %r11d
ja 0x17e90
leaq (%rax,%rdx,8), %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rdx, 0x8(%rax)
movb 0x14(%rax), %dl
andb $-0x8, %dl
movb %dl, 0x14(%rax)
movq 0x8(%r15), %r8
movq %r8, %rsi
shrq $0x30, %rsi
je 0x17e71
xorl %esi, %esi
movb $0x1, %r13b
movl $0x2, %edi
shrl $0x18, %r8d
addq 0x30(%r15), %r8
movl -0x8(%r8,%rdi,4), %r9d
movl %r9d, %r8d
andl $0xf, %r8d
cmpl $0x4, %r8d
ja 0x17c09
shrl $0x4, %r9d
movslq (%r12,%r8,4), %r10
addq %r12, %r10
jmpq *%r10
addl %esi, %r9d
movl %r9d, %esi
movl %r11d, %r10d
jmp 0x17d43
addl %r11d, %r9d
movl %r9d, %r10d
cmpl %ecx, %r9d
jbe 0x17d48
jmp 0x17d8f
leal (%r9,%r11), %r10d
cmpl %ecx, %r10d
jbe 0x17d40
andb $-0x2, %dl
movb %dl, 0x14(%rax)
movl $0x0, 0xc(%rax)
leal (%rsi,%rcx), %ebp
subl %r11d, %ebp
movl %ebp, 0x8(%rax)
cmpl %r11d, %ecx
setne %r11b
notb %r13b
orb %r11b, %r13b
testb $0x1, %r13b
jne 0x17c61
orb $0x2, %dl
movb %dl, 0x14(%rax)
leal -0x1(%r10), %r11d
cmpl %ecx, %r11d
jne 0x17cdd
leaq -0x2(%rdi), %r11
movq 0x8(%r15), %r14
movq %r14, %rbp
shrq $0x30, %rbp
leal -0x1(%rbp), %r13d
cmpq %r13, %r11
jae 0x17ce4
movq 0x30(%r15), %r13
movl %r14d, %r11d
shrl $0x18, %r11d
movq %r13, 0x10(%rsp)
addq %r13, %r11
movl -0x4(%r11,%rdi,4), %r13d
movl %r13d, %r11d
andl $0xf, %r11d
cmpl $0x1, %r11d
je 0x17ceb
cmpl $0x2, %r11d
jne 0x17d21
shrl $0x4, %r13d
negl %r13d
jmp 0x17cef
addl %r11d, %r9d
cmpl %ecx, %r9d
jbe 0x17cd8
movl $0x0, 0xc(%rax)
orb $0x1, %dl
movb %dl, 0x14(%rax)
movl %ecx, %r10d
subl %r11d, %r10d
addl %esi, %r10d
movl %r10d, 0x8(%rax)
movl %r9d, %r10d
jmp 0x17d43
movl $0x100, %ebp            # imm = 0x100
jmp 0x17d40
movl $0x100, %ebp            # imm = 0x100
jmp 0x17d3a
shrl $0x4, %r13d
movl %r13d, 0xc(%rax)
movq 0x8(%r15), %r14
movq %r14, %rbp
shrq $0x30, %rbp
cmpq %rbp, %rdi
jae 0x17d19
shrl $0x18, %r14d
movq 0x10(%rsp), %r11
addq %r14, %r11
movl (%r11,%rdi,4), %r11d
andl $0xf, %r11d
jmp 0x17d2b
orb $0x4, %dl
movb %dl, 0x14(%rax)
jmp 0x17d2b
leal -0x1(%r11), %r13d
cmpl $0x2, %r13d
jb 0x17cfe
movl $0x100, %ebp            # imm = 0x100
addl $-0x3, %r11d
cmpl $0x2, %r11d
ja 0x17d40
orb $0x4, %dl
movb %dl, 0x14(%rax)
addl %r9d, %esi
cmpl %ecx, %r10d
ja 0x17da9
addl $-0x3, %r8d
cmpl $0x3, %r8d
setb %r13b
movq 0x8(%r15), %r8
movq %r8, %r9
shrq $0x30, %r9
leaq 0x1(%rdi), %r14
decq %rdi
movl %r10d, %r11d
cmpq %r9, %rdi
movq %r14, %rdi
jb 0x17bdb
jmp 0x17e71
movq 0x10(%r14), %rax
leal 0x1(%rcx), %edx
movl %edx, 0x4(%r14)
movslq %ecx, %rcx
movq %r15, (%rax,%rcx,8)
movq %r13, %r15
movq %r15, %r13
movq 0x40(%r15), %r15
leaq 0x40(%r15), %rdx
movq 0x40(%r15), %rax
testq %rax, %rax
jne 0x17ae8
jmp 0x17dbd
cmpl $0x3, %r8d
je 0x17d8f
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
jmp 0x17d8f
movq 0x18(%rbx), %rax
movq 0x40(%rax), %r15
movq %r15, 0x8(%rbx)
movl 0x30(%rbx), %edi
movq 0x8(%rsp), %rdx
testl %edx, %edx
je 0x17df0
movl 0x34(%rbx), %esi
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %r8
callq *0x20(%rbx)
movq 0x8(%rbx), %r15
jmp 0x17ded
movq %r15, 0x8(%rbx)
movl 0x30(%rbx), %edi
movq 0x40(%r15), %rax
testq %rax, %rax
setne %dl
movl (%r15), %ecx
cmpl %ecx, %edi
setg %sil
andb %dl, %sil
cmpb $0x1, %sil
je 0x17e52
cmpl %ecx, %edi
jge 0x17e19
movl %ecx, 0x30(%rbx)
movl 0x38(%r15), %ecx
jmp 0x17e28
movl 0x34(%rbx), %edx
movl 0x38(%r15), %esi
leal 0x1(%rdx), %ecx
cmpl %esi, %edx
cmovbl %esi, %ecx
testq %rax, %rax
setne %dl
movl %ecx, 0x34(%rbx)
movl 0x44(%rbx), %eax
testl %eax, %eax
sete %cl
orb %dl, %cl
jne 0x17a9e
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4984
movl $0x1, %eax
jmp 0x17e43
callq 0x4968
jmp 0x17e6a
callq 0x494c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x17e43
leaq 0xdfdd(%rip), %rdi      # 0x25e55
leaq 0xdf9c(%rip), %rsi      # 0x25e1b
leaq 0xdfa2(%rip), %rcx      # 0x25e28
movl $0x69, %edx
callq 0x3140
leaq 0xdf7b(%rip), %rdi      # 0x25e12
leaq 0xdf7d(%rip), %rsi      # 0x25e1b
leaq 0xdf83(%rip), %rcx      # 0x25e28
movl $0x41, %edx
callq 0x3140