// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x20, %rsp
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %rbx
leaq 0x8(%rsp), %rsi
movl %r14d, 0x8(%rsi)
movl %edx, 0xc(%rsi)
callq 0x3ae3a
testq %rax, %rax
je 0x3a872
movq %rax, %r15
movl 0x8(%rax), %ecx
cmpl %r14d, %ecx
jg 0x3a7d2
movl 0xc(%r15), %eax
cmpl %ebp, %eax
jge 0x3a8a5
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x3a922
testq %rax, %rax
je 0x3a869
movq %rax, %r15
cmpl 0x8(%r15), %r14d
jge 0x3a820
movq 0x28(%rbx), %rdi
movl $0x18, %esi
callq 0x2c325
movq %rax, %r12
movl %r14d, 0x8(%rax)
movl 0x8(%r15), %eax
movl %eax, 0xc(%r12)
movl $0x1, %edi
callq 0x32e24
movq %rax, 0x10(%r12)
jmp 0x3a84e
jle 0x3a859
movq 0x28(%rbx), %rdi
movl $0x18, %esi
callq 0x2c325
movq %rax, %r12
movl 0x8(%r15), %eax
movl %eax, 0x8(%r12)
movl %r14d, 0xc(%r12)
movq 0x10(%r15), %rax
movq %rax, 0x10(%r12)
movl %r14d, 0x8(%r15)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3ac3d
incq 0x10(%r15)
movl 0xc(%r15), %r14d
movq (%r15), %r15
testq %r15, %r15
jne 0x3a7eb
cmpl %ebp, %r14d
jge 0x3a915
movq 0x28(%rbx), %rdi
movl $0x18, %esi
callq 0x2c325
movq %rax, %r15
movl %r14d, 0x8(%rax)
movl %ebp, 0xc(%rax)
movl $0x1, %edi
callq 0x32e24
movq %rax, 0x10(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3ac3d
jmp 0x3a915
cmpl %r14d, %ecx
jge 0x3a8de
movq 0x28(%rbx), %rdi
movl $0x18, %esi
callq 0x2c325
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 0x3ac3d
movl 0xc(%r15), %eax
cmpl %ebp, %eax
jle 0x3a911
movq 0x28(%rbx), %rdi
movl $0x18, %esi
callq 0x2c325
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 0x3ac3d
incq 0x10(%r15)
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq