// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_mate.c
// Function: bam_mating_core
void bam_mating_core(bamFile in, bamFile out)
{
	bam_header_t *header;
	bam1_t *b[2];
	int curr, has_prev;

	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 (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)))
				{
					uint32_t cur5, pre5;
					cur5 = (cur->core.flag&BAM_FREVERSE)? bam_calend(&cur->core, bam1_cigar(cur)) : cur->core.pos;
					pre5 = (pre->core.flag&BAM_FREVERSE)? bam_calend(&pre->core, bam1_cigar(pre)) : 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_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;
	}
	if (has_prev) bam_write1(out, b[1-curr]);
	bam_header_destroy(header);
	bam_destroy1(b[0]);
	bam_destroy1(b[1]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
callq 0x11d21
movq %rax, %r12
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x11ef4
movl $0x1, %ebx
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %rbp
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r15
movq %rax, 0x18(%rsp)
movq %r14, 0x40(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1204b
testl %eax, %eax
js 0xd8a6
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x28(%rsp)
xorl %ebx, %ebx
movl %ebx, %eax
movl %r12d, %r13d
movl $0x1, %ebx
movl $0x1, %r12d
subl %r13d, %r12d
testl %eax, %eax
je 0xd715
movq 0x10(%rsp,%r12,8), %r14
movq 0x30(%rbp), %r15
movq 0x30(%r14), %rsi
movq %r15, %rdi
callq 0x34e0
testl %eax, %eax
je 0xd71d
movq $-0x1, 0x14(%r14)
movl $0x0, 0x1c(%r14)
movq 0x8(%r14), %rax
btq $0x20, %rax
jae 0xd708
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 0x1241e
xorl %r14d, %r14d
jmp 0xd82a
movl (%r14), %eax
movl %eax, 0x14(%rbp)
movl 0x4(%r14), %ecx
movl %ecx, 0x18(%rbp)
movl (%rbp), %ecx
movl %ecx, 0x14(%r14)
movl 0x4(%rbp), %ebx
movl %ebx, 0x18(%r14)
cmpl %ecx, %eax
jne 0xd757
movq 0x8(%rbp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
testb $0xc, %cl
jne 0xd757
testb $0xc, 0xc(%r14)
je 0xd849
movl $0x0, 0x1c(%r14)
xorl %ebx, %ebx
movq %rbp, %rax
movl %ebx, 0x1c(%rax)
movq 0x8(%r14), %rcx
movq 0x8(%rbp), %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(%rbp)
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(%rbp)
je 0xd7d8
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 0xd80a
movq 0x8(%rbp), %rax
movabsq $-0xa00000001, %rcx     # imm = 0xFFFFFFF5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%rbp)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1241e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1241e
xorl %ebx, %ebx
movb $0x1, %r14b
movl %r12d, %eax
movq 0x10(%rsp,%rax,8), %rbp
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x1204b
testl %eax, %eax
jns 0xd69f
jmp 0xd892
testb $0x10, %cl
je 0xd861
shrl $0x18, %eax
addq %rax, %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x11c08
movl %eax, %ebx
movq 0x8(%r14), %rsi
btq $0x24, %rsi
jb 0xd872
movl 0x4(%r14), %eax
jmp 0xd881
shrl $0x18, %esi
addq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0x11c08
movl %eax, %ecx
subl %ebx, %ecx
movl %ecx, 0x1c(%rbp)
subl %eax, %ebx
movq %r14, %rax
jmp 0xd764
movl %r13d, %ebx
movq 0x38(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %rbp
jmp 0xd8a9
movb $0x1, %r14b
testb %r14b, %r14b
jne 0xd8bd
movq 0x10(%rsp,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x1241e
movq %r12, %rdi
callq 0x11c9f
testq %rbp, %rbp
je 0xd8db
movq 0x30(%rbp), %rdi
callq 0x32c0
movq %rbp, %rdi
callq 0x32c0
testq %r15, %r15
je 0xd8ff
movq 0x30(%r15), %rdi
callq 0x32c0
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq