// File: /workspace/github_cpp_makefile_star2_O1/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 %rbx
subq $0x20, %rsp
movl %esi, %eax
movq %rdi, %rbx
movq $0x0, 0x8(%rsp)
addq $0x18, %rdi
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rsi
movq %rsi, %rdx
subq %rcx, %rdx
shrq $0x4, %rdx
movl %edx, %r9d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
decq %r9
testl %r9d, %r9d
js 0x4b97
movl %r9d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
shlq $0x4, %r10
movl %edx, %r11d
cmpl %eax, 0x8(%rcx,%r10)
cmovgl %r9d, %r11d
cmovnel %r11d, %edx
cmovel %r9d, %r8d
jne 0x4b6b
cmpl $-0x1, %r8d
jne 0x4bdb
cmpl $-0x1, %edx
jne 0x4bdb
movslq %eax, %rcx
shlq $0x2, %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x4ccb
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x4cd8
cmpl $-0x1, %r8d
je 0x4c3e
movl %r8d, %edx
shlq $0x4, %rdx
leaq (%rcx,%rdx), %rdi
addq $0x10, %rdi
movq -0x10(%rdi), %rax
movq %rax, 0x8(%rsp)
cmpq %rsi, %rdi
je 0x4c31
movq %rsi, %rax
subq %rdi, %rax
sarq $0x4, %rax
testq %rax, %rax
jle 0x4c31
incq %rax
addq %rdx, %rcx
addq $0x18, %rcx
movq -0x8(%rcx), %rdx
movq %rdx, -0x18(%rcx)
movl (%rcx), %edx
movl %edx, -0x10(%rcx)
decq %rax
addq $0x10, %rcx
cmpq $0x1, %rax
jg 0x4c17
addq $-0x10, %rsi
movq %rsi, 0x20(%rbx)
jmp 0x4cd8
cmpl $-0x1, %edx
je 0x4cd8
movl %edx, %r9d
shlq $0x4, %r9
leaq (%rcx,%r9), %r10
addq $0x10, %r10
movq -0x10(%r10), %r8
movq %r8, 0x8(%rsp)
movl -0x8(%r10), %edx
subl %eax, %edx
cltq
leaq (%r8,%rax,4), %rax
cmpq %rsi, %r10
je 0x4ca5
movq %rsi, %r8
subq %r10, %r8
sarq $0x4, %r8
testq %r8, %r8
jle 0x4ca5
incq %r8
addq %r9, %rcx
addq $0x18, %rcx
movq -0x8(%rcx), %r9
movq %r9, -0x18(%rcx)
movl (%rcx), %r9d
movl %r9d, -0x10(%rcx)
decq %r8
addq $0x10, %rcx
cmpq $0x1, %r8
jg 0x4c89
addq $-0x10, %rsi
movq %rsi, 0x20(%rbx)
movq %rax, 0x10(%rsp)
movl %edx, 0x18(%rsp)
cmpq 0x28(%rbx), %rsi
je 0x4ce3
movups 0x10(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x20(%rbx)
jmp 0x4cd8
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x4d2a
movq 0x8(%rsp), %rax
addq $0x20, %rsp
popq %rbx
retq
leaq 0x10(%rsp), %rdx
callq 0x4e66
jmp 0x4cd8
nop