// File: /workspace/github_c_makefile_star2_O2/sc932[P]ALE/src/ALElike.c
// Function: getOrStoreMate
bam1_t *getOrStoreMate(void **mateTree1, void **mateTree2, bam1_t *thisRead) {
    assert(thisRead != NULL);
    void *found = NULL;
    int isRead1 = (thisRead->core.flag & BAM_FREAD1) == BAM_FREAD1;
    int isRead2 = (thisRead->core.flag & BAM_FREAD2) == BAM_FREAD2;
    assert(isRead1 != isRead2 && (isRead1 || isRead2));

    found =  tfind((void*)thisRead, isRead2 ? mateTree1 : mateTree2, mateTreeCmp);

    if (found == NULL) {
        bam1_t *stored = thisRead; //bam_dup1(thisRead);
        if (stored == NULL) {
            printf("ERROR: Unable to store another alignment %d\n", mateTreeCount);
            exit(1);
        }
        void *successful = tsearch((void*) stored, isRead1 ? mateTree1 : mateTree2, mateTreeCmp);
        assert(successful != NULL);
        bam1_t *stored2 = *((bam1_t**) successful);
        if (stored != stored2) {
        	// this primary read has already been observed.
        	setMultiplePrimaryAlignments();
            // ignore this read and hope the mate picked the first one as match
        	bam_destroy1(thisRead);
        	// TODO lookup mate and keep only matching read-mate pair, possibly erasing existing entry...
        } else {
        	mateTreeCount++;
        }
        return NULL;
    } else {
        bam1_t* ret = *((bam1_t**) found);
        void *deleted = tdelete((void*) thisRead, isRead2 ? mateTree1 : mateTree2, mateTreeCmp);
        assert(deleted != NULL);
        mateTreeCount--;
        return ret;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
testq %rdx, %rdx
je 0x6ebf
movq %rdx, %rbx
movl 0xc(%rdx), %eax
testb $-0x40, %al
je 0x6ede
movl %eax, %ebp
andl $0x40, %ebp
movl %ebp, %ecx
shrl $0x6, %ecx
andl $0x80, %eax
movl %eax, %edx
shrl $0x7, %edx
cmpl %edx, %ecx
je 0x6ede
movq %rsi, %r15
movq %rdi, %r14
testl %eax, %eax
movq %rdi, %r12
cmoveq %rsi, %r12
leaq -0x124(%rip), %rdx      # 0x6d0d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3130
testq %rax, %rax
je 0x6e67
movq (%rax), %r14
leaq -0x13e(%rip), %rdx      # 0x6d0d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3280
testq %rax, %rax
je 0x6efd
decl 0x22beb(%rip)           # 0x29a50
jmp 0x6eb3
testl %ebp, %ebp
cmoveq %r15, %r14
leaq -0x167(%rip), %rdx      # 0x6d0d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x34a0
testq %rax, %rax
je 0x6f1c
cmpq %rbx, (%rax)
je 0x6eaa
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x8ac5
movq 0x30(%rbx), %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x3070
jmp 0x6eb3
incl 0x22ba0(%rip)           # 0x29a50
xorl %r14d, %r14d
movq %r14, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x177c4(%rip), %rdi     # 0x1e68a
leaq 0x16e68(%rip), %rsi     # 0x1dd35
leaq 0x177c7(%rip), %rcx     # 0x1e69b
movl $0x6fb, %edx            # imm = 0x6FB
callq 0x3230
leaq 0x177e9(%rip), %rdi     # 0x1e6ce
leaq 0x16e49(%rip), %rsi     # 0x1dd35
leaq 0x177a8(%rip), %rcx     # 0x1e69b
movl $0x6ff, %edx            # imm = 0x6FF
callq 0x3230
leaq 0x17808(%rip), %rdi     # 0x1e70c
leaq 0x16e2a(%rip), %rsi     # 0x1dd35
leaq 0x17789(%rip), %rcx     # 0x1e69b
movl $0x719, %edx            # imm = 0x719
callq 0x3230
leaq 0x177d6(%rip), %rdi     # 0x1e6f9
leaq 0x16e0b(%rip), %rsi     # 0x1dd35
leaq 0x1776a(%rip), %rcx     # 0x1e69b
movl $0x70a, %edx            # imm = 0x70A
callq 0x3230