// File: meghanamreddy[P]Range-trees-and-Interval-Trees/interval_tree.cpp
// Function: IntervalTree::constructY(std::vector<Line*, std::allocator<Line*>>, IntervalNode*)
IntervalNode* IntervalTree::constructY(vector<Line*> lines, IntervalNode *node) {
	if (node == NULL)
                 node = new IntervalNode();
	int j;
	
	vector<int> endpoints = getSortedEndPointsY(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]->y2 < x_mid)
			I_left.push_back(lines[i]);
		else if (lines[i]->y1 > x_mid)
			I_right.push_back(lines[i]);
		else
			I_mid.push_back(lines[i]);
	}
	
	if (I_left.size() > 0)
		node->left = constructY(I_left, node->left);
	if (I_right.size() > 0)
		node->right = constructY(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 0x2d38
movl $0x38, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x118(%rbp)
callq 0x3750
jmp 0x2d0e
movq -0x118(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x2d38
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x20f0
jmp 0x3150
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 0x24b0
jmp 0x2d68
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 0x2ee4
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq (%rax), %rax
movl 0xc(%rax), %eax
cmpl -0x5c(%rbp), %eax
jge 0x2e62
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq %rax, %rsi
leaq -0x78(%rbp), %rdi
callq 0x3800
jmp 0x2e32
jmp 0x2ece
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x36f0
jmp 0x3150
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x3126
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq (%rax), %rax
movl 0x8(%rax), %eax
cmpl -0x5c(%rbp), %eax
jle 0x2ea6
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
callq 0x3800
jmp 0x2ea4
jmp 0x2ecc
movq -0x110(%rbp), %rdi
movslq -0xac(%rbp), %rsi
callq 0x34d0
movq %rax, %rsi
leaq -0xa8(%rbp), %rdi
callq 0x3800
jmp 0x2eca
jmp 0x2ecc
jmp 0x2ece
jmp 0x2ed0
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x2dc6
leaq -0x78(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x2f63
leaq -0xc8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x3600
jmp 0x2f05
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq -0xc8(%rbp), %rsi
callq 0x2cc0
movq %rax, -0x130(%rbp)
jmp 0x2f29
movq -0x130(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
leaq -0xc8(%rbp), %rdi
callq 0x36f0
jmp 0x2f63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x36f0
jmp 0x3126
leaq -0x90(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x2fe8
leaq -0xe0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3600
jmp 0x2f8a
movq -0x108(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
leaq -0xe0(%rbp), %rsi
callq 0x2cc0
movq %rax, -0x138(%rbp)
jmp 0x2fae
movq -0x138(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
leaq -0xe0(%rbp), %rdi
callq 0x36f0
jmp 0x2fe8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x36f0
jmp 0x3126
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3870
jmp 0x2ffe
leaq -0xa8(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x30e1
movl $0x8, %edi
callq 0x2110
movq %rax, -0x140(%rbp)
jmp 0x3027
movq -0x140(%rbp), %rdi
callq 0x3ac0
jmp 0x3035
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 0x306d
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x86f0
movq %rax, -0x158(%rbp)
jmp 0x3090
movq -0x158(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
leaq -0x100(%rbp), %rdi
callq 0x36f0
jmp 0x30e1
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x20f0
jmp 0x3126
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x36f0
jmp 0x3126
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)