// 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_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, %r13d
movq %rsi, %rbx
movq %rdi, %r14
movq 0xa8(%rdi), %rcx
movzwl (%rsi), %eax
movq (%rcx,%rax,8), %rbp
movq 0x58(%rsi), %rdx
cmpb $0x1, 0x4c(%rdi)
jne 0x2c4a5
movq 0x20(%rbp), %rax
leaq (%rdx,%rdx,2), %rcx
movq (%rax,%rcx,8), %r15
jmp 0x2c4b4
movq 0x10(%rbp), %rdi
movzwl %ax, %esi
callq 0x29600
movq %rax, %r15
cmpq $-0x1, %r15
je 0x2c513
testb %r13b, %r13b
je 0x2c53b
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 0x2c55a
movq 0x20(%rbp), %rax
leaq (%rdx,%rdx,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
jmp 0x2c566
testb %r13b, %r13b
je 0x2c774
movq 0x42acd(%rip), %rbx     # 0x6eff0
leaq 0x2a07e(%rip), %rsi     # 0x565a8
movq %rbx, %rdi
callq 0x7390
leaq 0x2c561(%rip), %rsi     # 0x58a9a
jmp 0x2c590
movq 0x58(%rbx), %rdx
cmpb $0x1, 0x4c(%r14)
movl %r13d, 0xc(%rsp)
jne 0x2c5b6
movq 0x20(%rbp), %rax
leaq (%rdx,%rdx,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
jmp 0x2c5c2
movzwl (%rbx), %esi
movq 0x10(%rbp), %rdi
callq 0x296d8
cmpq 0x98(%rbx), %rax
je 0x2c76a
movq 0x42a76(%rip), %rbx     # 0x6eff0
leaq 0x2a027(%rip), %rsi     # 0x565a8
movq %rbx, %rdi
callq 0x7390
leaq 0x2c56f(%rip), %rsi     # 0x58aff
movq %rbx, %rdi
callq 0x7390
movq %rax, %rdi
callq 0x71c0
movq 0x42a29(%rip), %rdi     # 0x6efd0
callq 0x7430
movl $0x1, %edi
callq 0x7440
movzwl (%rbx), %esi
movq 0x10(%rbp), %rdi
callq 0x296d8
movq 0x98(%rbx), %r13
andq %rax, %r13
cmpq %rax, %r13
jne 0x2c610
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 0x2c765
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 0x2beb8(%rip), %xmm1    # 0x584f0
movdqa 0x2bec0(%rip), %xmm2    # 0x58500
pxor %xmm3, %xmm3
movl $0x40, %ecx
movdqa 0x2bebf(%rip), %xmm4    # 0x58510
pcmpeqd %xmm5, %xmm5
movdqa 0x2bec3(%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 0x2c661
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 0x20(%rbp), %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 0x48a88
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 0x3e230
movq 0x40(%r14), %rdi
movzwl (%rbx), %esi
movq (%rdi), %rax
movq %r13, %rdx
callq *0x30(%rax)
movq %r12, 0x88(%rbx)
movl 0xc(%rsp), %r13d
movl $0x18, %eax
testb %r13b, %r13b
jne 0x2c779
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)
movb 0x4c(%r14), %r12b
movq 0x58(%rbx), %r14
movq 0x98(%rbx), %r13
movzwl (%rbx), %ebx
cmpb $0x1, %r12b
jne 0x2c7ca
movq 0x20(%rbp), %rax
leaq (%r14,%r14,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
jmp 0x2c7d8
movq 0x10(%rbp), %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x296d8
orq %r13, %rax
testb %r12b, %r12b
je 0x2c80d
movq 0x20(%rbp), %rcx
shlq $0x3, %r14
leaq (%r14,%r14,2), %rbx
movq %r15, (%rcx,%rbx)
movq %rax, 0x8(%rcx,%rbx)
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq 0x20(%rbp), %rcx
movq %rax, 0x10(%rcx,%rbx)
jmp 0x2c821
movq 0x10(%rbp), %rdi
movl %ebx, %esi
movq %r14, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x29798
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r12, %rdi
callq 0x7370
movq %rbx, %rdi
callq 0x7620
nop