// 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_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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
movq 0xa8(%rdi), %rcx
movzwl (%rsi), %eax
movq (%rcx,%rax,8), %r13
movq 0x58(%rsi), %rdx
cmpb $0x1, 0x4c(%rdi)
jne 0x2bf44
movq 0x20(%r13), %rax
leaq (%rdx,%rdx,2), %rcx
movq (%rax,%rcx,8), %r15
jmp 0x2bf53
movq 0x10(%r13), %rdi
movzwl %ax, %esi
callq 0x293bc
movq %rax, %r15
cmpq $-0x1, %r15
movq %r13, 0x8(%rsp)
je 0x2bfbb
testb %bpl, %bpl
je 0x2bfeb
pxor %xmm0, %xmm0
leaq 0x10(%rsp), %r12
movdqa %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x88(%rax)
movq 0x40(%r14), %rdi
movzwl (%rbx), %esi
movq (%rdi), %rax
movq %r12, %rdx
callq *0x30(%rax)
movq 0x58(%rbx), %rdx
cmpb $0x1, 0x4c(%r14)
jne 0x2c005
movq 0x20(%r13), %rax
leaq (%rdx,%rdx,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
jmp 0x2c011
movl $0x10, %eax
testb %bpl, %bpl
je 0x2c1e1
movq 0x43020(%rip), %rbx     # 0x6eff0
leaq 0x2a5d1(%rip), %rsi     # 0x565a8
movq %rbx, %rdi
callq 0x7390
leaq 0x2cab4(%rip), %rsi     # 0x58a9a
jmp 0x2c2af
movq 0x58(%rbx), %rdx
cmpb $0x1, 0x4c(%r14)
jne 0x2c028
movq 0x20(%r13), %rax
leaq (%rdx,%rdx,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
jmp 0x2c034
movzwl (%rbx), %esi
movq 0x10(%r13), %rdi
callq 0x2944e
cmpq 0x98(%rbx), %rax
jne 0x2c292
movl $0x18, %eax
jmp 0x2c1e1
movzwl (%rbx), %esi
movq 0x10(%r13), %rdi
callq 0x2944e
movq 0x98(%rbx), %r13
andq %rax, %r13
cmpq %rax, %r13
jne 0x2c082
pxor %xmm0, %xmm0
leaq 0x10(%rsp), %r12
movdqa %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x88(%rax)
movq 0x40(%r14), %rdi
movzwl (%rbx), %esi
movq (%rdi), %rax
movq %r12, %rdx
callq *0x30(%rax)
jmp 0x2c1dc
xorq %rax, %r13
movl $0xa0, %edi
callq 0x73a0
movq %rax, %r12
movl 0x4(%rbx), %esi
movzwl (%rbx), %eax
movq %r13, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x2c446(%rip), %xmm1    # 0x584f0
movdqa 0x2c44e(%rip), %xmm2    # 0x58500
pxor %xmm3, %xmm3
movl $0x40, %ecx
movdqa 0x2c44d(%rip), %xmm4    # 0x58510
pcmpeqd %xmm5, %xmm5
movdqa 0x2c451(%rip), %xmm6    # 0x58520
pxor %xmm7, %xmm7
movdqa %xmm4, %xmm8
psllq %xmm2, %xmm8
pshufd $0xee, %xmm2, %xmm9     # xmm9 = xmm2[2,3,2,3]
movdqa %xmm4, %xmm10
psllq %xmm9, %xmm10
movsd %xmm8, %xmm10           # xmm10 = xmm8[0],xmm10[1]
movdqa %xmm4, %xmm8
psllq %xmm1, %xmm8
pshufd $0xee, %xmm1, %xmm9     # xmm9 = xmm1[2,3,2,3]
movdqa %xmm4, %xmm11
psllq %xmm9, %xmm11
movsd %xmm8, %xmm11           # xmm11 = xmm8[0],xmm11[1]
andpd %xmm0, %xmm11
andpd %xmm0, %xmm10
pcmpeqd %xmm3, %xmm10
pcmpeqd %xmm3, %xmm11
movdqa %xmm10, %xmm8
shufps $0xdd, %xmm11, %xmm8    # xmm8 = xmm8[1,3],xmm11[1,3]
shufps $0x88, %xmm11, %xmm10   # xmm10 = xmm10[0,2],xmm11[0,2]
andps %xmm8, %xmm10
paddd %xmm10, %xmm7
psubd %xmm5, %xmm7
paddq %xmm6, %xmm2
paddq %xmm6, %xmm1
addq $-0x4, %rcx
jne 0x2c0d3
pshufd $0xee, %xmm7, %xmm0     # xmm0 = xmm7[2,3,2,3]
paddd %xmm7, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
paddd %xmm0, %xmm1
movd %xmm1, %ecx
shll $0x9, %ecx
movq 0x58(%rbx), %r8
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %r10
leaq (%r8,%r8,2), %r11
subq $0x8, %rsp
movzwl %ax, %edx
movq %r12, %rdi
movq %r15, %r9
pushq 0x10(%r10,%r11,8)
pushq %r13
pushq %rbx
pushq 0x80(%rbx)
pushq 0x10(%rbx)
callq 0x4802c
addq $0x30, %rsp
movq %r12, %r13
addq $0x38, %r13
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq *0x88(%rax)
movq 0x40(%r14), %rdi
movq %r13, %rsi
callq 0x3d7d8
movq 0x40(%r14), %rdi
movzwl (%rbx), %esi
movq (%rdi), %rax
movq %r13, %rdx
callq *0x30(%rax)
movq %r12, 0x88(%rbx)
movl $0x10, %eax
movq 0x40(%r14), %rdi
movzwl (%rbx), %esi
leaq 0x38(%rbx), %r15
movq (%rdi), %rcx
movq %r15, %rdx
callq *(%rcx,%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x90(%rax)
movq %rax, %r15
movq %rax, 0x60(%rbx)
movq 0x58(%rbx), %r13
movq 0x98(%rbx), %r12
cmpb $0x1, 0x4c(%r14)
jne 0x2c256
movq 0x8(%rsp), %r14
movq 0x20(%r14), %rax
shlq $0x3, %r13
leaq (,%r13,2), %rbx
addq %r13, %rbx
movq %r15, (%rax,%rbx)
orq %r12, 0x8(%rax,%rbx)
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq 0x20(%r14), %rcx
movq %rax, 0x10(%rcx,%rbx)
jmp 0x2c283
movzwl (%rbx), %ebx
movq 0x8(%rsp), %r14
movq 0x10(%r14), %rdi
movl %ebx, %esi
movq %r13, %rdx
callq 0x2944e
orq %rax, %r12
movq 0x10(%r14), %rdi
movl %ebx, %esi
movq %r13, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x294b8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x42d57(%rip), %rbx     # 0x6eff0
leaq 0x2a308(%rip), %rsi     # 0x565a8
movq %rbx, %rdi
callq 0x7390
leaq 0x2c850(%rip), %rsi     # 0x58aff
movq %rbx, %rdi
callq 0x7390
movq %rax, %rdi
callq 0x71c0
movq 0x42d0a(%rip), %rdi     # 0x6efd0
callq 0x7430
movl $0x1, %edi
callq 0x7440
movq %rax, %rbx
movq %r12, %rdi
callq 0x7370
movq %rbx, %rdi
callq 0x7620