// File: /workspace/github_c_makefile_star2_O0/dlbeer[P]ufat/ufat_mkfs.c
// Function: init_fat12
static int init_fat12(struct ufat_device *dev, const struct fs_layout *fl)
{
	const unsigned int block_size = 1 << dev->log2_block_size;
	unsigned int minor_byte = 0;
	unsigned int cluster_pair = 0;
	ufat_block_t i;
	ufat_block_t block;

	for (i = 0, block = 0; i < fl->fat_blocks * 2; i++, block++) {
		uint8_t buf[block_size];
		unsigned int j;

		if (block == fl->fat_blocks) {
			block = 0;
			minor_byte = 0;
			cluster_pair = 0;
		}

		memset(buf, 0, block_size);

		for (j = 0; j < block_size; j++) {
			uint32_t pair_data = 0;

			if ((cluster_pair << 1) >= fl->clusters)
				pair_data = 0xff7ff7;
			else if ((cluster_pair << 1) + 1 >= fl->clusters)
				pair_data = 0xff7000;

			buf[j] = pair_data >> (minor_byte << 3);

			if (++minor_byte >= 3) {
				minor_byte = 0;
				cluster_pair++;
			}
		}

		if (block == 0) {
			buf[0] = MEDIA_DISK;
			buf[1] = 0x8f;
			buf[2] = 0xff;
		}

		if (dev->write(dev, fl->reserved_blocks + i, 1, buf) < 0)
			return -UFAT_ERR_IO;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
jae 0x8324
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jne 0x8200
movq $0x0, -0x38(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x60(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x2100
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x829e
movl $0x0, -0x50(%rbp)
movl -0x24(%rbp), %eax
shll %eax
movq -0x18(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jb 0x823d
movl $0xff7ff7, -0x50(%rbp)  # imm = 0xFF7FF7
jmp 0x8257
movl -0x24(%rbp), %eax
shll %eax
addl $0x1, %eax
movq -0x18(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jb 0x8255
movl $0xff7000, -0x50(%rbp)  # imm = 0xFF7000
jmp 0x8257
movq -0x60(%rbp), %rax
movl -0x50(%rbp), %edx
movl -0x20(%rbp), %ecx
shll $0x3, %ecx
shrl %cl, %edx
movl %edx, %ecx
movb %cl, %dl
movl -0x4c(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
cmpl $0x3, %eax
jb 0x828e
movl $0x0, -0x20(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x8290
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8217
cmpq $0x0, -0x38(%rbp)
jne 0x82b4
movq -0x60(%rbp), %rax
movb $-0x8, (%rax)
movb $-0x71, 0x1(%rax)
movb $-0x1, 0x2(%rax)
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq 0x8(%rdx), %rsi
addq -0x30(%rbp), %rsi
movl $0x1, %edx
callq *%rax
cmpl $0x0, %eax
jge 0x82ec
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x54(%rbp)
jmp 0x82f3
movl $0x0, -0x54(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rsp
movl -0x54(%rbp), %eax
testl %eax, %eax
je 0x8305
jmp 0x8303
jmp 0x832b
jmp 0x8307
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x819e
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)