// File: meghanamreddy[P]Range-trees-and-Interval-Trees/interval_tree.cpp
// Function: IntervalTree::constructX(std::vector<Line*, std::allocator<Line*>>, IntervalNode*)
IntervalNode* IntervalTree::constructX(vector<Line*> lines, IntervalNode *node) {
	if (node == NULL)
                 node = new IntervalNode();
	int j;
	vector<int> endpoints = getSortedEndPointsX(lines);
	int x_mid = endpoints[(endpoints.size()/2)-1];
	vector<Line*> I_left;
	vector<Line*> I_right;
	vector<Line*> I_mid;
	node->x_mid = x_mid;
	int i;
	for (i=0; i<lines.size(); i++) {
		if (lines[i]->x2 < x_mid)
			I_left.push_back(lines[i]);
		else if (lines[i]->x1 > x_mid)
			I_right.push_back(lines[i]);
		else
			I_mid.push_back(lines[i]);
	}

	if (I_left.size() > 0)
		node->left = constructX(I_left, node->left);
	if (I_right.size() > 0)
		node->right = constructX(I_right, node->right);
	node->I_mid = I_mid;
	if (I_mid.size() > 0) {
		BinaryRangeTree *sbbst = new BinaryRangeTree();
		node->rangetree = sbbst->buildRangeTree2(sbbst, I_mid);
	}
	return node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rsi, -0x110(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x2818
movl $0x38, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x118(%rbp)
callq 0x3750
jmp 0x27ee
movq -0x118(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x2818
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x20f0
jmp 0x2c2f
movq -0x110(%rbp), %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x3600
movq -0x108(%rbp), %rsi
movq -0x120(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x23a0
jmp 0x2848
leaq -0x58(%rbp), %rdi
callq 0x36f0
leaq -0x40(%rbp), %rdi
callq 0x37c0
movq %rax, %rsi
shrq %rsi
subq $0x1, %rsi
leaq -0x40(%rbp), %rdi
callq 0x37a0
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x37e0
leaq -0x90(%rbp), %rdi
callq 0x37e0
leaq -0xa8(%rbp), %rdi
callq 0x37e0
movl -0x5c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0xac(%rbp)
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rax
movq %rax, -0x128(%rbp)
callq 0x3440
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x29c3
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq (%rax), %rax
movl 0x4(%rax), %eax
cmpl -0x5c(%rbp), %eax
jge 0x2942
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq %rax, %rsi
leaq -0x78(%rbp), %rdi
callq 0x3800
jmp 0x2912
jmp 0x29ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x36f0
jmp 0x2c2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x2c05
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq (%rax), %rax
movl (%rax), %eax
cmpl -0x5c(%rbp), %eax
jle 0x2985
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
callq 0x3800
jmp 0x2983
jmp 0x29ab
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq %rax, %rsi
leaq -0xa8(%rbp), %rdi
callq 0x3800
jmp 0x29a9
jmp 0x29ab
jmp 0x29ad
jmp 0x29af
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x28a6
leaq -0x78(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x2a42
leaq -0xc8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x3600
jmp 0x29e4
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq -0xc8(%rbp), %rsi
callq 0x27a0
movq %rax, -0x130(%rbp)
jmp 0x2a08
movq -0x130(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
leaq -0xc8(%rbp), %rdi
callq 0x36f0
jmp 0x2a42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x36f0
jmp 0x2c05
leaq -0x90(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x2ac7
leaq -0xe0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3600
jmp 0x2a69
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
leaq -0xe0(%rbp), %rsi
callq 0x27a0
movq %rax, -0x138(%rbp)
jmp 0x2a8d
movq -0x138(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
leaq -0xe0(%rbp), %rdi
callq 0x36f0
jmp 0x2ac7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x36f0
jmp 0x2c05
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3870
jmp 0x2add
leaq -0xa8(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x2bc0
movl $0x8, %edi
callq 0x2110
movq %rax, -0x140(%rbp)
jmp 0x2b06
movq -0x140(%rbp), %rdi
callq 0x3ac0
jmp 0x2b14
movq -0x140(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x150(%rbp)
movq %rax, -0x148(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3600
jmp 0x2b4c
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x86f0
movq %rax, -0x158(%rbp)
jmp 0x2b6f
movq -0x158(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
leaq -0x100(%rbp), %rdi
callq 0x36f0
jmp 0x2bc0
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x20f0
jmp 0x2c05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x36f0
jmp 0x2c05
movq -0x18(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x36f0
leaq -0x90(%rbp), %rdi
callq 0x36f0
leaq -0x78(%rbp), %rdi
callq 0x36f0
leaq -0x40(%rbp), %rdi
callq 0x35a0
movq -0x160(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0xa8(%rbp), %rdi
callq 0x36f0
leaq -0x90(%rbp), %rdi
callq 0x36f0
leaq -0x78(%rbp), %rdi
callq 0x36f0
leaq -0x40(%rbp), %rdi
callq 0x35a0
movq -0x20(%rbp), %rdi
callq 0x21b0
nopl (%rax,%rax)