// File: /workspace/github_cpp_makefile_star2_O3/sirak92[P]TradeMatchingEngine/src/OrdersBook.cpp
// Function: void OrdersBook::handleRequest<std::map<int, std::__cxx11::list<Order*, std::allocator<Order*>>, std::less<int>, std::allocator<std::pair<int const, std::__cxx11::list<Order*, std::allocator<Order*>>>>>>(std::map<int, std::__cxx11::list<Order*, std::allocator<Order*>>, std::less<int>, std::allocator<std::pair<int const, std::__cxx11::list<Order*, std::allocator<Order*>>>>>&, Order*, int&)
void OrdersBook::handleRequest(T &table, Order *order, int &origQnty)
{
	int currQnty, performedQnty;
	Order usedOrder;
	
	//get the range of satisfied key prices from the table.
	auto lowIt = table.lower_bound(order->m_price);
	// include in the range the equal element
	if (table.find(order->m_price) != table.end())
		lowIt++;

	// use while loops to have a change to remove some elements
	auto tableIt = table.begin();
	while (tableIt != lowIt) {
		auto ordIt = (tableIt->second).begin();
		// check the list of orders with the same price
		while (ordIt != (tableIt->second).end() && origQnty != 0) {
			currQnty = (*ordIt)->m_quantity;
			performedQnty = std::min(currQnty, origQnty);
			// the current quantity is more than the requested one
			if (currQnty > performedQnty) {
				// update the current order quantity
				(*ordIt)->m_quantity = currQnty - performedQnty;
				usedOrder = *(*ordIt);
			}
			else {
				// stored order before removing
				usedOrder = *(*ordIt);
				// current order's quantity is used, so remove it
				ordIt = (tableIt->second).erase(ordIt);
			}

			// add trade log info
			addTradeInfo(order, usedOrder, performedQnty);
			// decrease the number of aggresor order quantity
			origQnty -= performedQnty;
		}

		// remove the key from table when the list gets empty
		if (tableIt->second.empty())
			tableIt = table.erase(tableIt);
		else
			tableIt++;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, %rbx
movq %rdi, 0x30(%rsp)
leaq 0x48(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
xorl %eax, %eax
movb %al, (%rcx)
movb %al, 0x10(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rcx)
movq 0x10(%rsi), %rax
movq %rsi, 0x8(%rsp)
leaq 0x8(%rsi), %r14
testq %rax, %rax
je 0x2ae8
movl 0x30(%rbx), %ecx
movq %rax, %rdx
movq %r14, %rdi
xorl %esi, %esi
cmpl %ecx, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rdi
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x2aa0
movq %r14, %rdx
xorl %esi, %esi
cmpl %ecx, 0x20(%rax)
setl %sil
cmovgeq %rax, %rdx
movq 0x10(%rax,%rsi,8), %rax
testq %rax, %rax
jne 0x2aba
cmpq %r14, %rdx
je 0x2adb
cmpl %ecx, 0x20(%rdx)
jle 0x2ae0
movq %rdi, %r14
jmp 0x2ae8
callq 0x20e0
movq %rax, %r14
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
cmpq %r14, %rax
je 0x2c60
movq %rbx, 0x28(%rsp)
leaq 0x58(%rsp), %rbp
leaq 0x80(%rsp), %r13
movq %r14, 0x20(%rsp)
leaq 0x28(%rax), %rbx
movq 0x28(%rax), %r15
cmpq %rbx, %r15
je 0x2c36
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
je 0x2c1d
movq 0x10(%r15), %r12
movl 0x28(%r12), %ecx
cmpl %ecx, %eax
movl %ecx, %r14d
cmovll %eax, %r14d
subl %eax, %ecx
jle 0x2b76
movslq %ecx, %rax
movq %rax, 0x28(%r12)
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x2090
movq 0x30(%r12), %rax
movq %rax, 0x10(%rbp)
movups 0x20(%r12), %xmm0
movups %xmm0, (%rbp)
jmp 0x2bb5
leaq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x2090
movq 0x30(%r12), %rax
movq %rax, 0x10(%rbp)
movups 0x20(%r12), %xmm0
movups %xmm0, (%rbp)
movq (%r15), %r12
movq 0x10(%rsp), %rax
decq 0x38(%rax)
movq %r15, %rdi
callq 0x21d0
movq %r15, %rdi
callq 0x2120
movq %r12, %r15
movq %r13, 0x70(%rsp)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x336a
movq 0x10(%rbp), %rax
movq %rax, 0x20(%r13)
movups (%rbp), %xmm0
movups %xmm0, 0x10(%r13)
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movl %r14d, %ecx
callq 0x279c
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x2c08
callq 0x2120
movq 0x18(%rsp), %rcx
movl (%rcx), %eax
subl %r14d, %eax
movl %eax, (%rcx)
cmpq %rbx, %r15
jne 0x2b2e
cmpq %rbx, (%rbx)
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %rax
je 0x2c36
movq %rax, %rdi
callq 0x20e0
jmp 0x2c43
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x349a
cmpq %r14, %rax
jne 0x2b11
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2c60
callq 0x2120
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c88
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x2c8b
callq 0x2120
jmp 0x2c8b
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2c9f
callq 0x2120
movq %rbx, %rdi
callq 0x2210
nop