// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]MQSim/src/ssd/Address_Mapping_Unit_Page_Level.cpp
// Function: SSD_Components::Address_Mapping_Unit_Page_Level::Allocate_new_page_for_gc(SSD_Components::NVM_Transaction_Flash_WR*, bool)
void Address_Mapping_Unit_Page_Level::Allocate_new_page_for_gc(NVM_Transaction_Flash_WR* transaction, bool is_translation_page)
	{
		if (is_translation_page) {
			MPPN_type mppn = domains[transaction->Stream_id]->GlobalTranslationDirectory[transaction->LPA].MPPN;
			if (mppn == NO_MPPN) {
				PRINT_ERROR("Unexpected situation occured for gc write in Allocate_new_page_for_gc function!")
			}

			allocate_page_in_plane_for_translation_write(transaction, (MVPN_type)transaction->LPA, true);
			transaction->Physical_address_determined = true;
		} else {
			if (!domains[transaction->Stream_id]->Mapping_entry_accessible(ideal_mapping_table, transaction->Stream_id, transaction->LPA)) {
				if (!domains[transaction->Stream_id]->CMT->Check_free_slot_availability()) {
					LPA_type evicted_lpa;
					CMTSlotType evictedItem = domains[transaction->Stream_id]->CMT->Evict_one_slot(evicted_lpa);
					if (evictedItem.Dirty) {
						/* In order to eliminate possible race conditions for the requests that
						* will access the evicted lpa in the near future (before the translation
						* write finishes), MQSim updates GMT (the on flash mapping table) right
						* after eviction happens.*/
						domains[transaction->Stream_id]->GlobalMappingTable[evicted_lpa].PPA = evictedItem.PPA;
						domains[transaction->Stream_id]->GlobalMappingTable[evicted_lpa].WrittenStateBitmap = evictedItem.WrittenStateBitmap;
						if (domains[transaction->Stream_id]->GlobalMappingTable[evicted_lpa].TimeStamp > CurrentTimeStamp) {
							throw std::logic_error("Unexpected situation occured in handling GMT!");
						}
						domains[transaction->Stream_id]->GlobalMappingTable[evicted_lpa].TimeStamp = CurrentTimeStamp;
						generate_flash_writeback_request_for_mapping_data(transaction->Stream_id, evicted_lpa);
					}
				}
				domains[transaction->Stream_id]->CMT->Reserve_slot_for_lpn(transaction->Stream_id, transaction->LPA);
				domains[transaction->Stream_id]->CMT->Insert_new_mapping_info(transaction->Stream_id, transaction->LPA, Convert_address_to_ppa(transaction->Address), transaction->write_sectors_bitmap);
			}

			allocate_page_in_plane_for_user_write(transaction, true);
			transaction->Physical_address_determined = true;

			//the mapping entry should be updated
			stream_id_type stream_id = transaction->Stream_id;
			Stats::total_CMT_queries++;
			Stats::total_CMT_queries_per_stream[stream_id]++;

			//either limited or unlimited mapping
			if (domains[stream_id]->Mapping_entry_accessible(ideal_mapping_table, stream_id, transaction->LPA)) {
				Stats::CMT_hits++;
				Stats::CMT_hits_per_stream[stream_id]++;
				Stats::total_writeTR_CMT_queries++;
				Stats::total_writeTR_CMT_queries_per_stream[stream_id]++;
				Stats::writeTR_CMT_hits++;
				Stats::writeTR_CMT_hits_per_stream[stream_id]++;
				domains[stream_id]->Update_mapping_info(ideal_mapping_table, stream_id, transaction->LPA, transaction->PPA, transaction->write_sectors_bitmap);
			} else { //the else block only executed for non-ideal mapping table in which CMT has a limited capacity and mapping data is read/written from/to the flash storage
				if (!domains[stream_id]->CMT->Check_free_slot_availability()) {
					LPA_type evicted_lpa;
					CMTSlotType evictedItem = domains[stream_id]->CMT->Evict_one_slot(evicted_lpa);
					if (evictedItem.Dirty) {
						/* In order to eliminate possible race conditions for the requests that
						* will access the evicted lpa in the near future (before the translation
						* write finishes), MQSim updates GMT (the on flash mapping table) right
						* after eviction happens.*/
						domains[stream_id]->GlobalMappingTable[evicted_lpa].PPA = evictedItem.PPA;
						domains[stream_id]->GlobalMappingTable[evicted_lpa].WrittenStateBitmap = evictedItem.WrittenStateBitmap;
						if (domains[stream_id]->GlobalMappingTable[evicted_lpa].TimeStamp > CurrentTimeStamp)
							throw std::logic_error("Unexpected situation occured in handling GMT!");
						domains[stream_id]->GlobalMappingTable[evicted_lpa].TimeStamp = CurrentTimeStamp;
						generate_flash_writeback_request_for_mapping_data(stream_id, evicted_lpa);
					}
				}
				domains[stream_id]->CMT->Reserve_slot_for_lpn(stream_id, transaction->LPA);
				domains[stream_id]->CMT->Insert_new_mapping_info(stream_id, transaction->LPA, transaction->PPA, transaction->write_sectors_bitmap);
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0xa8(%rdi), %rcx
movzwl (%rsi), %eax
movq (%rcx,%rax,8), %rdi
testl %edx, %edx
je 0x24aef
movq (%rdi), %rax
movq 0x58(%rbx), %rdx
movq %rdx, %rcx
shlq $0x4, %rcx
cmpl $-0x1, (%rax,%rcx)
je 0x24dc6
pushq $0x1
popq %rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21d5e
movb $0x1, 0x69(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x4c(%r14), %esi
movq 0x58(%rbx), %rcx
movzwl %ax, %edx
callq 0x26502
testb %al, %al
jne 0x24c12
movq 0xa8(%r14), %rax
movzwl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movl 0x50(%rax), %edx
cmpq %rdx, 0x18(%rax)
jb 0x24bc4
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x20d7a
cmpb $0x1, 0x10(%r15)
jne 0x24bb2
movq 0xa8(%r14), %rax
movzwl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movq 0x8(%rsp), %r15
imulq $0x18, %r15, %r12
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%rax,%r12)
movq 0x10(%rax,%r12), %r13
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
cmpq %rax, %r13
ja 0x24e07
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq 0xa8(%r14), %rcx
movzwl (%rbx), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x20(%rcx), %rcx
movq %rax, 0x10(%rcx,%r12)
movq %r14, %rdi
movq %r15, %rdx
callq 0x24e68
movq 0xa8(%r14), %rax
movzwl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movq 0x58(%rbx), %rdx
movzwl %cx, %esi
movq %rax, %rdi
callq 0x20c7c
movq (%r14), %rax
movq 0xa8(%r14), %rcx
movzwl (%rbx), %r15d
movq (%rcx,%r15,8), %rcx
movq 0x10(%rcx), %r12
movq 0x58(%rbx), %r13
leaq 0x38(%rbx), %rsi
movq %r14, %rdi
callq *0x90(%rax)
movq 0x98(%rbx), %r8
movq %r12, %rdi
movl %r15d, %esi
movq %r13, %rdx
movq %rax, %rcx
callq 0x20bba
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x232ce
movb $0x1, 0x69(%rbx)
movzwl (%rbx), %r15d
leaq 0x3eaf1(%rip), %rax     # 0x63720
incl (%rax)
leaq 0x41328(%rip), %rax     # 0x65f60
incl (%rax,%r15,4)
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rdi
movzbl 0x4c(%r14), %esi
movq 0x58(%rbx), %rcx
movl %r15d, %edx
callq 0x26502
testb %al, %al
je 0x24cd2
leaq 0x3eaa5(%rip), %rax     # 0x63708
incl (%rax)
leal (,%r15,4), %eax
leaq 0x3faec(%rip), %rcx     # 0x64760
incl (%rcx,%rax)
leaq 0x3eaaa(%rip), %rcx     # 0x63728
incl (%rcx)
leaq 0x41ad9(%rip), %rcx     # 0x66760
incl (%rcx,%rax)
leaq 0x3ea7f(%rip), %rcx     # 0x63710
incl (%rcx)
leaq 0x402c6(%rip), %rcx     # 0x64f60
incl (%rcx,%rax)
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rdi
movzbl 0x4c(%r14), %esi
movq 0x58(%rbx), %rcx
movq 0x60(%rbx), %r8
movq 0x98(%rbx), %r9
movl %r15d, %edx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x265a6
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x10(%rax), %rsi
movl 0x50(%rsi), %eax
cmpq %rax, 0x18(%rsi)
jb 0x24d83
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x20d7a
cmpb $0x1, 0x10(%r12)
jne 0x24d74
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x20(%rax), %rax
movq 0x8(%rsp), %r12
imulq $0x18, %r12, %r13
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%rax,%r13)
movq 0x10(%rax,%r13), %rbp
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
cmpq %rax, %rbp
ja 0x24e23
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq 0xa8(%r14), %rcx
movq (%rcx,%r15,8), %rcx
movq 0x20(%rcx), %rcx
movq %rax, 0x10(%rcx,%r13)
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x24e68
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x10(%rax), %rsi
movq 0x58(%rbx), %rdx
movq %rsi, %rdi
movl %r15d, %esi
callq 0x20c7c
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x10(%rax), %rdi
movq 0x58(%rbx), %rdx
movq 0x60(%rbx), %rcx
movq 0x98(%rbx), %r8
movl %r15d, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20bba
movq 0x3d223(%rip), %rbx     # 0x61ff0
leaq 0x1f7b4(%rip), %rsi     # 0x44588
movq %rbx, %rdi
callq 0x7360
leaq 0x21ae3(%rip), %rsi     # 0x468c6
movq %rbx, %rdi
callq 0x7360
movq %rax, %rdi
callq 0x71a0
movq 0x3d1d6(%rip), %rdi     # 0x61fd0
callq 0x73d0
pushq $0x1
popq %rdi
callq 0x73e0
pushq $0x10
popq %rdi
callq 0x71b0
movq %rax, %rbx
leaq 0x21afd(%rip), %rsi     # 0x46916
movq %rax, %rdi
callq 0x74b0
jmp 0x24e3d
pushq $0x10
popq %rdi
callq 0x71b0
movq %rax, %rbx
leaq 0x21ae1(%rip), %rsi     # 0x46916
movq %rax, %rdi
callq 0x74b0
movq 0x3d17c(%rip), %rsi     # 0x61fc0
movq 0x3d125(%rip), %rdx     # 0x61f70
movq %rbx, %rdi
callq 0x7590
jmp 0x24e55
movq %rax, %r14
movq %rbx, %rdi
callq 0x72a0
movq %r14, %rdi
callq 0x75d0