// File: /workspace/github_cpp_makefile_star2_O3/yangwanyuan[P]HLKVDS/src/KernelDevice.cc
// Function: hlkvds::KernelDevice::Open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
int KernelDevice::Open(string path, bool dsync) {
    if (isOpen_) {
        __ERROR("This Device is already Open!!!");
        return KD_ERR;
    }

    path_ = path;

    int r = 0;

    directFd_ = open(path_.c_str(), O_RDWR | O_DIRECT | O_DSYNC, 0666);
    if (directFd_ < 0) {
        __ERROR("Could not open file: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    if (dsync) {
        //bufFd_ = open(path_.c_str(), O_RDWR | O_SYNC, 0666);
        bufFd_ = open(path_.c_str(), O_RDWR | O_DSYNC, 0666);
    } else {
        bufFd_ = open(path_.c_str(), O_RDWR, 0666);
    }

    if (bufFd_ < 0) {
        __ERROR("Could not open file: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    r = posix_fadvise(bufFd_, 0, 0, POSIX_FADV_RANDOM);
    if (r < 0) {
        __ERROR("Couldn't posix_fadvise random: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    r = lock_device();
    if (r < 0) {
        __ERROR("failed to lock device: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    struct stat statbuf;
    r = fstat(directFd_, &statbuf);
    if (r < 0) {
        __ERROR("Couldn't read fstat: %s %s\n", path_.c_str(), strerror(errno));
        goto open_fail;
    }

    if (S_ISBLK(statbuf.st_mode)) {
        capacity_ = get_block_device_capacity();
        blockSize_ = statbuf.st_blksize;
    } else {
        capacity_ = statbuf.st_size;
        //TODO:() dynamic block size in file mode
        blockSize_ = getpagesize();
    }

    isOpen_ = true;

    return KD_OK;

open_fail:
    close(bufFd_);
    close(directFd_);
    directFd_ = -1;
    bufFd_ = -1;
    return KD_ERR;

}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0xb0, %rsp
cmpb $0x1, 0x40(%rdi)
je 0x12ac3
movl %edx, %ebp
movq %rdi, %rbx
leaq 0x20(%rdi), %r14
movq %r14, %rdi
callq 0x6240
movq 0x20(%rbx), %rdi
movl $0x5002, %esi           # imm = 0x5002
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6310
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0x12aca
movq 0x20(%rbx), %rdi
movzbl %bpl, %esi
shll $0xc, %esi
orl $0x2, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6310
movl %eax, 0xc(%rbx)
testl %eax, %eax
js 0x12ad4
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x6330
testl %eax, %eax
js 0x12ade
xorps %xmm0, %xmm0
movq %rsp, %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movw $0x1, (%rdx)
movl 0x8(%rbx), %edi
movl $0x6, %esi
xorl %eax, %eax
callq 0x6260
testl %eax, %eax
js 0x12ae8
movl 0x8(%rbx), %edi
leaq 0x20(%rsp), %rsi
callq 0x6630
testl %eax, %eax
js 0x12af2
movl $0xf000, %eax           # imm = 0xF000
andl 0x38(%rsp), %eax
cmpl $0x6000, %eax           # imm = 0x6000
jne 0x12aaa
movq %rbx, %rdi
callq 0x12976
movq %rax, %r14
movl 0x58(%rsp), %eax
jmp 0x12ab4
movq 0x50(%rsp), %r14
callq 0x66a0
movq %r14, 0x10(%rbx)
movl %eax, 0x18(%rbx)
movb $0x1, 0x40(%rbx)
xorl %eax, %eax
jmp 0x12b17
callq 0x77bc
jmp 0x12b12
movq %r14, %rdi
callq 0x7746
jmp 0x12afa
movq %r14, %rdi
callq 0x76d0
jmp 0x12afa
movq %r14, %rdi
callq 0x765a
jmp 0x12afa
movq %r14, %rdi
callq 0x75e3
jmp 0x12afa
movq %r14, %rdi
callq 0x756a
movl 0xc(%rbx), %edi
callq 0x6740
movl 0x8(%rbx), %edi
callq 0x6740
movq $-0x1, 0x8(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xb0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nop