// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x28(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x25fc
movl 0x30(%r14), %eax
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
movq %r15, %rdi
callq 0x30bc
movq %rax, 0x8(%rsp)
movl 0x30(%r14), %eax
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
movq %r15, %rdi
callq 0x30ea
leaq 0x8(%r15), %rcx
cmpq %rcx, %rax
movq %r14, 0x30(%rsp)
je 0x2a1d
movq 0x8(%rsp), %rdi
callq 0x2120
movq %rax, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq 0x18(%r15), %rax
leaq 0x38(%rsp), %rbp
leaq 0x70(%rsp), %r15
cmpq 0x8(%rsp), %rax
je 0x2ae4
leaq 0x28(%rax), %r13
movq %rax, 0x18(%rsp)
movq 0x28(%rax), %r14
cmpq %r13, %r14
je 0x2abb
movl (%rbx), %eax
testl %eax, %eax
je 0x2abb
movq 0x10(%r14), %rsi
movl 0x28(%rsi), %ecx
cmpl %ecx, %eax
movl %ecx, %r12d
cmovll %eax, %r12d
subl %eax, %ecx
jle 0x2a78
movslq %ecx, %rax
movq %rax, 0x28(%rsi)
movq %rbp, %rdi
callq 0x3082
jmp 0x2a8e
movq %rbp, %rdi
callq 0x3082
movq %r13, %rdi
movq %r14, %rsi
callq 0x30ae
movq %rax, %r14
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2ca8
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x27b0
movq %r15, %rdi
callq 0x20b0
subl %r12d, (%rbx)
jmp 0x2a48
cmpq %r13, (%r13)
je 0x2ad0
movq 0x18(%rsp), %rdi
callq 0x2120
jmp 0x2a30
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x3138
jmp 0x2a30
leaq 0x38(%rsp), %rdi
callq 0x20b0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2b15
jmp 0x2b15
jmp 0x2b15
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x20b0
jmp 0x2b18
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2260