// File: /workspace/github_c_makefile_star2_O3/JimKent[P]linearSat/linearSat/linearSat.c
// Function: integrateOrphans
void integrateOrphans(struct alphaStore *store)
/* Make up fake reads that integrate orphans (monomers only found at beginning or end
 * of a read) better. */
{
struct alphaOrphan *orphanStarts = findOrphanStarts(store->readList, store);
struct alphaOrphan *orphanEnds = findOrphanEnds(store->readList, store);
verbose(3, "orphanStarts has %d items, orphanEnds %d\n", 
    slCount(orphanStarts), slCount(orphanEnds));

/* First look for the excellent situation where you can pair up orphans with each 
 * other.  For Y at least this happens half the time. */
struct alphaOrphan *start, *end;
for (start = orphanStarts; start != NULL; start = start->next)
    {
    struct monomerType *startType = start->monomer->type;
    if (startType == NULL)
        continue;
    for (end = orphanEnds; end != NULL && !start->paired; end = end->next)
        {
	struct monomerType *endType = end->monomer->type;
	if (endType == NULL)
	    continue;
	if (!end->paired)
	    {
	    struct monomerType *nextType = typeAfter(store, endType, 1);
	    if (nextType == startType)
	        {
		end->paired = TRUE;
		start->paired = TRUE;
		addReadOfTwo(store, end->monomer, start->monomer);
		verbose(3, "Pairing %s with %s\n", end->monomer->word, start->monomer->word);
		}
	    }
	}
    }

/* Ok, now have to just manufacture other side of orphan starts out of thin air. */
for (start = orphanStarts; start != NULL; start = start->next)
    {
    if (start->paired)
        continue;
    struct monomer *startMono = start->monomer;
    struct monomerType *startType = startMono->type;
    if (startType == NULL)
        continue;

    struct monomerType *newType = typeBefore(store, startType, 1);
    verbose(3, "Trying to find end of type %s\n", newType->name);
    struct monomer *newMono = pickRandomFromType(newType);
    addReadOfTwo(store, newMono, startMono);
    verbose(3, "Pairing new %s with start %s\n", newMono->word, startMono->word);
    }

/* Ok, now have to just manufacture other side of orphan ends out of thin air. */
for (end = orphanEnds; end != NULL; end = end->next)
    {
    if (end->paired)
        continue;
    struct monomer *endMono = end->monomer;
    struct monomerType *endType = endMono->type;
    if (endType == NULL)
        continue;

    struct monomerType *newType = typeAfter(store, endType, 1);
    verbose(3, "Trying to find start of type %s\n", newType->name);
    struct monomer *newMono = pickRandomFromType(newType);
    addReadOfTwo(store, endMono, newMono);
    verbose(3, "Pairing end %s with new %s\n", endMono->word, newMono->word);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
callq 0x5052
movq %rax, %r15
movq %rbx, (%rsp)
movq 0x10(%rbx), %rdi
callq 0x518f
movq %rax, %rbx
movq %r15, %rdi
callq 0x65f4
movl %eax, %ebp
movq %rbx, %rdi
callq 0x65f4
leaq 0x138b8(%rip), %rsi     # 0x18c92
movl $0x3, %edi
movl %ebp, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x12a31
testq %r15, %r15
je 0x550c
movl $0x1, %ebp
movq %r15, %r13
movq 0x8(%r13), %rax
movq 0x20(%rax), %r12
testq %r12, %r12
je 0x5480
testq %rbx, %rbx
je 0x5480
movq %rbx, %r14
cmpl $0x0, 0x10(%r13)
jne 0x5480
movq 0x8(%r14), %rsi
movq 0x20(%rsi), %rax
testq %rax, %rax
je 0x5478
cmpl $0x0, 0x10(%r14)
jne 0x5478
movq (%rax), %rax
testq %rax, %rax
jne 0x543b
movq (%rsp), %rax
movq 0x38(%rax), %rax
cmpq %r12, %rax
jne 0x5478
movl %ebp, 0x10(%r14)
movl %ebp, 0x10(%r13)
movq 0x8(%r13), %rdx
movq (%rsp), %rdi
callq 0x52d3
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdx
movq 0x8(%r13), %rax
movq 0x8(%rax), %rcx
movl $0x3, %edi
leaq 0x1384b(%rip), %rsi     # 0x18cbc
xorl %eax, %eax
callq 0x12a31
movq (%r14), %r14
testq %r14, %r14
jne 0x5410
movq (%r13), %r13
testq %r13, %r13
jne 0x53fb
leaq 0x1385b(%rip), %r13     # 0x18cef
movq (%rsp), %r14
cmpl $0x0, 0x10(%r15)
jne 0x5504
movq 0x8(%r15), %rbp
movq 0x20(%rbp), %rsi
testq %rsi, %rsi
je 0x5504
movq %r14, %rdi
movl $0x1, %edx
callq 0x437f
movq %rax, %r12
movq 0x8(%rax), %rdx
movl $0x3, %edi
leaq 0x13804(%rip), %rsi     # 0x18cd0
xorl %eax, %eax
callq 0x12a31
movq %r12, %rdi
callq 0x4103
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x52d3
movq 0x8(%r12), %rdx
movq 0x8(%rbp), %rcx
movl $0x3, %edi
movq %r13, %rsi
xorl %eax, %eax
callq 0x12a31
movq (%r15), %r15
testq %r15, %r15
jne 0x5498
testq %rbx, %rbx
je 0x558e
leaq 0x137f5(%rip), %r15     # 0x18d0d
leaq 0x1380f(%rip), %r12     # 0x18d2e
cmpl $0x0, 0x10(%rbx)
jne 0x5586
movq 0x8(%rbx), %r13
movq 0x20(%r13), %rax
testq %rax, %rax
je 0x5586
movq (%rax), %rbp
testq %rbp, %rbp
jne 0x5542
movq (%rsp), %rax
movq 0x38(%rax), %rbp
movq 0x8(%rbp), %rdx
movl $0x3, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x12a31
movq %rbp, %rdi
callq 0x4103
movq %rax, %rbp
movq (%rsp), %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x52d3
movq 0x8(%r13), %rdx
movq 0x8(%rbp), %rcx
movl $0x3, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x12a31
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x551f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq