// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x8e30
jmp 0x8e4f
leaq 0x278e3(%rip), %rdi     # 0x3071a
leaq 0x26f87(%rip), %rsi     # 0x2fdc5
movl $0x6fb, %edx            # imm = 0x6FB
leaq 0x278e1(%rip), %rcx     # 0x3072b
callq 0x3220
movq $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xffff, %rax           # imm = 0xFFFF
andl $0x40, %eax
cmpl $0x40, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xffff, %rax           # imm = 0xFFFF
andl $0x80, %eax
cmpl $0x80, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0x8eb7
cmpl $0x0, -0x2c(%rbp)
jne 0x8eb5
cmpl $0x0, -0x30(%rbp)
je 0x8eb7
jmp 0x8ed6
leaq 0x278a0(%rip), %rdi     # 0x3075e
leaq 0x26f00(%rip), %rsi     # 0x2fdc5
movl $0x6ff, %edx            # imm = 0x6FF
leaq 0x2785a(%rip), %rcx     # 0x3072b
callq 0x3220
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x8eee
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x8ef6
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
leaq -0x275(%rip), %rdx      # 0x8c90
callq 0x3120
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x8fff
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x8f46
movl 0x31212(%rip), %esi     # 0x3a140
leaq 0x27854(%rip), %rdi     # 0x30789
movb $0x0, %al
callq 0x3200
movl $0x1, %edi
callq 0x35b0
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x8f5e
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x8f66
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0x2e5(%rip), %rdx      # 0x8c90
callq 0x3490
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x8f87
jmp 0x8fa6
leaq 0x27828(%rip), %rdi     # 0x307b6
leaq 0x26e30(%rip), %rsi     # 0x2fdc5
movl $0x70a, %edx            # imm = 0x70A
leaq 0x2778a(%rip), %rcx     # 0x3072b
callq 0x3220
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x8fe3
movb $0x0, %al
callq 0xbf20
cmpq $0x0, -0x20(%rbp)
je 0x8fdf
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x3050
movq -0x20(%rbp), %rdi
callq 0x3050
jmp 0x8fe1
jmp 0x8ff2
movl 0x31157(%rip), %eax     # 0x3a140
addl $0x1, %eax
movl %eax, 0x3114e(%rip)     # 0x3a140
movq $0x0, -0x8(%rbp)
jmp 0x908a
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x9025
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x9030
movq -0x18(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
leaq -0x3b2(%rip), %rdx      # 0x8c90
callq 0x3270
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x9054
jmp 0x9073
leaq 0x2776e(%rip), %rdi     # 0x307c9
leaq 0x26d63(%rip), %rsi     # 0x2fdc5
movl $0x719, %edx            # imm = 0x719
leaq 0x276bd(%rip), %rcx     # 0x3072b
callq 0x3220
movl 0x310c7(%rip), %eax     # 0x3a140
addl $-0x1, %eax
movl %eax, 0x310be(%rip)     # 0x3a140
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)