// File: /workspace/github_cpp_makefile_star2_O2/narges64[P]SIMBA/ftl.cpp
// Function: full_write_preconditioning(ssd_info*, bool)
void full_write_preconditioning(ssd_info * ssd, bool seq){
	unsigned int total_size = ssd->parameter->lun_num * ssd->parameter->plane_lun *
						ssd->parameter->block_plane * ssd->parameter->page_block;
	total_size = total_size * (1-ssd->parameter->overprovide);
	cerr << "full write for total size " << total_size << " page ";
	// add write to table
	int lpn = 0;
	int ppn = -1;
	local * location = new local(0,0,0,0,0);
	for (int i = 0; i <  total_size; i++){
		if (!seq){
			if ((invalid_old_page(ssd, lpn, location) != SUCCESS)){
				#if DEBUG
				cout << "fail in invalid old page" << endl;
				#endif 
			}
			ppn = get_new_ppn (ssd, lpn, location, true);
			if (ppn == -1) {
				cout << "fail in precondition " << endl;
			}
		}else {
			if ((invalid_old_page(ssd, lpn, location) == SUCCESS)){
				#if DEBUG 
				cout << "seqential should not see the old page " << endl;
				#endif 
			}
			ppn = get_new_ppn(ssd, lpn, NULL, true);
			if (ppn == -1)
				cout << "fail in precondition 2" << endl;
		}
		if (write_page(ssd, lpn, ppn) == FAIL)
			cout << "full write precondition fail in write_page " << endl;
		bool gc = check_need_gc(ssd, ppn);
		if (gc) {
			if (seq) cerr << "should not have GC in sequential preconditioning " << endl;
			local * location = new local(0,0,0);
			find_location(ssd, ppn, location);
			pre_process_gc(ssd, location);
			delete location;
 		}
		if(seq) {
			lpn++;
			lpn = lpn % total_size;
		}else
			lpn = rand() % total_size;
	}
	cerr << "is complete. erase count: " <<  ssd->stats->flash_erase_count
				<< ". move count: " << ssd->stats->gc_moved_page << endl;
	ssd->stats->flash_erase_count  = 0;
	ssd->stats->gc_moved_page = 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x58(%rdi), %rax
movl 0x22c(%rax), %ecx
imull 0x88(%rax), %ecx
imull 0x230(%rax), %ecx
imull 0x234(%rax), %ecx
cvtsi2ss %rcx, %xmm0
movss 0x4e0b(%rip), %xmm1     # 0x9040
subss 0x25c(%rax), %xmm1
movl %esi, %ebp
mulss %xmm0, %xmm1
cvttss2si %xmm1, %r13
movq 0x9da1(%rip), %rdi      # 0xdff0
leaq 0x528a(%rip), %rsi      # 0x94e0
callq 0x2140
movl %r13d, %esi
movq %rax, %rdi
callq 0x20d0
leaq 0x580e(%rip), %rsi      # 0x9a7b
movq %rax, %rdi
callq 0x2140
pushq $0x18
popq %rdi
callq 0x2160
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andl $0x0, 0x10(%rax)
xorl %r14d, %r14d
movl %r13d, %r12d
movq %rax, 0x8(%rsp)
movl %ebp, 0x4(%rsp)
movq %r13, 0x10(%rsp)
subl $0x1, %r12d
jb 0x43cb
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0x4445
movq %rbx, %rdi
testb %bpl, %bpl
je 0x42d1
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x44c6
leaq 0x5242(%rip), %rsi      # 0x9511
jmp 0x42e3
movq %r15, %rdx
pushq $0x1
popq %rcx
callq 0x44c6
leaq 0x5218(%rip), %rsi      # 0x94fb
cmpl $-0x1, %eax
je 0x42ed
movl %eax, %r15d
jmp 0x4305
movq 0x9cdc(%rip), %rdi      # 0xdfd0
callq 0x2140
movq %rax, %rdi
callq 0x20b0
pushq $-0x1
popq %r15
movq %rbx, %rdi
movl %r14d, %esi
movl %r15d, %edx
callq 0x4544
testl %eax, %eax
jne 0x4332
movq 0x9cb2(%rip), %rdi      # 0xdfd0
leaq 0x5203(%rip), %rsi      # 0x9528
callq 0x2140
movq %rax, %rdi
callq 0x20b0
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4564
testb %al, %al
je 0x43a4
testb %bpl, %bpl
je 0x4361
movq 0x9ca3(%rip), %rdi      # 0xdff0
leaq 0x5200(%rip), %rsi      # 0x9554
callq 0x2140
movq %rax, %rdi
callq 0x20b0
pushq $0x18
popq %rbp
movq %rbp, %rdi
callq 0x2160
movq %rax, %r13
andq $0x0, (%rax)
andl $0x0, 0x8(%rax)
movq %rbx, %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x45f8
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6fdb
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2170
movq 0x10(%rsp), %r13
movl 0x4(%rsp), %ebp
testb %bpl, %bpl
je 0x43ae
incl %r14d
jmp 0x43b6
callq 0x2090
movl %eax, %r14d
movq 0x8(%rsp), %r15
movl %r14d, %eax
xorl %edx, %edx
divl %r13d
movl %edx, %r14d
jmp 0x429e
movq 0x9c1e(%rip), %rdi      # 0xdff0
leaq 0x51ad(%rip), %rsi      # 0x9586
callq 0x2140
movq 0x98(%rbx), %rcx
movq 0x98(%rcx), %rsi
movq %rax, %rdi
callq 0x2120
leaq 0x51a6(%rip), %rsi      # 0x95a1
movq %rax, %rdi
callq 0x2140
movq 0x98(%rbx), %rcx
movl 0xc8(%rcx), %esi
movq %rax, %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x20b0
movq 0x98(%rbx), %rax
andq $0x0, 0x98(%rax)
andl $0x0, 0xc8(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq