// File: /workspace/github_cpp_makefile_star2_O1/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), %rax
movzwl (%rsi), %ecx
movq (%rax,%rcx,8), %r8
testl %edx, %edx
je 0x2dfb9
movq (%r8), %rax
movq 0x58(%rbx), %rdx
movq %rdx, %rcx
shlq $0x4, %rcx
cmpl $-0x1, (%rax,%rcx)
je 0x2e3ad
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
callq 0x2ad08
movb $0x1, 0x69(%rbx)
jmp 0x2e291
cmpb $0x0, 0x4c(%r14)
jne 0x2e13b
movq 0x10(%r8), %rax
shlq $0x38, %rcx
orq 0x58(%rbx), %rcx
movq (%rax), %rdi
movq 0x8(%rax), %r9
xorl %r11d, %r11d
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0x2e029
movq (%rax), %r10
movq %rax, %r11
cmpq 0x8(%r10), %rcx
je 0x2e029
movq %rdx, %rsi
movq %r10, %r15
movq (%r10), %r10
testq %r10, %r10
je 0x2e026
movq 0x8(%r10), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r9
movl $0x0, %r11d
cmpq %rsi, %rdx
jne 0x2e029
movq %r15, %r11
cmpq %rdi, %rcx
jne 0x2dffa
jmp 0x2e029
xorl %r11d, %r11d
testq %r11, %r11
je 0x2e033
movq (%r11), %rax
jmp 0x2e035
xorl %eax, %eax
testq %rax, %rax
je 0x2e048
movq 0x10(%rax), %rax
cmpl $0x2, 0x14(%rax)
je 0x2e13b
movq 0x10(%r8), %rsi
movl 0x50(%rsi), %eax
cmpq %rax, 0x18(%rsi)
jb 0x2e0e1
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
callq 0x29b34
cmpb $0x1, 0x10(%r15)
jne 0x2e0e1
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 0x281b4
movq %rax, %rdi
callq 0x285a6
cmpq %rax, %r13
ja 0x2e3f0
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
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 0x2e456
movq 0xa8(%r14), %rax
movzwl (%rbx), %esi
movq (%rax,%rsi,8), %rax
movq 0x10(%rax), %rdi
movq 0x58(%rbx), %rdx
callq 0x299cc
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 0x2986e
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x2c468
movb $0x1, 0x69(%rbx)
movzwl (%rbx), %r15d
leaq 0x425f6(%rip), %rax     # 0x70750
incl (%rax)
leaq 0x44e2d(%rip), %rax     # 0x72f90
incl (%rax,%r15,4)
movq 0xa8(%r14), %rax
movq (%rax,%r15,8), %r12
movb 0x4c(%r14), %r10b
movq 0x58(%rbx), %r9
testb %r10b, %r10b
jne 0x2e20f
movq 0x10(%r12), %rax
movq %r15, %rsi
shlq $0x38, %rsi
orq %r9, %rsi
movq (%rax), %r8
movq 0x8(%rax), %rcx
xorl %r13d, %r13d
movq %rsi, %rax
xorl %edx, %edx
divq %rcx
movq (%r8,%rdx,8), %rax
testq %rax, %rax
je 0x2e1eb
movq (%rax), %r11
movq %rax, %r13
cmpq 0x8(%r11), %rsi
je 0x2e1eb
movq %rdx, %rdi
movq %r11, %rbp
movq (%r11), %r11
testq %r11, %r11
je 0x2e1e8
movq 0x8(%r11), %r8
movq %r8, %rax
xorl %edx, %edx
divq %rcx
movl $0x0, %r13d
cmpq %rdi, %rdx
jne 0x2e1eb
movq %rbp, %r13
cmpq %r8, %rsi
jne 0x2e1bc
jmp 0x2e1eb
xorl %r13d, %r13d
testq %r13, %r13
je 0x2e1f6
movq (%r13), %rax
jmp 0x2e1f8
xorl %eax, %eax
testq %rax, %rax
je 0x2e2a0
movq 0x10(%rax), %rax
cmpl $0x2, 0x14(%rax)
jne 0x2e2a0
leaq 0x42522(%rip), %rax     # 0x70738
incl (%rax)
leal (,%r15,4), %eax
leaq 0x43569(%rip), %rcx     # 0x71790
incl (%rcx,%rax)
leaq 0x42527(%rip), %rcx     # 0x70758
incl (%rcx)
leaq 0x45556(%rip), %rcx     # 0x73790
incl (%rcx,%rax)
leaq 0x424fc(%rip), %rcx     # 0x70740
incl (%rcx)
leaq 0x43d43(%rip), %rcx     # 0x71f90
incl (%rcx,%rax)
movq 0x60(%rbx), %rcx
movq 0x98(%rbx), %r8
testb %r10b, %r10b
je 0x2e38f
movq 0x20(%r12), %rax
shlq $0x3, %r9
leaq (%r9,%r9,2), %rbx
movq %rcx, (%rax,%rbx)
movq %r8, 0x8(%rax,%rbx)
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq 0x20(%r12), %rcx
movq %rax, 0x10(%rcx,%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r12), %rsi
movl 0x50(%rsi), %eax
cmpq %rax, 0x18(%rsi)
jb 0x2e338
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x29b34
cmpb $0x1, 0x10(%r12)
jne 0x2e338
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 0x281b4
movq %rax, %rdi
callq 0x285a6
cmpq %rax, %rbp
ja 0x2e40e
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
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 0x2e456
movq 0xa8(%r14), %rax
leal (,%r15,8), %r12d
movq (%rax,%r12), %rax
movq 0x10(%rax), %rdi
movq 0x58(%rbx), %rdx
movl %r15d, %esi
callq 0x299cc
movq 0xa8(%r14), %rax
movq (%rax,%r12), %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 0x2986e
movq 0x10(%r12), %rdi
movl %r15d, %esi
movq %r9, %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29798
movq 0x40c3c(%rip), %rbx     # 0x6eff0
leaq 0x281ed(%rip), %rsi     # 0x565a8
movq %rbx, %rdi
callq 0x7390
leaq 0x2a62c(%rip), %rsi     # 0x589f6
movq %rbx, %rdi
callq 0x7390
movq %rax, %rdi
callq 0x71c0
movq 0x40bef(%rip), %rdi     # 0x6efd0
callq 0x7430
movl $0x1, %edi
callq 0x7440
movl $0x10, %edi
callq 0x71d0
movq %rax, %rbx
leaq 0x2a642(%rip), %rsi     # 0x58a46
movq %rax, %rdi
callq 0x7500
jmp 0x2e42a
movl $0x10, %edi
callq 0x71d0
movq %rax, %rbx
leaq 0x2a624(%rip), %rsi     # 0x58a46
movq %rax, %rdi
callq 0x7500
movq 0x40b8f(%rip), %rsi     # 0x6efc0
movq 0x40b50(%rip), %rdx     # 0x6ef88
movq %rbx, %rdi
callq 0x75e0
jmp 0x2e442
movq %rax, %r14
movq %rbx, %rdi
callq 0x72c0
movq %r14, %rdi
callq 0x7620
nop