// File: /workspace/github_c_makefile_star2_O3/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 0x8eea
movq %rdx, %rbx
movl 0xc(%rdx), %eax
testb $-0x40, %al
je 0x8f09
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 0x8f09
movq %rsi, %r15
movq %rdi, %r14
testl %eax, %eax
movq %rdi, %r12
cmoveq %rsi, %r12
leaq -0x124(%rip), %rdx      # 0x8d38
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3130
testq %rax, %rax
je 0x8e92
movq (%rax), %r14
leaq -0x13e(%rip), %rdx      # 0x8d38
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3280
testq %rax, %rax
je 0x8f28
decl 0x23bc0(%rip)           # 0x2ca50
jmp 0x8ede
testl %ebp, %ebp
cmoveq %r15, %r14
leaq -0x167(%rip), %rdx      # 0x8d38
movq %rbx, %rdi
movq %r14, %rsi
callq 0x34a0
testq %rax, %rax
je 0x8f47
cmpq %rbx, (%rax)
je 0x8ed5
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xabc9
movq 0x30(%rbx), %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x3070
jmp 0x8ede
incl 0x23b75(%rip)           # 0x2ca50
xorl %r14d, %r14d
movq %r14, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x197c9(%rip), %rdi     # 0x226ba
leaq 0x18e6d(%rip), %rsi     # 0x21d65
leaq 0x197cc(%rip), %rcx     # 0x226cb
movl $0x6fb, %edx            # imm = 0x6FB
callq 0x3230
leaq 0x197ee(%rip), %rdi     # 0x226fe
leaq 0x18e4e(%rip), %rsi     # 0x21d65
leaq 0x197ad(%rip), %rcx     # 0x226cb
movl $0x6ff, %edx            # imm = 0x6FF
callq 0x3230
leaq 0x1980d(%rip), %rdi     # 0x2273c
leaq 0x18e2f(%rip), %rsi     # 0x21d65
leaq 0x1978e(%rip), %rcx     # 0x226cb
movl $0x719, %edx            # imm = 0x719
callq 0x3230
leaq 0x197db(%rip), %rdi     # 0x22729
leaq 0x18e10(%rip), %rsi     # 0x21d65
leaq 0x1976f(%rip), %rcx     # 0x226cb
movl $0x70a, %edx            # imm = 0x70A
callq 0x3230