// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r13
callq 0x12383
movq %rbx, (%rsp)
movq %rbx, %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rsi
callq 0x1255d
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %rbx
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, 0x30(%rsp)
movq %rax, 0x18(%rsp)
movq %r13, %rdi
movq %rbx, 0x8(%rsp)
movq %rbx, %rsi
callq 0x126b5
testl %eax, %eax
js 0xdd5a
movb $0x1, %cl
xorl %r15d, %r15d
movq 0x8(%rsp), %r12
movl %r15d, %ebx
xorl $0x1, %r15d
movq 0x10(%rsp,%r15,8), %rbp
testb $0x1, %cl
jne 0xdbb9
movq 0x30(%r12), %r14
movq 0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x34e0
testl %eax, %eax
je 0xdbd2
movq $-0x1, 0x14(%rbp)
movl $0x0, 0x1c(%rbp)
movq 0x8(%rbp), %rax
btq $0x20, %rax
jae 0xdbad
movabsq $-0x2a00000001, %rcx    # imm = 0xFFFFFFD5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%rbp)
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x12a9a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x126b5
xorl %ecx, %ecx
movq %rbp, %r12
testl %eax, %eax
jns 0xdb4f
jmp 0xdd4a
movq (%rbp), %rax
movq %rax, 0x14(%r12)
movl (%r12), %ecx
movl %ecx, 0x14(%rbp)
movl 0x4(%r12), %ebx
movl %ebx, 0x18(%rbp)
cmpl %ecx, %eax
jne 0xdc16
movq 0x8(%r12), %rax
movq %rax, %rcx
shrq $0x20, %rcx
testb $0xc, %cl
jne 0xdc16
movq 0x8(%rbp), %rsi
movabsq $0xc00000000, %rdx      # imm = 0xC00000000
testq %rdx, %rsi
je 0xdd00
movl $0x0, 0x1c(%rbp)
xorl %ebx, %ebx
movq %r12, %rax
movl %ebx, 0x1c(%rax)
movq 0x8(%rbp), %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(%rbp), %rcx
andq %rsi, %rcx
addq %rax, %rax
andq %rdi, %rax
orq %rcx, %rax
movq %rax, 0x8(%rbp)
movabsq $0x400000000, %rcx      # imm = 0x400000000
testq %rcx, 0x8(%r12)
je 0xdc99
movabsq $-0xa00000001, %rcx     # imm = 0xFFFFFFF5FFFFFFFF
andq %rcx, %rax
movabsq $0x800000000, %rcx      # imm = 0x800000000
orq %rcx, %rax
movq %rax, 0x8(%rbp)
movabsq $0x400000000, %rcx      # imm = 0x400000000
testq %rcx, %rax
je 0xdccc
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 (%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x12a9a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x12a9a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x126b5
movq %rbp, %r12
movb $0x1, %cl
testl %eax, %eax
jns 0xdb4f
jmp 0xdd5a
testb $0x10, %cl
je 0xdd1c
shrl $0x18, %eax
addq %rax, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x12268
movl %eax, %ebx
movq 0x8(%rbp), %rsi
btq $0x24, %rsi
jb 0xdd28
movl 0x4(%rbp), %eax
jmp 0xdd37
shrl $0x18, %esi
addq 0x30(%rbp), %rsi
movq %rbp, %rdi
callq 0x12268
movl %eax, %ecx
subl %ebx, %ecx
movl %ecx, 0x1c(%r12)
subl %eax, %ebx
movq %rbp, %rax
jmp 0xdc22
movl %ebx, %eax
movq 0x10(%rsp,%rax,8), %rsi
movq (%rsp), %rdi
callq 0x12a9a
movq 0x28(%rsp), %rdi
callq 0x122ff
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0xdd7f
movq 0x30(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0x32c0
movq 0x30(%rsp), %rbx
testq %rbx, %rbx
je 0xdda8
movq 0x30(%rbx), %rdi
callq 0x32c0
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq