// File: /workspace/github_cpp_makefile_star2_O3/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
testl %edx, %edx
je 0x2da71
movq 0xa8(%r14), %rax
movzwl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq 0x58(%rbx), %rdx
movq %rdx, %rcx
shlq $0x4, %rcx
cmpl $-0x1, (%rax,%rcx)
je 0x2ddcc
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
callq 0x2a856
movb $0x1, 0x69(%rbx)
jmp 0x2ddbd
cmpb $0x0, 0x4c(%r14)
jne 0x2dbc0
movq 0xa8(%r14), %rax
movzwl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rdi
shlq $0x38, %rcx
orq 0x58(%rbx), %rcx
leaq 0x10(%rsp), %rsi
movq %rcx, (%rsi)
callq 0x30a0c
testq %rax, %rax
je 0x2dab6
movq 0x10(%rax), %rax
cmpl $0x2, 0x14(%rax)
je 0x2dbc0
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 0x2db72
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x2972e
cmpb $0x1, 0x10(%r15)
jne 0x2db60
movq 0xa8(%r14), %rax
movzwl (%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movq 0x8(%rsp), %r15
leaq (%r15,%r15,2), %r12
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%rax,%r12,8)
movq 0x10(%rax,%r12,8), %r13
callq 0x28018
movq %rax, %rdi
callq 0x2841a
cmpq %rax, %r13
ja 0x2de0f
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq 0xa8(%r14), %rcx
movzwl (%rbx), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x20(%rcx), %rcx
movq %rax, 0x10(%rcx,%r12,8)
movq %r14, %rdi
movq %r15, %rdx
callq 0x2de74
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 0x2960c
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 0x29548
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x2bf08
movb $0x1, 0x69(%rbx)
movzwl (%rbx), %r15d
leaq 0x42b71(%rip), %rax     # 0x70750
incl (%rax)
leaq 0x453a8(%rip), %rax     # 0x72f90
incl (%rax,%r15,4)
movb 0x4c(%r14), %al
testb %al, %al
jne 0x2dc36
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x10(%rax), %rdi
movq %r15, %rax
shlq $0x38, %rax
orq 0x58(%rbx), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
callq 0x30a0c
testq %rax, %rax
je 0x2dcc9
movq 0x10(%rax), %rax
cmpl $0x2, 0x14(%rax)
jne 0x2dcc9
movb 0x4c(%r14), %al
leaq 0x42afb(%rip), %rcx     # 0x70738
incl (%rcx)
leal (,%r15,4), %ecx
leaq 0x43b42(%rip), %rdx     # 0x71790
incl (%rdx,%rcx)
leaq 0x42b00(%rip), %rdx     # 0x70758
incl (%rdx)
leaq 0x45b2f(%rip), %rdx     # 0x73790
incl (%rdx,%rcx)
leaq 0x42ad5(%rip), %rdx     # 0x70740
incl (%rdx)
leaq 0x4431c(%rip), %rdx     # 0x71f90
incl (%rdx,%rcx)
movq 0xa8(%r14), %rcx
movq (%rcx,%r15,8), %r14
movq 0x58(%rbx), %rdx
movq 0x60(%rbx), %rcx
movq 0x98(%rbx), %r8
testb $0x1, %al
je 0x2ddb1
movq 0x20(%r14), %rax
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rbx
movq %rcx, (%rax,%rbx)
movq %r8, 0x8(%rax,%rbx)
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq 0x20(%r14), %rcx
movq %rax, 0x10(%rcx,%rbx)
jmp 0x2ddbd
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x10(%rax), %rsi
movl 0x50(%rsi), %eax
cmpq %rax, 0x18(%rsi)
jb 0x2dd7a
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x2972e
cmpb $0x1, 0x10(%r12)
jne 0x2dd6b
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x20(%rax), %rax
movq 0x8(%rsp), %r12
leaq (%r12,%r12,2), %r13
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%rax,%r13,8)
movq 0x10(%rax,%r13,8), %rbp
callq 0x28018
movq %rax, %rdi
callq 0x2841a
cmpq %rax, %rbp
ja 0x2de2d
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq 0xa8(%r14), %rcx
movq (%rcx,%r15,8), %rcx
movq 0x20(%rcx), %rcx
movq %rax, 0x10(%rcx,%r13,8)
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x2de74
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %rax
movq 0x10(%rax), %rsi
movq 0x58(%rbx), %rdx
movq %rsi, %rdi
movl %r15d, %esi
callq 0x2960c
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
callq 0x29548
jmp 0x2ddbd
movq 0x10(%r14), %rdi
movl %r15d, %esi
callq 0x294b8
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4121d(%rip), %rbx     # 0x6eff0
leaq 0x287ce(%rip), %rsi     # 0x565a8
movq %rbx, %rdi
callq 0x7390
leaq 0x2ac0d(%rip), %rsi     # 0x589f6
movq %rbx, %rdi
callq 0x7390
movq %rax, %rdi
callq 0x71c0
movq 0x411d0(%rip), %rdi     # 0x6efd0
callq 0x7430
movl $0x1, %edi
callq 0x7440
movl $0x10, %edi
callq 0x71d0
movq %rax, %rbx
leaq 0x2ac23(%rip), %rsi     # 0x58a46
movq %rax, %rdi
callq 0x7500
jmp 0x2de49
movl $0x10, %edi
callq 0x71d0
movq %rax, %rbx
leaq 0x2ac05(%rip), %rsi     # 0x58a46
movq %rax, %rdi
callq 0x7500
movq 0x41170(%rip), %rsi     # 0x6efc0
movq 0x41129(%rip), %rdx     # 0x6ef80
movq %rbx, %rdi
callq 0x75e0
jmp 0x2de61
movq %rax, %r14
movq %rbx, %rdi
callq 0x72c0
movq %r14, %rdi
callq 0x7620