// File: /workspace/github_c_makefile_star2_O1/dlbeer[P]ufat/ufat_mkfs.c
// Function: ufat_mkfs
int ufat_mkfs(struct ufat_device *dev, ufat_block_t nblk)
{
	struct fs_layout fl;
	int err;

	err = calculate_layout(&fl, nblk, dev->log2_block_size);
	if (err < 0)
		return err;

	err = erase_reserved_blocks(dev, &fl);
	if (err < 0)
		return err;

	switch (fl.type) {
	case UFAT_TYPE_FAT12:
		err = init_fat12(dev, &fl);
		break;

	case UFAT_TYPE_FAT16:
		err = init_fat16(dev, &fl);
		break;

	case UFAT_TYPE_FAT32:
		err = init_fat32(dev, &fl);
		break;
	}

	if (err < 0)
		return err;

	if (fl.type == UFAT_TYPE_FAT32)
		err = init_root_cluster(dev, &fl);
	else
		err = init_root_blocks(dev, &fl);

	if (err < 0)
		return err;

	if (fl.type == UFAT_TYPE_FAT32) {
		err = write_fsinfo(dev, &fl);
		if (err < 0)
			return err;
	}

	return write_bpb(dev, &fl);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
movl (%rdi), %edx
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpl $0xc, %edx
ja 0x472a
xorl %edi, %edi
movl $0x9, %r8d
movl $0x9, %eax
subl %edx, %eax
cmoval %edx, %r8d
cmovbl %edi, %eax
movl %edx, %r9d
subl %r8d, %r9d
leal (%rax,%rdi), %ecx
movq %rsi, %r10
shrq %cl, %r10
leal (%r9,%rdi), %ecx
addl $0x9, %ecx
incq %rdi
cmpl $0xb, %ecx
ja 0x45a4
movq %r10, %rcx
shrq $0x20, %rcx
jne 0x4583
addq %rdi, %rax
decq %rax
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %eax, %ecx
shlq %cl, %r11
shrq $0x20, %r10
testq %r10, %r10
cmovneq %r11, %rsi
leal 0x8(%r9,%rdi), %ecx
movl %ecx, (%rsp)
movq %rsi, %r10
movl %eax, %ecx
shrq %cl, %r10
cmpl $0x20cf, %r10d          # imm = 0x20CF
ja 0x45e9
movl $0xc, 0x2c(%rsp)
movl $0x1000, %r11d          # imm = 0x1000
jmp 0x4600
cmpl $0xfffff, %r10d         # imm = 0xFFFFF
ja 0x4607
movl $0x10, 0x2c(%rsp)
movl $0x10000, %r11d         # imm = 0x10000
movl $0x1, %ecx
jmp 0x461a
movl $0x20, 0x2c(%rsp)
movl $0x200000, %r11d        # imm = 0x200000
movl $0x3, %ecx
addl %ecx, %r9d
addl %edi, %r9d
addl $0x8, %r9d
movl %r10d, %ebp
shrl %cl, %ebp
cmpl $0xe, %r9d
ja 0x4643
cmpl %r11d, %ebp
jbe 0x4643
incl %ecx
incl %r9d
cmpl $0x7, %ecx
jne 0x4624
movl $0x7, %ecx
movl %ecx, %ecx
negl %r8d
addq %rcx, %r8
addq %r8, %rdi
addq $0x8, %rdi
movl %edi, 0x4(%rsp)
movl 0x2c(%rsp), %r10d
cmpl $0x20, %r10d
movl $0x20, %ecx
movl $0x1, %r9d
cmoveq %rcx, %r9
movl %eax, %ecx
shlq %cl, %r9
movq %rsi, %rax
subq %r9, %rax
movl %edi, %ecx
shrq %cl, %rax
movq %r9, 0x8(%rsp)
movl %edi, %r8d
addl $0x2, %eax
cmpl $0x10, %r10d
je 0x469a
cmpl $0x20, %r10d
jne 0x469e
shll $0x2, %eax
jmp 0x46a5
addl %eax, %eax
jmp 0x46a5
leal (%rax,%rax,2), %eax
incl %eax
shrl %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %r15d
notl %r15d
addl %eax, %r15d
shrl %cl, %r15d
movl $0x3fff, %r12d          # imm = 0x3FFF
shrl %cl, %r12d
incl %r12d
xorl %eax, %eax
cmpl $0x20, %r10d
cmovel %eax, %r12d
leaq (%r15,%r15), %r11
leaq (%r9,%r12), %rcx
leaq (%rcx,%r15,2), %rcx
movq %rsi, %r14
subq %rcx, %r14
movl %r8d, %ecx
shrq %cl, %r14
leal 0x2(%r14), %ecx
movq %r15, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movl %ecx, 0x28(%rsp)
cmpl $0x20, %r10d
je 0x4715
movl %r14d, %r8d
movl %edi, %ecx
shll %cl, %r8d
addq %r9, %r8
addq %r11, %r8
subq %r8, %rsi
movq %rsi, 0x10(%rsp)
movl %edi, %ecx
shll %cl, %r14d
addq %r9, %r11
addq %r14, %r11
addq 0x10(%rsp), %r11
movq %r11, 0x20(%rsp)
cmpl $0xc, %edx
ja 0x47dd
movq 0x8(%rsp), %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4e81
testl %eax, %eax
js 0x47dd
movl 0x2c(%rsp), %ebp
cmpl $0x20, %ebp
je 0x477a
cmpl $0x10, %ebp
je 0x476d
cmpl $0xc, %ebp
jne 0x4785
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x47ea
jmp 0x4785
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x4952
jmp 0x4785
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x4a6f
testl %eax, %eax
js 0x47d8
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
leaq (%r14,%rax,2), %rsi
cmpl $0x20, %ebp
jne 0x47ab
addq %rdx, %rsi
movb 0x4(%rsp), %cl
movl $0x1, %edx
shlq %cl, %rdx
movq %rbx, %rdi
callq 0x4e81
testl %eax, %eax
js 0x47dd
cmpl $0x20, %ebp
jne 0x47cb
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x4b9c
testl %eax, %eax
js 0x47dd
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x4c85
jmp 0x47dd
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq