// File: /workspace/github_c_makefile_star2_O1/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, 0x14(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
callq 0x22efa
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
callq 0x230c9
movl $0x1, %ebx
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r15
movq %rax, 0x20(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r14
movq %rax, 0x28(%rsp)
movq %r12, 0x48(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x23238
testl %eax, %eax
js 0xe221
movq %r14, 0x40(%rsp)
xorl %r13d, %r13d
movq %r15, 0x38(%rsp)
movq %r15, %r12
movl $0x0, 0x4(%rsp)
xorl %ebx, %ebx
cmpl $0x0, (%r12)
js 0xe071
movl $0x1, %r15d
subl %r13d, %r15d
movq 0x20(%rsp,%r15,8), %r14
movzbl 0xb(%r12), %esi
addq 0x30(%r12), %rsi
movq %r12, %rdi
callq 0x22d1c
movl %eax, %ebp
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movslq (%r12), %rcx
cmpl (%rax,%rcx,4), %ebp
jle 0xdffd
movabsq $0x400000000, %rax      # imm = 0x400000000
orq %rax, 0x8(%r12)
testb $0x1, 0xd(%r12)
jne 0xe071
cmpl $0x0, 0x4(%rsp)
je 0xe064
movq 0x30(%r12), %rdi
movq 0x30(%r14), %rsi
callq 0x35a0
testl %eax, %eax
je 0xe08c
movq $-0x1, 0x14(%r14)
movl $0x0, 0x1c(%r14)
movq 0x8(%r14), %rax
btq $0x20, %rax
jae 0xe057
movabsq $-0x2a00000001, %rcx    # imm = 0xFFFFFFD5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%r14)
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x23ccf
movl $0x1, 0x4(%rsp)
jmp 0xe1be
cmpl $0x0, 0x14(%rsp)
jne 0xe085
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x23ccf
movl %ebx, %ebp
jmp 0xe1c1
movl (%r14), %edx
movl %edx, 0x14(%r12)
movl 0x4(%r14), %eax
movl %eax, 0x18(%r12)
movl (%r12), %esi
movl %esi, 0x14(%r14)
movl 0x4(%r12), %ecx
movl %ecx, 0x18(%r14)
cmpl %esi, %edx
jne 0xe0d3
movl 0xc(%r12), %edx
testb $0xc, %dl
jne 0xe0d3
movq 0x8(%r14), %rsi
movabsq $0xc00000000, %rdi      # imm = 0xC00000000
testq %rdi, %rsi
je 0xe1e2
movl $0x0, 0x1c(%r14)
xorl %edx, %edx
movq %r12, %rax
movl %edx, 0x1c(%rax)
movq 0x8(%r14), %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(%r14), %rcx
andq %rsi, %rcx
addq %rax, %rax
andq %rdi, %rax
orq %rcx, %rax
movq %rax, 0x8(%r14)
movabsq $0x400000000, %rcx      # imm = 0x400000000
testq %rcx, 0x8(%r12)
je 0xe157
movabsq $-0xa00000001, %rcx     # imm = 0xFFFFFFF5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%r14)
movabsq $0x400000000, %rax      # imm = 0x400000000
testq %rax, 0x8(%r14)
je 0xe18b
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 %r14, %rdi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
callq 0xd900
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x23ccf
movq %rbx, %rdi
movq %r12, %rsi
callq 0x23ccf
movl $0x0, 0x4(%rsp)
movl %r15d, %r13d
movl %r13d, %eax
movq 0x20(%rsp,%rax,8), %r12
movq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x23238
movl %ebp, %ebx
testl %eax, %eax
jns 0xdfae
jmp 0xe205
testb $0x10, %dl
movl %ebp, %edx
cmovel %ecx, %edx
btq $0x24, %rsi
cmovael %eax, %ebx
movl %ebx, %eax
subl %edx, %eax
movl %eax, 0x1c(%r12)
subl %ebx, %edx
movq %r14, %rax
jmp 0xe0e0
cmpl $0x0, 0x4(%rsp)
sete %al
movl $0x1, %ebx
subl %r13d, %ebx
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %r15
jmp 0xe223
movb $0x1, %al
testb %al, %al
jne 0xe236
movq 0x20(%rsp,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x23ccf
movq 0x18(%rsp), %rdi
callq 0x22e78
testq %r15, %r15
je 0xe256
movq 0x30(%r15), %rdi
callq 0x3350
movq %r15, %rdi
callq 0x3350
testq %r14, %r14
je 0xe26c
movq 0x30(%r14), %rdi
callq 0x3350
movq %r14, %rdi
callq 0x3350
movq 0x60(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3350