// File: 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:
subq $0xc8, %rsp
movb %dl, %al
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
andb $0x1, %al
movb %al, 0xb7(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x38(%rsp)
testb $0x1, 0xb7(%rsp)
je 0x54de9
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movq 0xb8(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq 0xb8(%rsp), %rcx
movq 0x58(%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jne 0x54db3
movq 0x85281(%rip), %rdi     # 0xd9ff0
leaq 0x55b72(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x8526e(%rip), %rdi     # 0xd9ff0
leaq 0x57cdd(%rip), %rsi     # 0xaca66
callq 0x8440
movq %rax, %rdi
movq 0x851c0(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x8522c(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
movq 0x38(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0xb8(%rsp), %rax
movq 0x58(%rax), %rax
movl %eax, %edx
movl $0x1, %ecx
callq 0x4cc70
movq 0xb8(%rsp), %rax
movb $0x1, 0x69(%rax)
jmp 0x55467
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rcx
movq 0xb8(%rsp), %rdx
movzwl (%rdx), %edx
movq (%rcx,%rdx,8), %rdi
movb 0x4c(%rax), %dl
movq 0xb8(%rsp), %rax
movw (%rax), %ax
movq 0xb8(%rsp), %rcx
movq 0x58(%rcx), %rcx
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x599b0
testb $0x1, %al
jne 0x550c8
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movq 0xb8(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rdi
callq 0x59800
testb $0x1, %al
jne 0x55007
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movq 0xb8(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rsi
leaq 0x80(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x4ab80
testb $0x1, 0x90(%rsp)
je 0x55005
movq 0x38(%rsp), %rax
movq 0x80(%rsp), %rdx
movq 0xa8(%rax), %rcx
movq 0xb8(%rsp), %rsi
movzwl (%rsi), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x20(%rcx), %rcx
imulq $0x18, 0xa8(%rsp), %rsi
addq %rsi, %rcx
movq %rdx, (%rcx)
movq 0x88(%rsp), %rdx
movq 0xa8(%rax), %rcx
movq 0xb8(%rsp), %rsi
movzwl (%rsi), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x20(%rcx), %rcx
imulq $0x18, 0xa8(%rsp), %rsi
addq %rsi, %rcx
movq %rdx, 0x8(%rcx)
movq 0xa8(%rax), %rax
movq 0xb8(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
imulq $0x18, 0xa8(%rsp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, 0x30(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jbe 0x54fab
movl $0x10, %edi
callq 0x8200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
leaq 0x57b47(%rip), %rsi     # 0xacab6
callq 0x8610
jmp 0x54f76
movq 0x28(%rsp), %rdi
movq 0x8503e(%rip), %rsi     # 0xd9fc0
movq 0x85007(%rip), %rdx     # 0xd9f90
callq 0x8710
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
callq 0x8330
jmp 0x5546f
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movq 0xa8(%rdi), %rax
movq 0xb8(%rsp), %rdx
movzwl (%rdx), %edx
movq (%rax,%rdx,8), %rax
movq 0x20(%rax), %rax
imulq $0x18, 0xa8(%rsp), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq 0xb8(%rsp), %rax
movw (%rax), %ax
movq 0xa8(%rsp), %rdx
movzwl %ax, %esi
callq 0x55480
jmp 0x55007
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movq 0xb8(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rdi
movq 0xb8(%rsp), %rax
movw (%rax), %ax
movq 0xb8(%rsp), %rcx
movq 0x58(%rcx), %rdx
movzwl %ax, %esi
callq 0x4a960
movq 0x38(%rsp), %rdi
movq 0xa8(%rdi), %rax
movq 0xb8(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0xb8(%rsp), %rax
movw (%rax), %ax
movw %ax, 0x26(%rsp)
movq 0xb8(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0xb8(%rsp), %rsi
addq $0x38, %rsi
movq (%rdi), %rax
callq *0x90(%rax)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
movw 0x26(%rsp), %ax
movq 0xb8(%rsp), %rsi
movq 0x98(%rsi), %r8
movzwl %ax, %esi
callq 0x4a760
movq 0x38(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movl $0x1, %edx
callq 0x50a40
movq 0x38(%rsp), %rax
movq 0xb8(%rsp), %rcx
movb $0x1, 0x69(%rcx)
movq 0xb8(%rsp), %rcx
movw (%rcx), %cx
movw %cx, 0x72(%rsp)
leaq 0x86709(%rip), %rcx     # 0xdb810
movl (%rcx), %edx
addl $0x1, %edx
leaq 0x866fd(%rip), %rcx     # 0xdb810
movl %edx, (%rcx)
movzwl 0x72(%rsp), %ecx
movl %ecx, %edx
leaq 0x88f2d(%rip), %rcx     # 0xde050
movl (%rcx,%rdx,4), %esi
addl $0x1, %esi
leaq 0x88f20(%rip), %rcx     # 0xde050
movl %esi, (%rcx,%rdx,4)
movq 0xa8(%rax), %rcx
movzwl 0x72(%rsp), %edx
movq (%rcx,%rdx,8), %rdi
movb 0x4c(%rax), %dl
movw 0x72(%rsp), %ax
movq 0xb8(%rsp), %rcx
movq 0x58(%rcx), %rcx
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x599b0
testb $0x1, %al
jne 0x5516e
jmp 0x5525e
movq 0x38(%rsp), %rax
leaq 0x8667e(%rip), %rcx     # 0xdb7f8
movl (%rcx), %edx
addl $0x1, %edx
leaq 0x86672(%rip), %rcx     # 0xdb7f8
movl %edx, (%rcx)
movzwl 0x72(%rsp), %ecx
movl %ecx, %edx
leaq 0x876ba(%rip), %rcx     # 0xdc850
movl (%rcx,%rdx,4), %esi
addl $0x1, %esi
leaq 0x876ad(%rip), %rcx     # 0xdc850
movl %esi, (%rcx,%rdx,4)
leaq 0x8666b(%rip), %rcx     # 0xdb818
movl (%rcx), %edx
addl $0x1, %edx
leaq 0x8665f(%rip), %rcx     # 0xdb818
movl %edx, (%rcx)
movzwl 0x72(%rsp), %ecx
movl %ecx, %edx
leaq 0x89687(%rip), %rcx     # 0xde850
movl (%rcx,%rdx,4), %esi
addl $0x1, %esi
leaq 0x8967a(%rip), %rcx     # 0xde850
movl %esi, (%rcx,%rdx,4)
leaq 0x86620(%rip), %rcx     # 0xdb800
movl (%rcx), %edx
addl $0x1, %edx
leaq 0x86614(%rip), %rcx     # 0xdb800
movl %edx, (%rcx)
movzwl 0x72(%rsp), %ecx
movl %ecx, %edx
leaq 0x87e54(%rip), %rcx     # 0xdd050
movl (%rcx,%rdx,4), %esi
addl $0x1, %esi
leaq 0x87e47(%rip), %rcx     # 0xdd050
movl %esi, (%rcx,%rdx,4)
movq 0xa8(%rax), %rcx
movzwl 0x72(%rsp), %edx
movq (%rcx,%rdx,8), %rdi
movb 0x4c(%rax), %dl
movw 0x72(%rsp), %ax
movq 0xb8(%rsp), %rcx
movq 0x58(%rcx), %rcx
movq 0xb8(%rsp), %rsi
movq 0x60(%rsi), %r8
movq 0xb8(%rsp), %rsi
movq 0x98(%rsi), %r9
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x5a030
jmp 0x55465
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movzwl 0x72(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rdi
callq 0x59800
testb $0x1, %al
jne 0x553e6
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movzwl 0x72(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rsi
leaq 0x40(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x4ab80
testb $0x1, 0x50(%rsp)
je 0x553e4
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rdx
movq 0xa8(%rax), %rcx
movzwl 0x72(%rsp), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x20(%rcx), %rcx
imulq $0x18, 0x68(%rsp), %rsi
addq %rsi, %rcx
movq %rdx, (%rcx)
movq 0x48(%rsp), %rdx
movq 0xa8(%rax), %rcx
movzwl 0x72(%rsp), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x20(%rcx), %rcx
imulq $0x18, 0x68(%rsp), %rsi
addq %rsi, %rcx
movq %rdx, 0x8(%rcx)
movq 0xa8(%rax), %rax
movzwl 0x72(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
imulq $0x18, 0x68(%rsp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jbe 0x5539c
movl $0x10, %edi
callq 0x8200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
leaq 0x57754(%rip), %rsi     # 0xacab6
callq 0x8610
jmp 0x55369
movq (%rsp), %rdi
movq 0x84c4c(%rip), %rsi     # 0xd9fc0
movq 0x84c15(%rip), %rdx     # 0xd9f90
callq 0x8710
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
callq 0x8330
jmp 0x5546f
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movq 0xa8(%rdi), %rax
movzwl 0x72(%rsp), %edx
movq (%rax,%rdx,8), %rax
movq 0x20(%rax), %rax
imulq $0x18, 0x68(%rsp), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movw 0x72(%rsp), %ax
movq 0x68(%rsp), %rdx
movzwl %ax, %esi
callq 0x55480
jmp 0x553e6
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movzwl 0x72(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rdi
movw 0x72(%rsp), %ax
movq 0xb8(%rsp), %rcx
movq 0x58(%rcx), %rdx
movzwl %ax, %esi
callq 0x4a960
movq 0x38(%rsp), %rax
movq 0xa8(%rax), %rax
movzwl 0x72(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rdi
movw 0x72(%rsp), %ax
movq 0xb8(%rsp), %rcx
movq 0x58(%rcx), %rdx
movq 0xb8(%rsp), %rcx
movq 0x60(%rcx), %rcx
movq 0xb8(%rsp), %rsi
movq 0x98(%rsi), %r8
movzwl %ax, %esi
callq 0x4a760
jmp 0x55467
addq $0xc8, %rsp
retq
movq 0x78(%rsp), %rdi
callq 0x8770
nopl (%rax)