// File: /workspace/github_c_makefile_star2_O0/krinkinmu[P]64bit/ide.c
// Function: _write_sectors_lba48
static int __write_sectors_lba48(const void *data, unsigned long long sector,
			size_t count)
{
	const unsigned long slo = sector & 0xfffffffful;
	const unsigned long shi = sector >> 32;

	const unsigned clo = count & 0xffu;
	const unsigned chi = count >> 8;

	out8(IDE_DEVCTL_REG, IDE_NIEN);

	out8(IDE_SC_REG, chi);
	out8(IDE_LBA0_REG, shi & 0xff);
	out8(IDE_LBA1_REG, (shi >> 8) & 0xff);
	out8(IDE_LBA2_REG, (shi >> 16) & 0xff);

	out8(IDE_SC_REG, clo);
	out8(IDE_LBA0_REG, slo & 0xff);
	out8(IDE_LBA1_REG, (slo >> 8) & 0xff);
	out8(IDE_LBA2_REG, (slo >> 16) & 0xff);

	out8(IDE_DEV_REG, IDE_LBA);
	out8(IDE_CMD_REG, IDE_CMD_WRITE_LBA48);

	ide_wait_ready();

	const uint16_t *wptr = (uint16_t *)data;
	const size_t words = IDE_SECTOR_SIZE / sizeof(*wptr);

	for (size_t i = 0; i != count; ++i) {
		for (size_t j = 0; j != words; ++j)
			out16(IDE_DATA_REG, *wptr++);
		in8(IDE_ASTATUS_REG); // wait one PIO transfer cycle
		if (ide_wait_ready() != 0)
			return -1;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movabsq $0xffffffff, %rax # imm = 0xFFFFFFFF
andq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
shrq $0x20, %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
andq $0xff, %rax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
shrq $0x8, %rax
movl %eax, -0x38(%rbp)
movl $0x3f6, %edi            # imm = 0x3F6
movl $0x2, %esi
callq 0xffffffff8010eb10
movl -0x38(%rbp), %eax
movl $0x1f2, %edi            # imm = 0x1F2
movzbl %al, %esi
callq 0xffffffff8010eb10
movq -0x30(%rbp), %rax
andq $0xff, %rax
movl $0x1f3, %edi            # imm = 0x1F3
movzbl %al, %esi
callq 0xffffffff8010eb10
movq -0x30(%rbp), %rax
shrq $0x8, %rax
andq $0xff, %rax
movl $0x1f4, %edi            # imm = 0x1F4
movzbl %al, %esi
callq 0xffffffff8010eb10
movq -0x30(%rbp), %rax
shrq $0x10, %rax
andq $0xff, %rax
movl $0x1f5, %edi            # imm = 0x1F5
movzbl %al, %esi
callq 0xffffffff8010eb10
movl -0x34(%rbp), %eax
movl $0x1f2, %edi            # imm = 0x1F2
movzbl %al, %esi
callq 0xffffffff8010eb10
movq -0x28(%rbp), %rax
andq $0xff, %rax
movl $0x1f3, %edi            # imm = 0x1F3
movzbl %al, %esi
callq 0xffffffff8010eb10
movq -0x28(%rbp), %rax
shrq $0x8, %rax
andq $0xff, %rax
movl $0x1f4, %edi            # imm = 0x1F4
movzbl %al, %esi
callq 0xffffffff8010eb10
movq -0x28(%rbp), %rax
shrq $0x10, %rax
andq $0xff, %rax
movl $0x1f5, %edi            # imm = 0x1F5
movzbl %al, %esi
callq 0xffffffff8010eb10
movl $0x1f6, %edi            # imm = 0x1F6
movl $0x40, %esi
callq 0xffffffff8010eb10
movl $0x1f7, %edi            # imm = 0x1F7
movl $0x34, %esi
callq 0xffffffff8010eb10
callq 0xffffffff8010ead0
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x100, -0x48(%rbp) # imm = 0x100
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0xffffffff8010ed41
movq $0x0, -0x58(%rbp)
cmpq $0x100, -0x58(%rbp) # imm = 0x100
je 0xffffffff8010ed14
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $0x2, %rcx
movq %rcx, -0x40(%rbp)
movl $0x1f0, %edi            # imm = 0x1F0
movzwl (%rax), %esi
callq 0xffffffff8010ed60
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xffffffff8010ece0
movl $0x3f6, %edi            # imm = 0x3F6
callq 0xffffffff8010eb50
callq 0xffffffff8010ead0
cmpl $0x0, %eax
je 0xffffffff8010ed31
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xffffffff8010ed48
jmp 0xffffffff8010ed33
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0xffffffff8010ecce
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)