// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl (%rdi), %eax
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
cmpl $0xc, %eax
ja 0x4609
xorl %r14d, %r14d
movl $0x9, %r8d
movl $0x9, %edx
subl %eax, %edx
cmoval %eax, %r8d
cmovbl %r14d, %edx
movl %eax, %r9d
subl %r8d, %r9d
leal (%rdx,%r14), %ecx
movq %rsi, %r10
shrq %cl, %r10
leal (%r9,%r14), %ecx
addl $0x9, %ecx
incq %r14
cmpl $0xb, %ecx
ja 0x436b
movq %r10, %rcx
shrq $0x20, %rcx
jne 0x4349
addq %r14, %rdx
decq %rdx
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %edx, %ecx
shlq %cl, %r11
shrq $0x20, %r10
testq %r10, %r10
cmovneq %r11, %rsi
leaq (%r9,%r14), %r15
addq $0x8, %r15
movl %r15d, 0x20(%rsp)
movq %rsi, %r10
shrq %cl, %r10
cmpl $0x20cf, %r10d          # imm = 0x20CF
ja 0x43bd
movl $0xc, 0x4c(%rsp)
movl $0x1000, %r11d          # imm = 0x1000
movl $0x1, %ecx
movl $0xc, %ebp
jmp 0x43de
cmpl $0xfffff, %r10d         # imm = 0xFFFFF
ja 0x43e8
movl $0x10, 0x4c(%rsp)
movl $0x10000, %r11d         # imm = 0x10000
movl $0x1, %ecx
movl $0x10, %ebp
movl $0x0, 0xc(%rsp)
jmp 0x4406
movl $0x20, 0x4c(%rsp)
movl $0x200000, %r11d        # imm = 0x200000
movl $0x3, %ecx
movl $0x20, %ebp
movb $0x1, %bl
movl %ebx, 0xc(%rsp)
addl %ecx, %r9d
addl %r14d, %r9d
addl $0x8, %r9d
movq %rdi, 0x10(%rsp)
movq %r15, 0x50(%rsp)
movl %r10d, %ebx
shrl %cl, %ebx
cmpl $0xe, %r9d
ja 0x4439
cmpl %r11d, %ebx
jbe 0x4439
incl %ecx
incl %r9d
cmpl $0x7, %ecx
jne 0x441a
movl $0x7, %ecx
movl %ecx, %ecx
negl %r8d
addq %rcx, %r8
movl 0xc(%rsp), %r9d
testb %r9b, %r9b
movl $0x20, %ecx
movl $0x1, %r15d
cmovneq %rcx, %r15
movl %edx, %ecx
shlq %cl, %r15
leaq (%r14,%r8), %r12
addq $0x8, %r12
movl %r12d, 0x24(%rsp)
movq %rsi, %rdi
subq %r15, %rdi
movl %r12d, %ecx
shrq %cl, %rdi
movq %r15, 0x28(%rsp)
movl %r12d, %edx
addl $0x2, %edi
cmpl $0x10, %ebp
movl %ebp, 0x1c(%rsp)
je 0x44ab
cmpl $0x20, %ebp
jne 0x44c3
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %eax, %ecx
shll %cl, %r8d
notl %r8d
leal (%r8,%rdi,4), %r13d
shrl %cl, %r13d
movq %r13, 0x38(%rsp)
jmp 0x44e7
movl $0x1, %ebp
movl %eax, %ecx
shll %cl, %ebp
leal -0x1(%rbp,%rdi,2), %r13d
shrl %cl, %r13d
movq %r13, 0x38(%rsp)
jmp 0x44eb
leal (%rdi,%rdi,2), %edi
incl %edi
shrl %edi
movl $0x1, %ebp
movl %eax, %ecx
shll %cl, %ebp
leal (%rdi,%rbp), %r13d
decl %r13d
shrl %cl, %r13d
movq %r13, 0x38(%rsp)
testb %r9b, %r9b
je 0x44eb
xorl %ebp, %ebp
jmp 0x44f5
addl $0x3fff, %ebp           # imm = 0x3FFF
movl %eax, %ecx
shrl %cl, %ebp
movq %rbp, 0x30(%rsp)
leaq (%r15,%rbp), %rax
leaq (%rax,%r13,2), %rax
movq %rsi, %rdi
subq %rax, %rdi
movl %edx, %ecx
shrq %cl, %rdi
leal 0x2(%rdi), %ebx
movl %r12d, %ecx
shll %cl, %edi
movl %ebx, 0x48(%rsp)
leaq (%r15,%r13,2), %rax
addq %rdi, %rax
testb %r9b, %r9b
jne 0x4530
subq %rax, %rsi
movq %rsi, 0x30(%rsp)
movq %rsi, %rbp
addq %rbp, %rax
movq %rax, 0x40(%rsp)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x4ce1
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x4609
addq %r13, %r13
movl 0x1c(%rsp), %eax
cmpl $0x20, %eax
je 0x45af
cmpl $0x10, %eax
je 0x457c
cmpl $0xc, %eax
jne 0x4597
leaq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x461b
jmp 0x458b
leaq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x477c
testl %eax, %eax
movl 0xc(%rsp), %eax
js 0x4609
testb %al, %al
jne 0x45c2
addq %r15, %r13
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x4ce1
jmp 0x45f5
leaq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x4892
testl %eax, %eax
js 0x4609
addq %r15, %r13
addq %rbp, %r13
movl $0x1, %edx
movl %r12d, %ecx
shll %cl, %edx
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x4ce1
testl %eax, %eax
js 0x4609
movq %r15, %rdi
movq 0x50(%rsp), %rsi
movl %ebx, %edx
callq 0x49b8
testl %eax, %eax
js 0x4609
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x4a98
movl %eax, %r14d
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq