// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/lib/rangeTree.c
// Function: rangeTreeAddToCoverageDepth
void rangeTreeAddToCoverageDepth(struct rbTree *tree, int start, int end)
/* Add area from start to end to a tree that is being built up to store the
 * depth of coverage.  Recover coverage back out by looking at ptToInt(range->val)
 * on tree elements. */
{
struct range q;
q.start = start;
q.end = end;

struct range *r, *existing = rbTreeFind(tree, &q);
if (existing == NULL)
    {
    lmAllocVar(tree->lm, r);
    r->start = start;
    r->end = end;
    r->val = intToPt(1);
    rbTreeAdd(tree, r);
    }
else
    {
    if (existing->start <= start && existing->end >= end)
    /* The existing one completely encompasses us */
        {
	/* Make a new section for the bit before start. */
	if (existing->start < start)
	    {
	    lmAllocVar(tree->lm, r);
	    r->start = existing->start;
	    r->end = start;
	    r->val = existing->val;
	    existing->start = start;
	    rbTreeAdd(tree, r);
	    }
	/* Make a new section for the bit after end. */
	if (existing->end > end)
	    {
	    lmAllocVar(tree->lm, r);
	    r->start = end;
	    r->end = existing->end;
	    r->val = existing->val;
	    existing->end = end;
	    rbTreeAdd(tree, r);
	    }
	/* Increment existing section in overlapping area. */
        existing->val = (char *)(existing->val) + 1;
	}
    else
    /* In general case fetch list of regions that overlap us. 
       Remaining cases to handle are: 
	     r >> e     rrrrrrrrrrrrrrrrrrrr
			     eeeeeeeeee

	     e < r           rrrrrrrrrrrrrrr
			eeeeeeeeeeee

	     r < e      rrrrrrrrrrrr
			     eeeeeeeeeeeee
     */
        {
	struct range *existingList = rangeTreeAllOverlapping(tree, start, end);

#ifdef DEBUG
	/* Make sure that list is really sorted for debugging... */
	int lastStart = existingList->start;
	for (r = existingList; r != NULL; r = r->next)
	    {
	    int start = r->start;
	    if (start < lastStart)
	        internalErr();
	    }
#endif /* DEBUG */

	int s = start, e = end;
	for (existing = existingList; existing != NULL; existing = existing->next)
	    {
	    /* Deal with start of new range that comes before existing */
	    if (s < existing->start)
	        {
		lmAllocVar(tree->lm, r);
		r->start = s;
		r->end = existing->start;
		r->val = intToPt(1);
		s = existing->start;
		rbTreeAdd(tree, r);
		}
	    else if (s > existing->start)
	        {
		lmAllocVar(tree->lm, r);
		r->start = existing->start;
		r->end = s;
		r->val = existing->val;
		existing->start = s;
		rbTreeAdd(tree, r);
		}
	    existing->val = (char *)(existing->val) + 1;
	    s = existing->end;
	    }
	if (s < e)
	/* Deal with end of new range that doesn't overlap with anything. */
	    {
	    lmAllocVar(tree->lm, r);
	    r->start = s;
	    r->end = e;
	    r->val = intToPt(1);
	    rbTreeAdd(tree, r);
	    }
	}
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %rbx
leaq 0x10(%rsp), %rsi
movl %r14d, 0x8(%rsi)
movl %edx, 0xc(%rsi)
callq 0x34fba
testq %rax, %rax
je 0x34a99
movq %rax, %r15
movl 0x8(%rax), %ecx
cmpl %r14d, %ecx
jg 0x349fd
movl 0xc(%r15), %eax
cmpl %ebp, %eax
jge 0x34ac8
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
callq 0x34b43
movq %rax, %r15
pushq $0x18
popq %r12
pushq $0x1
popq %r13
testq %r15, %r15
je 0x34a8c
cmpl 0x8(%r15), %r14d
jge 0x34a4c
movq 0x28(%rbx), %rdi
movq %r12, %rsi
callq 0x26ff4
movq %rax, %rbp
movl %r14d, 0x8(%rax)
movl 0x8(%r15), %eax
movl %eax, 0xc(%rbp)
movl %r13d, %edi
callq 0x2d69b
movq %rax, 0x10(%rbp)
jmp 0x34a74
jle 0x34a7f
movq 0x28(%rbx), %rdi
movq %r12, %rsi
callq 0x26ff4
movq %rax, %rbp
movl 0x8(%r15), %eax
movl %eax, 0x8(%rbp)
movl %r14d, 0xc(%rbp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rbp)
movl %r14d, 0x8(%r15)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x34ddc
incq 0x10(%r15)
movl 0xc(%r15), %r14d
movq (%r15), %r15
jmp 0x34a19
movl 0xc(%rsp), %ebp
cmpl %ebp, %r14d
jge 0x34b34
movq 0x28(%rbx), %rdi
pushq $0x18
popq %rsi
callq 0x26ff4
movq %rax, %r15
movl %r14d, 0x8(%rax)
movl %ebp, 0xc(%rax)
pushq $0x1
popq %rdi
callq 0x2d69b
movq %rax, 0x10(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x34ddc
jmp 0x34b34
cmpl %r14d, %ecx
jge 0x34aff
movq 0x28(%rbx), %rdi
pushq $0x18
popq %rsi
callq 0x26ff4
movl 0x8(%r15), %ecx
movl %ecx, 0x8(%rax)
movl %r14d, 0xc(%rax)
movq 0x10(%r15), %rcx
movq %rcx, 0x10(%rax)
movl %r14d, 0x8(%r15)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x34ddc
movl 0xc(%r15), %eax
cmpl %ebp, %eax
jle 0x34b30
movq 0x28(%rbx), %rdi
pushq $0x18
popq %rsi
callq 0x26ff4
movl %ebp, 0x8(%rax)
movl 0xc(%r15), %ecx
movl %ecx, 0xc(%rax)
movq 0x10(%r15), %rcx
movq %rcx, 0x10(%rax)
movl %ebp, 0xc(%r15)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x34ddc
incq 0x10(%r15)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq