// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xb0, %rsp
cmpb $0x1, 0x40(%rdi)
je 0x1299c
movl %edx, %ebp
movq %rdi, %rbx
leaq 0x20(%rdi), %r14
movq %r14, %rdi
callq 0x6250
movq 0x20(%rbx), %rdi
movl $0x5002, %esi           # imm = 0x5002
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6320
leaq 0x8(%rbx), %r15
movl %eax, 0x8(%rbx)
leaq 0x103a2(%rip), %r12     # 0x22c87
testl %eax, %eax
js 0x129a3
movq 0x20(%rbx), %rdi
movzbl %bpl, %esi
shll $0xc, %esi
orl $0x2, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x6320
movl %eax, 0xc(%rbx)
testl %eax, %eax
js 0x129aa
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x6340
testl %eax, %eax
js 0x129b1
xorps %xmm0, %xmm0
movq %rsp, %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x1, (%rdx)
movl (%r15), %edi
movl $0x6, %esi
xorl %eax, %eax
callq 0x6270
testl %eax, %eax
js 0x129bf
movl (%r15), %edi
leaq 0x20(%rsp), %rsi
callq 0x6640
testl %eax, %eax
js 0x129cd
movl $0xf000, %eax           # imm = 0xF000
andl 0x38(%rsp), %eax
cmpl $0x6000, %eax           # imm = 0x6000
jne 0x12983
movq %rbx, %rdi
callq 0x1283e
movq %rax, 0x10(%rbx)
movl 0x58(%rsp), %eax
jmp 0x12991
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rbx)
callq 0x66b0
movl %eax, 0x18(%rbx)
movb $0x1, 0x40(%rbx)
xorl %eax, %eax
jmp 0x129ef
callq 0x7655
jmp 0x129ea
movl $0x64, %edi
jmp 0x129d9
movl $0x70, %edi
jmp 0x129d9
leaq 0x102eb(%rip), %r12     # 0x22ca3
movl $0x76, %edi
jmp 0x129d9
leaq 0x10303(%rip), %r12     # 0x22cc9
movl $0x7c, %edi
jmp 0x129d9
leaq 0x10313(%rip), %r12     # 0x22ce7
movl $0x83, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq %r15, %r8
callq 0x75ae
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop