// File: yangwanyuan[P]HLKVDS/src/MetaStor.cc
// Function: hlkvds::MetaStor::zeroDeviceMetaRegion(unsigned long)
bool MetaStor::zeroDeviceMetaRegion(uint64_t meta_size) {
    if (meta_size % 4096 != 0) {
        return false;
    }

    char *align_buf;
    int block_size = metaDev_->GetBlockSize();
    block_size = (block_size > 4096? block_size : 4096);

    int ret = posix_memalign((void **)&align_buf, 4096, (size_t)block_size);
    if (ret < 0) {
        __ERROR("Can't allocate memory for SSTs, Persist Failed!");
        return false;
    }
    memset(align_buf, 0, block_size);

    uint64_t need_wte_bytes = meta_size;
    uint64_t wte_offset = 0;
    while (need_wte_bytes > 0) {
        uint64_t bytes_to_write = min(need_wte_bytes, (uint64_t)block_size);
        if ( !metaDev_->pWrite(align_buf, bytes_to_write, wte_offset) ) {
            __ERROR("error in fill_file_with_zeros write: %s\n", strerror(errno));
            free(align_buf);
            return false;
        }
        need_wte_bytes -= bytes_to_write;
        wte_offset += bytes_to_write;
    }
    free(align_buf);
    return true;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x60(%rsp), %rax
andq $0xfff, %rax            # imm = 0xFFF
cmpq $0x0, %rax
je 0x34ee3
movb $0x0, 0x77(%rsp)
jmp 0x35101
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movl %eax, 0x54(%rsp)
cmpl $0x1000, 0x54(%rsp)     # imm = 0x1000
jle 0x34f0a
movl 0x54(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x34f15
movl $0x1000, %eax           # imm = 0x1000
movl %eax, 0x24(%rsp)
jmp 0x34f15
movl 0x24(%rsp), %eax
movl %eax, 0x54(%rsp)
movslq 0x54(%rsp), %rdx
leaq 0x58(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x8230
movl %eax, 0x50(%rsp)
cmpl $0x0, 0x50(%rsp)
jge 0x34fb6
jmp 0x34f3e
movq 0x3c093(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x8480
movq 0x18(%rsp), %rdi
movq %rax, %rdx
leaq 0x1d550(%rip), %rsi     # 0x524b1
leaq 0x1e3b1(%rip), %rcx     # 0x53319
leaq 0x1e2de(%rip), %r8      # 0x5324d
movl $0x9d, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x3c055(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x1e3a1(%rip), %rsi     # 0x5332e
movb $0x0, %al
callq 0x86f0
movq 0x3c03d(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x1de20(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
movb $0x0, 0x77(%rsp)
jmp 0x35101
movq 0x58(%rsp), %rdi
movslq 0x54(%rsp), %rdx
xorl %esi, %esi
callq 0x8280
movq 0x60(%rsp), %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
cmpq $0x0, 0x48(%rsp)
jbe 0x350f2
movslq 0x54(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1b7a0
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq (%rcx), %rcx
movq %rcx, 0x38(%rsp)
movq 0x10(%rax), %rdi
movq 0x58(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq (%rdi), %rax
callq *0x48(%rax)
cmpq $0x0, %rax
jne 0x350cc
jmp 0x35034
movq 0x3bf9d(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x8480
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x1d45a(%rip), %rsi     # 0x524b1
leaq 0x1e2bb(%rip), %rcx     # 0x53319
leaq 0x1e1e8(%rip), %r8      # 0x5324d
movl $0xa7, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x3bf5f(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x8070
movl (%rax), %edi
callq 0x8130
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x1dd40(%rip), %rsi     # 0x52ddc
movb $0x0, %al
callq 0x86f0
movq 0x3bf2e(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x1dd11(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
movq 0x58(%rsp), %rdi
callq 0x85b0
movb $0x0, 0x77(%rsp)
jmp 0x35101
movq 0x38(%rsp), %rcx
movq 0x48(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x38(%rsp), %rax
addq 0x40(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x34fda
movq 0x58(%rsp), %rdi
callq 0x85b0
movb $0x1, 0x77(%rsp)
movb 0x77(%rsp), %al
andb $0x1, %al
addq $0x78, %rsp
retq
nopl (%rax)