// File: SNUCSE-CTA[P]GI/src/memory.cpp
// Function: Memory::getLLArray(int)
int32_t* Memory::getLLArray(int32_t aSize)
{
	#ifdef DEBUG
	cout << __PRETTY_FUNCTION__ << endl;
	#endif

	int32_t exact = -1; 	//an array of size aSize
	int32_t bigger = -1;	//an array bigger than aSize
	int32_t* p = NULL;	//output

	//find the reusable array in the pool
	for(int32_t i = llPool.size() - 1; i >= 0; --i) {
		auto slot = llPool[i];
		if( slot.second == aSize ) {
			exact = i;
			break;
		}
		else if( slot.second > aSize ) {
			bigger = i;
		}
	}

	if(exact == -1 && bigger == -1) { //if there is no array reusable
		p = new int32_t[aSize];
		createdArray.push_back(p);

		//cout << "Case: create " << aSize << endl;
	}
	else if(exact != -1) { //if there is an array of size aSize
		p = llPool[exact].first;
		llPool.erase(llPool.begin() + exact);

		//cout << "Case: exact " << aSize << " is erased" << endl;
	}
	else if(bigger != -1) { //if there is a bigger array
		//split the array
		p = llPool[bigger].first;
		int32_t rest = llPool[bigger].second - aSize;
		int32_t* restP = p + aSize;
		llPool.erase(llPool.begin() + bigger);
		llPool.push_back(make_pair(restP, rest));

		//cout << "Case: bigger " << aSize << " is erased, " << rest << " is added" << endl;
	}
	
	return p;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x20(%rbp)
addq $0x18, %rdi
callq 0x7c10
subq $0x1, %rax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jl 0x7869
movq -0xb0(%rbp), %rdi
addq $0x18, %rdi
movslq -0x24(%rbp), %rsi
callq 0x7c30
movq (%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0xc(%rbp), %eax
jne 0x784c
movl -0x24(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x7869
movl -0x30(%rbp), %eax
cmpl -0xc(%rbp), %eax
jle 0x785a
movl -0x24(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x785c
jmp 0x785e
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x7813
cmpl $-0x1, -0x10(%rbp)
jne 0x78b3
cmpl $-0x1, -0x14(%rbp)
jne 0x78b3
movslq -0xc(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3040
movq -0xb0(%rbp), %rdi
movq %rax, -0x20(%rbp)
leaq -0x20(%rbp), %rsi
callq 0x7c50
jmp 0x7a14
cmpl $-0x1, -0x10(%rbp)
je 0x792d
movq -0xb0(%rbp), %rdi
addq $0x18, %rdi
movslq -0x10(%rbp), %rsi
callq 0x7c30
movq -0xb0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq %rdi, %rax
addq $0x18, %rax
movq %rax, -0xb8(%rbp)
addq $0x18, %rdi
callq 0x7d30
movq %rax, -0x50(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7d60
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x7da0
movq -0xb8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x7cc0
movq %rax, -0x58(%rbp)
jmp 0x7a12
cmpl $-0x1, -0x14(%rbp)
je 0x7a10
movq -0xb0(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0xc0(%rbp)
movslq -0x14(%rbp), %rsi
callq 0x7c30
movq -0xc0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movslq -0x14(%rbp), %rsi
callq 0x7c30
movq -0xc0(%rbp), %rdi
movl 0x8(%rax), %eax
movl -0xc(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x5c(%rbp)
movq -0x20(%rbp), %rax
movslq -0xc(%rbp), %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, -0x68(%rbp)
callq 0x7d30
movq %rax, -0x80(%rbp)
movslq -0x14(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x7d60
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x7da0
movq -0xc0(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x7cc0
movq %rax, -0x88(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x7e10
movq -0xc0(%rbp), %rdi
movl %edx, -0xa0(%rbp)
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x90(%rbp)
leaq -0x98(%rbp), %rsi
callq 0x7de0
jmp 0x7a12
jmp 0x7a14
movq -0x20(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)