// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x27c0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x6c(%rbp)
leaq -0x6c(%rbp), %rsi
callq 0x4250
movq %rax, -0x120(%rbp)
jmp 0x308f
movq -0x120(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x8c(%rbp)
leaq -0x8c(%rbp), %rsi
callq 0x42b0
movq %rax, -0x130(%rbp)
jmp 0x30c0
movq -0x130(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
callq 0x42e0
movq %rax, -0x98(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x4280
testb $0x1, %al
jne 0x30f7
jmp 0x311c
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
callq 0x4310
movq %rax, -0xa0(%rbp)
jmp 0x311c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x33c5
movq -0x10(%rbp), %rdi
callq 0x4350
movq %rax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x4280
testb $0x1, %al
jne 0x3145
jmp 0x33b3
leaq -0xa8(%rbp), %rdi
callq 0x4380
movq %rax, %rdi
addq $0x8, %rdi
callq 0x43b0
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x4380
movq %rax, %rdi
addq $0x8, %rdi
callq 0x4410
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x43e0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x131(%rbp)
jne 0x31a7
jmp 0x31b7
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
setne %al
movb %al, -0x131(%rbp)
movb -0x131(%rbp), %al
testb $0x1, %al
jne 0x31c6
jmp 0x3336
leaq -0xb0(%rbp), %rdi
callq 0x4440
movq (%rax), %rax
movl 0x28(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x24(%rbp), %rdi
callq 0x4470
movq %rax, -0x140(%rbp)
jmp 0x31f1
movq -0x140(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x3253
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
subl %ecx, %eax
cltq
movq %rax, -0x150(%rbp)
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x4440
movq -0x150(%rbp), %rcx
movq -0x148(%rbp), %rdi
movq (%rax), %rax
movq %rcx, 0x28(%rax)
callq 0x4440
movq (%rax), %rsi
leaq -0x60(%rbp), %rdi
callq 0x44b0
jmp 0x3251
jmp 0x32bf
leaq -0xb0(%rbp), %rdi
callq 0x4440
movq (%rax), %rsi
leaq -0x60(%rbp), %rdi
callq 0x44b0
jmp 0x326d
leaq -0xa8(%rbp), %rdi
callq 0x4380
addq $0x8, %rax
movq %rax, -0x158(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x4550
movq -0x158(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x4500
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3790
jmp 0x32dc
movq -0x160(%rbp), %rsi
movq -0x128(%rbp), %rdi
movl -0x28(%rbp), %ecx
leaq -0x100(%rbp), %rdx
callq 0x2d00
jmp 0x32fb
leaq -0x100(%rbp), %rdi
callq 0x3770
movl -0x28(%rbp), %edx
movq -0x20(%rbp), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
jmp 0x3164
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x3770
jmp 0x33c5
leaq -0xa8(%rbp), %rdi
callq 0x4380
movq %rax, %rdi
addq $0x8, %rdi
callq 0x4570
testb $0x1, %al
jne 0x3354
jmp 0x3399
movq -0x10(%rbp), %rdi
movq -0xa8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rsi
callq 0x4590
movq %rax, -0x168(%rbp)
jmp 0x337b
movq -0x168(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x33ae
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
callq 0x4310
movq %rax, -0x118(%rbp)
jmp 0x312c
leaq -0x60(%rbp), %rdi
callq 0x3770
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x3770
movq -0x78(%rbp), %rdi
callq 0x2280
nopw (%rax,%rax)