// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]MQSim/src/ssd/Flash_Block_Manager.cpp
// Function: SSD_Components::Flash_Block_Manager::Allocate_Pages_in_block_and_invalidate_remaining_for_preconditioning(unsigned short, NVM::FlashMemory::Physical_Page_Address const&, std::vector<NVM::FlashMemory::Physical_Page_Address, std::allocator<NVM::FlashMemory::Physical_Page_Address>>&)
void Flash_Block_Manager::Allocate_Pages_in_block_and_invalidate_remaining_for_preconditioning(const stream_id_type stream_id, const NVM::FlashMemory::Physical_Page_Address& plane_address, std::vector<NVM::FlashMemory::Physical_Page_Address>& page_addresses)
	{
		if(page_addresses.size() > pages_no_per_block) {
			PRINT_ERROR("Error while precondition a physical block: the size of the address list is larger than the pages_no_per_block!")
		}
			
		PlaneBookKeepingType *plane_record = &plane_manager[plane_address.ChannelID][plane_address.ChipID][plane_address.DieID][plane_address.PlaneID];
		if (plane_record->Data_wf[stream_id]->Current_page_write_index > 0) {
			PRINT_ERROR("Illegal operation: the Allocate_Pages_in_block_and_invalidate_remaining_for_preconditioning function should be executed for an erased block!")
		}

		//Assign physical addresses
		for (int i = 0; i < page_addresses.size(); i++) {
			plane_record->Valid_pages_count++;
			plane_record->Free_pages_count--;
			page_addresses[i].BlockID = plane_record->Data_wf[stream_id]->BlockID;
			page_addresses[i].PageID = plane_record->Data_wf[stream_id]->Current_page_write_index++;
			plane_record->Check_bookkeeping_correctness(page_addresses[i]);
		}

		//Invalidate the remaining pages in the block
		NVM::FlashMemory::Physical_Page_Address target_address(plane_address);
		while (plane_record->Data_wf[stream_id]->Current_page_write_index < pages_no_per_block) {
			plane_record->Free_pages_count--;
			target_address.BlockID = plane_record->Data_wf[stream_id]->BlockID;
			target_address.PageID = plane_record->Data_wf[stream_id]->Current_page_write_index++;
			Invalidate_page_in_block_for_preconditioning(stream_id, target_address);
			plane_record->Check_bookkeeping_correctness(plane_address);
		}

		//Update the write frontier
		plane_record->Data_wf[stream_id] = plane_record->Get_a_free_block(stream_id, false);
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r13
movq %rdx, %r9
movq %rdi, %r14
movq (%rcx), %rdi
movq 0x8(%rcx), %rcx
movq %rcx, %rax
subq %rdi, %rax
pushq $0x18
popq %r8
cqto
idivq %r8
movl 0x34(%r14), %edx
cmpq %rdx, %rax
ja 0x30c7d
movq 0x8(%r14), %rax
movl (%r9), %edx
movl 0x4(%r9), %r8d
movq (%rax,%rdx,8), %rax
movq (%rax,%r8,8), %rax
movl 0x8(%r9), %edx
movq (%rax,%rdx,8), %r15
movq %r9, 0x8(%rsp)
movl 0xc(%r9), %eax
imulq $0xe0, %rax, %rax
movq 0x48(%r15,%rax), %rdx
movzwl %si, %r12d
movq (%rdx,%r12,8), %rdx
cmpl $0x0, 0x4(%rdx)
jne 0x30c9c
addq %rax, %r15
xorl %ebp, %ebp
xorl %ebx, %ebx
subq %rdi, %rcx
movq %rcx, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rbx, %rax
jbe 0x30bfc
movq 0x4(%r15), %xmm0
paddd 0x16c72(%rip), %xmm0    # 0x47830
movq %xmm0, 0x4(%r15)
movq 0x48(%r15), %rax
movq (%rax,%r12,8), %rax
movl (%rax), %ecx
leaq (%rdi,%rbp), %rsi
movl %ecx, 0x10(%rdi,%rbp)
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, 0x4(%rax)
movl %ecx, 0x14(%rdi,%rbp)
movq %r15, %rdi
callq 0x31882
incq %rbx
movq (%r13), %rdi
movq 0x8(%r13), %rcx
addq $0x18, %rbp
jmp 0x30b9d
movq 0x8(%rsp), %rbx
movdqu (%rbx), %xmm0
leaq 0x10(%rsp), %r13
movdqa %xmm0, (%r13)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r13)
movq 0x48(%r15), %rax
movq (%rax,%r12,8), %rax
movl 0x4(%rax), %ecx
cmpl 0x34(%r14), %ecx
jae 0x30c59
decl 0x4(%r15)
movl (%rax), %edx
movl %edx, 0x20(%rsp)
leal 0x1(%rcx), %edx
movl %edx, 0x4(%rax)
movl %ecx, 0x24(%rsp)
movq (%r14), %rax
movq %r14, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq *0x38(%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x31882
jmp 0x30c18
movq %r15, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x315c8
movq 0x48(%r15), %rcx
movq %rax, (%rcx,%r12,8)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3136c(%rip), %rbx     # 0x61ff0
leaq 0x138fd(%rip), %rsi     # 0x44588
movq %rbx, %rdi
callq 0x7360
leaq 0x16ba6(%rip), %rsi     # 0x47840
jmp 0x30cb9
movq 0x3134d(%rip), %rbx     # 0x61ff0
leaq 0x138de(%rip), %rsi     # 0x44588
movq %rbx, %rdi
callq 0x7360
leaq 0x16bf6(%rip), %rsi     # 0x478af
movq %rbx, %rdi
callq 0x7360
movq %rax, %rdi
callq 0x71a0
movq 0x31300(%rip), %rdi     # 0x61fd0
callq 0x73d0
pushq $0x1
popq %rdi
callq 0x73e0
nop