// File: /workspace/github_cpp_makefile_star2_O1/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 %rdi, 0x28(%rsp)
leaq 0x48(%rsp), %rdi
movq %rdi, -0x10(%rdi)
movq $0x0, -0x8(%rdi)
xorl %eax, %eax
movb %al, (%rdi)
movb %al, 0x10(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdi)
movq %rdx, 0x30(%rsp)
movl 0x30(%rdx), %r8d
movq 0x10(%rsi), %rdx
movq %rsi, 0x10(%rsp)
leaq 0x8(%rsi), %rax
movq %rax, %r9
testq %rdx, %rdx
je 0x2aff
movq %rdx, %rsi
movq %rax, %r9
xorl %edi, %edi
cmpl %r8d, 0x20(%rsi)
setl %dil
cmovgeq %rsi, %r9
movq 0x10(%rsi,%rdi,8), %rsi
testq %rsi, %rsi
jne 0x2ae7
movq %rax, %rsi
testq %rdx, %rdx
je 0x2b22
movq %rax, %rsi
xorl %edi, %edi
cmpl %r8d, 0x20(%rdx)
setl %dil
cmovgeq %rdx, %rsi
movq 0x10(%rdx,%rdi,8), %rdx
testq %rdx, %rdx
jne 0x2b0a
movq %rax, %rdx
cmpq %rax, %rsi
je 0x2b35
cmpl %r8d, 0x20(%rsi)
cmovgq %rax, %rsi
movq %rsi, %rdx
cmpq %rax, %rdx
movq %rcx, 0x20(%rsp)
je 0x2b4a
movq %r9, %rdi
callq 0x20f0
movq %rax, %r9
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
cmpq %r9, %rax
je 0x2ca9
leaq 0x58(%rsp), %rbp
leaq 0x80(%rsp), %r13
movq %r9, 0x18(%rsp)
leaq 0x28(%rax), %rbx
movq %rax, 0x8(%rsp)
movq 0x28(%rax), %r15
cmpq %rbx, %r15
movq 0x20(%rsp), %rax
je 0x2c76
movl (%rax), %r14d
testl %r14d, %r14d
je 0x2c76
movq 0x10(%r15), %r12
movl 0x28(%r12), %eax
cmpl %eax, %r14d
cmovgel %eax, %r14d
jge 0x2bd3
subl %r14d, %eax
cltq
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 0x2c12
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 0x8(%rsp), %rax
decq 0x38(%rax)
movq %r15, %rdi
callq 0x21e0
movq %r15, %rdi
callq 0x2130
movq %r12, %r15
movq %r13, 0x70(%rsp)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x33d8
movq 0x10(%rbp), %rax
movq %rax, 0x20(%r13)
movups (%rbp), %xmm0
movups %xmm0, 0x10(%r13)
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movl %r14d, %ecx
callq 0x27d8
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x2c65
callq 0x2130
movq 0x20(%rsp), %rax
subl %r14d, (%rax)
cmpq %rbx, %r15
jne 0x2b89
cmpq %rbx, (%rbx)
je 0x2c8c
movq 0x8(%rsp), %rdi
callq 0x20f0
movq 0x18(%rsp), %r9
jmp 0x2ca0
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3502
movq 0x18(%rsp), %r9
cmpq %r9, %rax
jne 0x2b6e
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2cbd
callq 0x2130
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2ce5
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x2ce8
callq 0x2130
jmp 0x2ce8
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2cfc
callq 0x2130
movq %rbx, %rdi
callq 0x2220