// File: CMU-SAFARI[P]MQSim/src/ssd/Address_Mapping_Unit_Page_Level.cpp
// Function: SSD_Components::Address_Mapping_Unit_Page_Level::allocate_page_in_plane_for_user_write(SSD_Components::NVM_Transaction_Flash_WR*, bool)
void Address_Mapping_Unit_Page_Level::allocate_page_in_plane_for_user_write(NVM_Transaction_Flash_WR* transaction, bool is_for_gc)
	{
		AddressMappingDomain* domain = domains[transaction->Stream_id];
		PPA_type old_ppa = domain->Get_ppa(ideal_mapping_table, transaction->Stream_id, transaction->LPA);

		if (old_ppa == NO_PPA)  /*this is the first access to the logical page*/
		{
			if (is_for_gc) {
				PRINT_ERROR("Unexpected mapping table status in allocate_page_in_plane_for_user_write function for a GC/WL write!")
			}
		} else {
			if (is_for_gc) {
				NVM::FlashMemory::Physical_Page_Address addr;
				Convert_ppa_to_address(old_ppa, addr);
				block_manager->Invalidate_page_in_block(transaction->Stream_id, addr);
				page_status_type page_status_in_cmt = domain->Get_page_status(ideal_mapping_table, transaction->Stream_id, transaction->LPA);
				if (page_status_in_cmt != transaction->write_sectors_bitmap)
					PRINT_ERROR("Unexpected mapping table status in allocate_page_in_plane_for_user_write for a GC/WL write!")
			} else {
				page_status_type prev_page_status = domain->Get_page_status(ideal_mapping_table, transaction->Stream_id, transaction->LPA);
				page_status_type status_intersection = transaction->write_sectors_bitmap & prev_page_status;
				//check if an update read is required
				if (status_intersection == prev_page_status) {
					NVM::FlashMemory::Physical_Page_Address addr;
					Convert_ppa_to_address(old_ppa, addr);
					block_manager->Invalidate_page_in_block(transaction->Stream_id, addr);
				} else {
					page_status_type read_pages_bitmap = status_intersection ^ prev_page_status;
					NVM_Transaction_Flash_RD *update_read_tr = new NVM_Transaction_Flash_RD(transaction->Source, transaction->Stream_id,
						count_sector_no_from_status_bitmap(read_pages_bitmap) * SECTOR_SIZE_IN_BYTE, transaction->LPA, old_ppa, transaction->UserIORequest,
						transaction->Content, transaction, read_pages_bitmap, domain->GlobalMappingTable[transaction->LPA].TimeStamp);
					Convert_ppa_to_address(old_ppa, update_read_tr->Address);
					block_manager->Read_transaction_issued(update_read_tr->Address);//Inform block manager about a new transaction as soon as the transaction's target address is determined
					block_manager->Invalidate_page_in_block(transaction->Stream_id, update_read_tr->Address);
					transaction->RelatedRead = update_read_tr;
				}
			}
		}

		/*The following lines should not be ordered with respect to the block_manager->Invalidate_page_in_block
		* function call in the above code blocks. Otherwise, GC may be invoked (due to the call to Allocate_block_....) and
		* may decide to move a page that is just invalidated.*/
		if (is_for_gc) {
			block_manager->Allocate_block_and_page_in_plane_for_gc_write(transaction->Stream_id, transaction->Address);
		} else {
			block_manager->Allocate_block_and_page_in_plane_for_user_write(transaction->Stream_id, transaction->Address);
		}
		transaction->PPA = Convert_address_to_ppa(transaction->Address);
		domain->Update_mapping_info(ideal_mapping_table, transaction->Stream_id, transaction->LPA, transaction->PPA,
			((NVM_Transaction_Flash_WR*)transaction)->write_sectors_bitmap | domain->Get_page_status(ideal_mapping_table, transaction->Stream_id, transaction->LPA));
	}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x110, %rsp            # imm = 0x110
movb %dl, %al
movq %rdi, 0x108(%rsp)
movq %rsi, 0x100(%rsp)
andb $0x1, %al
movb %al, 0xff(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0xa8(%rax), %rcx
movq 0x100(%rsp), %rdx
movzwl (%rdx), %edx
movq (%rcx,%rdx,8), %rcx
movq %rcx, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
movb 0x4c(%rax), %dl
movq 0x100(%rsp), %rax
movw (%rax), %ax
movq 0x100(%rsp), %rcx
movq 0x58(%rcx), %rcx
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x59a80
movq %rax, 0xe8(%rsp)
cmpq $-0x1, 0xe8(%rsp)
jne 0x50b2f
testb $0x1, 0xff(%rsp)
je 0x50b2a
movq 0x8950a(%rip), %rdi     # 0xd9ff0
leaq 0x59dfb(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x894f7(%rip), %rdi     # 0xd9ff0
leaq 0x5c00a(%rip), %rsi     # 0xacb0a
callq 0x8440
movq %rax, %rdi
movq 0x89449(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x894b5(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
jmp 0x50e99
testb $0x1, 0xff(%rsp)
je 0x50c49
leaq 0xd0(%rsp), %rdi
xorl %r9d, %r9d
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
callq 0x4a220
movq 0x78(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq (%rdi), %rax
leaq 0xd0(%rsp), %rdx
callq *0x88(%rax)
movq 0x78(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x100(%rsp), %rax
movw (%rax), %cx
movq (%rdi), %rax
leaq 0xd0(%rsp), %rdx
movzwl %cx, %esi
callq *0x30(%rax)
movq 0x78(%rsp), %rax
movq 0xf0(%rsp), %rdi
movb 0x4c(%rax), %dl
movq 0x100(%rsp), %rax
movw (%rax), %ax
movq 0x100(%rsp), %rcx
movq 0x58(%rcx), %rcx
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x5a0f0
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rax
movq 0x100(%rsp), %rcx
cmpq 0x98(%rcx), %rax
je 0x50c44
movq 0x893f0(%rip), %rdi     # 0xd9ff0
leaq 0x59ce1(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x893dd(%rip), %rdi     # 0xd9ff0
leaq 0x5bf55(%rip), %rsi     # 0xacb6f
callq 0x8440
movq %rax, %rdi
movq 0x8932f(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x8939b(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
jmp 0x50e97
movq 0x78(%rsp), %rax
movq 0xf0(%rsp), %rdi
movb 0x4c(%rax), %dl
movq 0x100(%rsp), %rax
movw (%rax), %ax
movq 0x100(%rsp), %rcx
movq 0x58(%rcx), %rcx
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x5a0f0
movq %rax, 0xc0(%rsp)
movq 0x100(%rsp), %rax
movq 0x98(%rax), %rax
andq 0xc0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
cmpq 0xc0(%rsp), %rax
jne 0x50d22
leaq 0xa0(%rsp), %rdi
xorl %r9d, %r9d
movl %r9d, %esi
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movl $0x0, (%rsp)
callq 0x4a220
movq 0x78(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq (%rdi), %rax
leaq 0xa0(%rsp), %rdx
callq *0x88(%rax)
movq 0x78(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x100(%rsp), %rax
movw (%rax), %cx
movq (%rdi), %rax
leaq 0xa0(%rsp), %rdx
movzwl %cx, %esi
callq *0x30(%rax)
jmp 0x50e95
movq 0xb8(%rsp), %rax
movq 0xc0(%rsp), %rcx
xorq %rcx, %rax
movq %rax, 0x98(%rsp)
movl $0xa0, %edi
callq 0x8450
movq %rax, 0x60(%rsp)
movq 0x100(%rsp), %rax
movl 0x4(%rax), %ecx
movl %ecx, 0x6c(%rsp)
movw (%rax), %ax
movw %ax, 0x72(%rsp)
movq 0x98(%rsp), %rdi
callq 0x5a170
movl %eax, 0x74(%rsp)
jmp 0x50d76
movl 0x6c(%rsp), %esi
movq 0x60(%rsp), %rdi
movw 0x72(%rsp), %ax
movl 0x74(%rsp), %ecx
shll $0x9, %ecx
movq 0x100(%rsp), %rbx
movq 0x10(%rbx), %r10
movq 0x58(%rbx), %r8
movq 0xe8(%rsp), %r9
movq 0x80(%rbx), %r11
movq 0x98(%rsp), %r14
movq 0xf0(%rsp), %rdx
movq 0x20(%rdx), %rdx
leaq (%r8,%r8,2), %r15
movq 0x10(%rdx,%r15,8), %r15
movq %rsp, %rdx
movq %r15, 0x20(%rdx)
movq %r14, 0x18(%rdx)
movq %rbx, 0x10(%rdx)
movq %r11, 0x8(%rdx)
movq %r10, (%rdx)
movzwl %ax, %edx
callq 0x95f20
jmp 0x50de7
movq 0x78(%rsp), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0xe8(%rsp), %rsi
movq 0x90(%rsp), %rdx
addq $0x38, %rdx
movq (%rdi), %rax
callq *0x88(%rax)
movq 0x78(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x90(%rsp), %rsi
addq $0x38, %rsi
callq 0x7cd70
movq 0x78(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x100(%rsp), %rax
movw (%rax), %cx
movq 0x90(%rsp), %rdx
addq $0x38, %rdx
movq (%rdi), %rax
movzwl %cx, %esi
callq *0x30(%rax)
movq 0x90(%rsp), %rcx
movq 0x100(%rsp), %rax
movq %rcx, 0x88(%rax)
jmp 0x50e95
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
callq 0x8410
jmp 0x50fed
jmp 0x50e97
jmp 0x50e99
testb $0x1, 0xff(%rsp)
je 0x50ece
movq 0x78(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x100(%rsp), %rax
movw (%rax), %cx
movq 0x100(%rsp), %rdx
addq $0x38, %rdx
movq (%rdi), %rax
movzwl %cx, %esi
callq *0x18(%rax)
jmp 0x50ef7
movq 0x78(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x100(%rsp), %rax
movw (%rax), %cx
movq 0x100(%rsp), %rdx
addq $0x38, %rdx
movq (%rdi), %rax
movzwl %cx, %esi
callq *0x10(%rax)
movq 0x78(%rsp), %rdi
movq 0x100(%rsp), %rsi
addq $0x38, %rsi
movq (%rdi), %rax
callq *0x90(%rax)
movq %rax, %rdx
movq 0x78(%rsp), %rax
movq 0x100(%rsp), %rcx
movq %rdx, 0x60(%rcx)
movq 0xf0(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movb 0x4c(%rax), %cl
movb %cl, 0x3f(%rsp)
movq 0x100(%rsp), %rcx
movw (%rcx), %cx
movw %cx, 0x5e(%rsp)
movq 0x100(%rsp), %rcx
movq 0x58(%rcx), %rcx
movq %rcx, 0x48(%rsp)
movq 0x100(%rsp), %rcx
movq 0x60(%rcx), %rcx
movq %rcx, 0x50(%rsp)
movq 0x100(%rsp), %rcx
movq 0x98(%rcx), %rcx
movq %rcx, 0x30(%rsp)
movq 0xf0(%rsp), %rdi
movb 0x4c(%rax), %dl
movq 0x100(%rsp), %rax
movw (%rax), %ax
movq 0x100(%rsp), %rcx
movq 0x58(%rcx), %rcx
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x5a0f0
movq 0x30(%rsp), %r9
movb 0x3f(%rsp), %dl
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq %rax, %rsi
movw 0x5e(%rsp), %ax
orq %rsi, %r9
andb $0x1, %dl
movzbl %dl, %esi
movzwl %ax, %edx
callq 0x5a030
addq $0x110, %rsp            # imm = 0x110
popq %rbx
popq %r14
popq %r15
retq
movq 0x88(%rsp), %rdi
callq 0x8770
nopw (%rax,%rax)