// File: /workspace/github_cpp_makefile_star2_O3/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 %r14
pushq %rbx
subq $0x18, %rsp
movl %esi, %eax
movq %rdi, %rbx
addq $0x18, %rdi
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rsi
movq %rsi, %r10
subq %rcx, %r10
shrq $0x4, %r10
movl %r10d, %r8d
addl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
decq %r8
testl %r8d, %r8d
js 0x4b81
movl %r10d, %edx
movl %r8d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
shlq $0x4, %r11
leal 0x7fffffff(%rdx), %r10d
cmpl %eax, 0x8(%rcx,%r11)
cmovgl %r8d, %r9d
jne 0x4aec
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
shlq $0x4, %r8
leaq (%rcx,%r8), %rdi
addq $0x10, %rdi
movq -0x10(%rdi), %r14
cmpq %rsi, %rdi
je 0x4b74
movq %rsi, %rax
subq %rdi, %rax
sarq $0x4, %rax
testq %rax, %rax
jle 0x4b74
incq %rax
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shlq $0x4, %rdx
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
ja 0x4b5a
addq $-0x10, %rsi
movq %rsi, 0x20(%rbx)
jmp 0x4c59
cmpl $-0x1, %r9d
je 0x4c06
movl %r9d, %r9d
shlq $0x4, %r9
leaq (%rcx,%r9), %r10
addq $0x10, %r10
movq -0x10(%r10), %r14
movl -0x8(%r10), %edx
subl %eax, %edx
cltq
leaq (%r14,%rax,4), %rax
cmpq %rsi, %r10
je 0x4be0
movq %rsi, %r8
subq %r10, %r8
sarq $0x4, %r8
testq %r8, %r8
jle 0x4be0
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
ja 0x4bc4
addq $-0x10, %rsi
movq %rsi, 0x20(%rbx)
movq %rax, 0x8(%rsp)
movl %edx, 0x10(%rsp)
cmpq 0x28(%rbx), %rsi
je 0x4c3a
movups 0x8(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x20(%rbx)
jmp 0x4c59
movl %eax, %ecx
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 0x4c46
movq %rax, %r14
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x4c59
leaq 0x8(%rsp), %rdx
callq 0x4dda
jmp 0x4c59
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4c9e
movq (%r14), %r14
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r14
retq