// File: /workspace/github_c_makefile_star2_O1/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 0x8c14
movq %rdx, %rbx
movl 0xc(%rdx), %eax
testb $-0x40, %al
je 0x8c33
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 0x8c33
movq %rsi, %r15
movq %rdi, %r14
testl %eax, %eax
movq %rdi, %r12
cmoveq %rsi, %r12
leaq -0x120(%rip), %rdx      # 0x8a66
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3130
testq %rax, %rax
je 0x8bbc
movq (%rax), %r14
leaq -0x13a(%rip), %rdx      # 0x8a66
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3280
testq %rax, %rax
je 0x8c52
decl 0x23e96(%rip)           # 0x2ca50
jmp 0x8c08
testl %ebp, %ebp
cmoveq %r15, %r14
leaq -0x163(%rip), %rdx      # 0x8a66
movq %rbx, %rdi
movq %r14, %rsi
callq 0x34a0
testq %rax, %rax
je 0x8c71
cmpq %rbx, (%rax)
je 0x8bff
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xa901
movq 0x30(%rbx), %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x3070
jmp 0x8c08
incl 0x23e4b(%rip)           # 0x2ca50
xorl %r14d, %r14d
movq %r14, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19a3f(%rip), %rdi     # 0x2265a
leaq 0x190e3(%rip), %rsi     # 0x21d05
leaq 0x19a42(%rip), %rcx     # 0x2266b
movl $0x6fb, %edx            # imm = 0x6FB
callq 0x3230
leaq 0x19a64(%rip), %rdi     # 0x2269e
leaq 0x190c4(%rip), %rsi     # 0x21d05
leaq 0x19a23(%rip), %rcx     # 0x2266b
movl $0x6ff, %edx            # imm = 0x6FF
callq 0x3230
leaq 0x19a83(%rip), %rdi     # 0x226dc
leaq 0x190a5(%rip), %rsi     # 0x21d05
leaq 0x19a04(%rip), %rcx     # 0x2266b
movl $0x719, %edx            # imm = 0x719
callq 0x3230
leaq 0x19a51(%rip), %rdi     # 0x226c9
leaq 0x19086(%rip), %rsi     # 0x21d05
leaq 0x199e5(%rip), %rcx     # 0x2266b
movl $0x70a, %edx            # imm = 0x70A
callq 0x3230