// File: aydarbiktimirov[P]data-structures/src/include/persistent-segment-tree.h
// Function: CPersistentSegmentTree<int, std::plus<int>>::AddBranch(unsigned long, int const&, unsigned long, unsigned long, unsigned long, unsigned long)
void CPersistentSegmentTree<T, Operation>::AddBranch(size_t changed_position, const T &changed_value, size_t previous, size_t current_depth_count, size_t left_bound, size_t right_bound)
{ // добавление новой ветки рекурсивно сверху вниз
	size_t new_node = values.size();
	values.push_back(CNode());

	if (current_depth_count >= element_count)
	{ // добрались до листа
		values[new_node] = CNode(changed_value);
	} else {
		size_t middle_bound = (left_bound + right_bound) / 2; // добавляем поддеревья в зависимости от того, которое из поддеревьев изменилось
		if (changed_position <= middle_bound)
		{
			values[new_node].left = values.size();
			values[new_node].right = values[previous].right;
			AddBranch(changed_position, changed_value, values[previous].left, 2 * current_depth_count, left_bound, middle_bound);
		} else {
			values[new_node].left = values[previous].left;
			values[new_node].right = values.size();
			AddBranch(changed_position, changed_value, values[previous].right, 2 * current_depth_count, middle_bound + 1, right_bound);
		}

		values[new_node].value = Operation()(values[values[new_node].left].value, values[values[new_node].right].value); // вычисление значения для новой вершины
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x88(%rbp)
addq $0x30, %rdi
callq 0x49b0
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, -0x38(%rbp)
addq $0x30, %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0x54(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x54(%rbp), %rsi
movq $-0x1, %rcx
movq %rcx, %rdx
callq 0x4a80
movq -0x90(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x60b0
movq -0x88(%rbp), %rcx
movq -0x28(%rbp), %rax
cmpq 0x20(%rcx), %rax
jb 0x57ca
movq -0x18(%rbp), %rsi
leaq -0x70(%rbp), %rdi
movq $-0x1, %rcx
movq %rcx, %rdx
callq 0x4a80
movq -0x88(%rbp), %rdi
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movq -0x70(%rbp), %rcx
movq %rcx, (%rax)
movq -0x68(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x5a2e
movq -0x30(%rbp), %rax
addq 0x10(%rbp), %rax
shrq %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x78(%rbp), %rax
ja 0x58bb
movq -0x88(%rbp), %rdi
addq $0x30, %rdi
callq 0x49b0
movq -0x88(%rbp), %rdi
movq %rax, -0xb0(%rbp)
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movq -0xb0(%rbp), %rcx
movq -0x88(%rbp), %rdi
movq %rcx, 0x8(%rax)
addq $0x30, %rdi
movq -0x20(%rbp), %rsi
callq 0x4ac0
movq -0x88(%rbp), %rdi
movq 0x10(%rax), %rax
movq %rax, -0xa8(%rbp)
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movq -0xa8(%rbp), %rcx
movq -0x88(%rbp), %rdi
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
addq $0x30, %rdi
movq -0x20(%rbp), %rsi
callq 0x4ac0
movq -0x88(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %r8
shlq %r8
movq -0x30(%rbp), %r9
movq -0x78(%rbp), %rax
movq %rax, (%rsp)
callq 0x56f0
jmp 0x598e
movq -0x88(%rbp), %rdi
addq $0x30, %rdi
movq -0x20(%rbp), %rsi
callq 0x4ac0
movq -0x88(%rbp), %rdi
movq 0x8(%rax), %rax
movq %rax, -0xd0(%rbp)
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movq -0xd0(%rbp), %rcx
movq -0x88(%rbp), %rdi
movq %rcx, 0x8(%rax)
addq $0x30, %rdi
callq 0x49b0
movq -0x88(%rbp), %rdi
movq %rax, -0xc8(%rbp)
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movq -0xc8(%rbp), %rcx
movq -0x88(%rbp), %rdi
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xb8(%rbp)
addq $0x30, %rdi
movq -0x20(%rbp), %rsi
callq 0x4ac0
movq -0x88(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %r8
shlq %r8
movq -0x78(%rbp), %r9
addq $0x1, %r9
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0x56f0
movq -0x88(%rbp), %rdi
movq %rdi, %rax
addq $0x30, %rax
movq %rax, -0xf0(%rbp)
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movq -0xf0(%rbp), %rdi
movq 0x8(%rax), %rsi
callq 0x4ac0
movq -0x88(%rbp), %rdi
movq %rax, -0xe0(%rbp)
movq %rdi, %rax
addq $0x30, %rax
movq %rax, -0xe8(%rbp)
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movq -0xe8(%rbp), %rdi
movq 0x10(%rax), %rsi
callq 0x4ac0
movq -0xe0(%rbp), %rsi
movq %rax, %rdx
leaq -0x79(%rbp), %rdi
callq 0x4ae0
movq -0x88(%rbp), %rdi
movl %eax, -0xd4(%rbp)
addq $0x30, %rdi
movq -0x38(%rbp), %rsi
callq 0x4ac0
movl -0xd4(%rbp), %ecx
movl %ecx, (%rax)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)