// File: /workspace/github_c_makefile_star2_O2/hobo0xcc[P]rve/src/cpu.c
// Function: DiskAccess
void DiskAccess(State *state) {
    // printf("Disk Access\n");
    uint64_t desc_addr = DescAddr(state);
    // printf("rve: desc: %lld\n", (int64_t)desc_addr);
    uint64_t avail_addr = DescAddr(state) + 8 * 16;
    // printf("rve: avail: %lld\n", (int64_t)avail_addr);
    uint64_t used_addr = DescAddr(state) + 4096;
    // printf("rve: used: %lld\n", (int64_t)used_addr);

    uint32_t queue_size = state->virtio->queue_num;
    uint16_t offset = Read16(state, avail_addr);
    uint16_t index = Read16(state, avail_addr + 2);
    uint64_t index_addr = avail_addr + 4 + (uint64_t)state->virtio->id * 2 /*desc_addr + VRING_DESC_SIZE * index*/;
    uint64_t head_index = Read16(state, index_addr) % queue_size;
    uint64_t desc_addr0 = desc_addr + VRING_DESC_SIZE * head_index;
    uint64_t addr0 = Read64(state, desc_addr0);
    uint16_t next0 = Read16(state, desc_addr0 + 14);
    uint64_t desc_addr1 = desc_addr + VRING_DESC_SIZE * next0;
    uint64_t addr1 = Read64(state, desc_addr1);
    uint32_t len1 = Read32(state, desc_addr1 + 8);
    uint16_t flags1 = Read16(state, desc_addr1 + 12);
    uint16_t next1 = Read64(state, desc_addr1 + 14);
    uint64_t desc_addr2 = desc_addr + VRING_DESC_SIZE * next1;
    uint64_t addr2 = Read64(state, desc_addr2);
    uint32_t len2 = Read32(state, desc_addr2 + 8);
    uint16_t flags2 = Read16(state, desc_addr2 + 12);

    uint64_t blk_sector = Read64(state, addr0 + 8);

    if ((flags1 & 2) == 0) {
        for (uint64_t i = 0; i < len1; i++) {
            uint8_t data = Read8(state, addr1 + i);
            WriteDisk(state, blk_sector * 512 + i, data);
        }
    } else {
        for (uint64_t i = 0; i < len1; i++) {
            uint8_t data = ReadDisk(state, blk_sector * 512 + i);
            Write8(state, addr1 + i, data);
            // printf("DiskRead disk_addr: %llx, addr: %llx, %c\n", blk_sector * 512 + i, addr1 + i, data);
        }
    }

    if (flags2 & 2) {
        Write8(state, addr2, 0);
    } else {
        Error("Third descriptor must be write mode");
    }

    uint64_t new_id = GetNewId(state) % queue_size;
    Write16(state, used_addr + 2, new_id % 8);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x8130(%rdi), %rax
movl 0x18(%rax), %ecx
movl 0x8(%rax), %r14d
movl 0x10(%rax), %r15d
imulq %rcx, %r14
leaq 0x80(%r14), %rsi
callq 0x2010
leaq 0x82(%r14), %rsi
movq %rbx, %rdi
callq 0x2010
movq 0x8130(%rbx), %rax
movl 0x38(%rax), %eax
leaq (%r14,%rax,2), %rsi
addq $0x84, %rsi
movq %rbx, %rdi
callq 0x2010
movzwl %ax, %eax
xorl %edx, %edx
movl %r15d, -0x38(%rbp)
divl %r15d
movl %edx, %r15d
shll $0x4, %r15d
leaq (%r14,%r15), %rsi
movq %rbx, %rdi
callq 0x2044
movq %rax, %r12
leaq (%r14,%r15), %rsi
addq $0xe, %rsi
movq %rbx, %rdi
callq 0x2010
movzwl %ax, %r13d
shll $0x4, %r13d
leaq (%r14,%r13), %rsi
movq %rbx, %rdi
callq 0x2044
movq %rax, %r15
leaq (%r14,%r13), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x2078
movl %eax, -0x30(%rbp)
leaq (%r14,%r13), %rsi
addq $0xc, %rsi
movq %rbx, %rdi
callq 0x2010
movl %eax, -0x2c(%rbp)
leaq (%r14,%r13), %rsi
addq $0xe, %rsi
movq %rbx, %rdi
callq 0x2044
movzwl %ax, %r13d
shll $0x4, %r13d
leaq (%r14,%r13), %rsi
movq %rbx, %rdi
callq 0x2044
movq %rax, -0x40(%rbp)
leaq (%r14,%r13), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x2078
leaq (%r14,%r13), %rsi
addq $0xc, %rsi
movq %rbx, %rdi
callq 0x2010
movl %eax, -0x34(%rbp)
addq $0x8, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2044
movq %rax, %r12
shlq $0x9, %r12
movl -0x30(%rbp), %r13d
testb $0x2, -0x2c(%rbp)
jne 0x1914
subq $0x1, %r13
jb 0x193d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x20ac
movq 0x8130(%rbx), %rcx
movq 0x40(%rcx), %rcx
movb %al, (%rcx,%r12)
incq %r12
incq %r15
jmp 0x18ec
subq $0x1, %r13
jb 0x193d
movq 0x8130(%rbx), %rax
movq 0x40(%rax), %rax
movzbl (%rax,%r12), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x20e0
incq %r12
incq %r15
jmp 0x1914
testb $0x2, -0x34(%rbp)
jne 0x1953
leaq 0x5921(%rip), %rdi      # 0x726b
xorl %eax, %eax
callq 0x5ca8
jmp 0x1961
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x20e0
movq 0x8130(%rbx), %rcx
movl 0x38(%rcx), %eax
incl %eax
movl %eax, 0x38(%rcx)
xorl %edx, %edx
divl -0x38(%rbp)
addq $0x1002, %r14           # imm = 0x1002
andl $0x7, %edx
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2115