// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam_mate.c
// Function: bam_mating_core
void bam_mating_core(bamFile in, bamFile out, int remove_reads)
{
	bam_header_t *header;
	bam1_t *b[2];
	int curr, has_prev, pre_end = 0, cur_end;
	kstring_t str;

	str.l = str.m = 0; str.s = 0;
	header = bam_header_read(in);
	bam_header_write(out, header);

	b[0] = bam_init1();
	b[1] = bam_init1();
	curr = 0; has_prev = 0;
	while (bam_read1(in, b[curr]) >= 0) {
		bam1_t *cur = b[curr], *pre = b[1-curr];
		if (cur->core.tid < 0) 
        {
            if ( !remove_reads ) bam_write1(out, cur);
            continue;
        }
		cur_end = bam_calend(&cur->core, bam1_cigar(cur));
		if (cur_end > (int)header->target_len[cur->core.tid]) cur->core.flag |= BAM_FUNMAP;
		if (cur->core.flag & BAM_FSECONDARY) 
        {
            if ( !remove_reads ) bam_write1(out, cur);
            continue; // skip secondary alignments
        }
		if (has_prev) {
			if (strcmp(bam1_qname(cur), bam1_qname(pre)) == 0) { // identical pair name
				cur->core.mtid = pre->core.tid; cur->core.mpos = pre->core.pos;
				pre->core.mtid = cur->core.tid; pre->core.mpos = cur->core.pos;
				if (pre->core.tid == cur->core.tid && !(cur->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))
					&& !(pre->core.flag&(BAM_FUNMAP|BAM_FMUNMAP))) // set TLEN/ISIZE
				{
					uint32_t cur5, pre5;
					cur5 = (cur->core.flag&BAM_FREVERSE)? cur_end : cur->core.pos;
					pre5 = (pre->core.flag&BAM_FREVERSE)? pre_end : pre->core.pos;
					cur->core.isize = pre5 - cur5; pre->core.isize = cur5 - pre5;
				} else cur->core.isize = pre->core.isize = 0;
				if (pre->core.flag&BAM_FREVERSE) cur->core.flag |= BAM_FMREVERSE;
				else cur->core.flag &= ~BAM_FMREVERSE;
				if (cur->core.flag&BAM_FREVERSE) pre->core.flag |= BAM_FMREVERSE;
				else pre->core.flag &= ~BAM_FMREVERSE;
				if (cur->core.flag & BAM_FUNMAP) { pre->core.flag |= BAM_FMUNMAP; pre->core.flag &= ~BAM_FPROPER_PAIR; }
				if (pre->core.flag & BAM_FUNMAP) { cur->core.flag |= BAM_FMUNMAP; cur->core.flag &= ~BAM_FPROPER_PAIR; }
				bam_template_cigar(pre, cur, &str);
				bam_write1(out, pre);
				bam_write1(out, cur);
				has_prev = 0;
			} else { // unpaired or singleton
				pre->core.mtid = -1; pre->core.mpos = -1; pre->core.isize = 0;
				if (pre->core.flag & BAM_FPAIRED) {
					pre->core.flag |= BAM_FMUNMAP;
					pre->core.flag &= ~BAM_FMREVERSE & ~BAM_FPROPER_PAIR;
				}
				bam_write1(out, pre);
			}
		} else has_prev = 1;
		curr = 1 - curr;
		pre_end = cur_end;
	}
	if (has_prev) bam_write1(out, b[1-curr]);
	bam_header_destroy(header);
	bam_destroy1(b[0]);
	bam_destroy1(b[1]);
	free(str.s);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, 0xc(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
callq 0x235fb
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
callq 0x237d0
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r12
movq %rax, 0x20(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %rbx
movq %rax, 0x28(%rsp)
movq %r15, 0x48(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x2393b
testl %eax, %eax
js 0xe558
movq %rbx, 0x40(%rsp)
xorl %r13d, %r13d
movq %r12, 0x38(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x0, (%r12)
js 0xe393
movl $0x1, %ebp
subl %r13d, %ebp
movq 0x20(%rsp,%rbp,8), %rbx
movzbl 0xb(%r12), %esi
addq 0x30(%r12), %rsi
movq %r12, %rdi
callq 0x2343c
movl %eax, %r14d
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rcx
movslq (%r12), %r15
movq 0x8(%r12), %rax
cmpl (%rcx,%r15,4), %r14d
jle 0xe32b
movabsq $0x400000000, %rcx      # imm = 0x400000000
orq %rcx, %rax
movq %rax, 0x8(%r12)
btq $0x28, %rax
jb 0xe393
cmpl $0x0, 0x8(%rsp)
je 0xe3a3
movq 0x30(%r12), %rdi
movq 0x30(%rbx), %rsi
callq 0x35a0
testl %eax, %eax
je 0xe3b8
movq $-0x1, 0x14(%rbx)
movl $0x0, 0x1c(%rbx)
movq 0x8(%rbx), %rax
movl $0x1, 0x8(%rsp)
btq $0x20, %rax
jae 0xe38b
movabsq $-0x2a00000001, %rcx    # imm = 0xFFFFFFD5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%rbx)
movq %rbx, %r12
jmp 0xe4d4
cmpl $0x0, 0xc(%rsp)
jne 0xe4e9
jmp 0xe4dc
movl $0x1, 0x8(%rsp)
movl %r14d, 0x4(%rsp)
movl %ebp, %r13d
jmp 0xe4e9
movl (%rbx), %edx
movl %edx, 0x14(%r12)
movl 0x4(%rbx), %eax
movl %eax, 0x18(%r12)
movl %r15d, 0x14(%rbx)
movl 0x4(%r12), %ecx
movl %ecx, 0x18(%rbx)
cmpl %r15d, %edx
jne 0xe3f9
movl 0xc(%r12), %edx
testb $0xc, %dl
jne 0xe3f9
movq 0x8(%rbx), %rsi
movabsq $0xc00000000, %rdi      # imm = 0xC00000000
testq %rdi, %rsi
je 0xe508
movl $0x0, 0x1c(%rbx)
xorl %edx, %edx
movq %r12, %rax
movl %edx, 0x1c(%rax)
movq 0x8(%rbx), %rcx
movq 0x8(%r12), %rax
movq %rax, %rdx
movabsq $-0x2000000001, %rsi    # imm = 0xFFFFFFDFFFFFFFFF
andq %rsi, %rdx
addq %rcx, %rcx
movabsq $0x2000000000, %rdi     # imm = 0x2000000000
andq %rdi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%r12)
movq 0x8(%rbx), %rcx
andq %rsi, %rcx
addq %rax, %rax
andq %rdi, %rax
orq %rcx, %rax
movq %rax, 0x8(%rbx)
movabsq $0x400000000, %rcx      # imm = 0x400000000
testq %rcx, 0x8(%r12)
je 0xe47c
movabsq $-0xa00000001, %rcx     # imm = 0xFFFFFFF5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%rbx)
movabsq $0x400000000, %rcx      # imm = 0x400000000
testq %rcx, %rax
je 0xe4af
movq 0x8(%r12), %rax
movabsq $-0xa00000001, %rcx     # imm = 0xFFFFFFF5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%r12)
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
callq 0xdc0c
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x24370
movl $0x0, 0x8(%rsp)
movl %r14d, 0x4(%rsp)
movl %ebp, %r13d
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x24370
movl %r13d, %eax
movq 0x20(%rsp,%rax,8), %r12
movq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x2393b
testl %eax, %eax
jns 0xe2d3
jmp 0xe530
testb $0x10, %dl
movl %r14d, %edx
cmovel %ecx, %edx
btq $0x24, %rsi
movl 0x4(%rsp), %ecx
cmovael %eax, %ecx
movl %ecx, %eax
subl %edx, %eax
movl %eax, 0x1c(%r12)
subl %ecx, %edx
movq %rbx, %rax
jmp 0xe405
cmpl $0x0, 0x8(%rsp)
movq 0x40(%rsp), %rbx
movq 0x38(%rsp), %r12
je 0xe558
movl $0x1, %eax
subl %r13d, %eax
movq 0x20(%rsp,%rax,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x24370
movq 0x18(%rsp), %rdi
callq 0x23577
testq %r12, %r12
je 0xe579
movq 0x30(%r12), %rdi
callq 0x3350
movq %r12, %rdi
callq 0x3350
testq %rbx, %rbx
je 0xe58f
movq 0x30(%rbx), %rdi
callq 0x3350
movq %rbx, %rdi
callq 0x3350
movq 0x60(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3350