// File: /workspace/github_c_makefile_star2_O0/dlbeer[P]ufat/ufat_mkfs.c
// Function: write_bpb
static int write_bpb(struct ufat_device *dev, const struct fs_layout *fl)
{
	static const uint8_t boot_header[11] = {
		0xeb, 0xfe, /* jmp $ */
		0x90,       /* nop */
		'u', 'f', 'a', 't', ' ', ' ', ' ', ' '
	};
	const char *type_name = "FAT     ";
	const unsigned int log2_bps =
		fl->log2_sector_size - dev->log2_block_size;
	const unsigned int block_size = 1 << dev->log2_block_size;
	uint8_t buf[block_size];

	switch (fl->type) {
	case UFAT_TYPE_FAT12:
		type_name = "FAT12   ";
		break;

	case UFAT_TYPE_FAT16:
		type_name = "FAT16   ";
		break;

	case UFAT_TYPE_FAT32:
		type_name = "FAT32   ";
		break;
	}

	memset(buf, 0, sizeof(buf));

	/* Boot sector signature */
	memcpy(buf, boot_header, sizeof(boot_header));
	buf[0x1fe] = 0x55;
	buf[0x1ff] = 0xaa;

	/* BIOS Parameter Block */
	w16(buf + 0x00b, 1 << fl->log2_sector_size);
	buf[0x00d] = 1 << (fl->log2_bpc - log2_bps);
	w16(buf + 0x00e, fl->reserved_blocks << log2_bps);
	buf[0x010] = 2; /* 2 FATs */
	w16(buf + 0x011, fl->root_blocks << (dev->log2_block_size - 5));
	if (fl->type != UFAT_TYPE_FAT32 && fl->logical_blocks <= UINT16_MAX)
		w16(buf + 0x013, fl->logical_blocks << log2_bps);
	else
		w32(buf + 0x020, fl->logical_blocks << log2_bps);
	buf[0x015] = MEDIA_DISK;

	if (fl->type != UFAT_TYPE_FAT32) {
		w16(buf + 0x016, fl->fat_blocks << log2_bps);
		buf[0x026] = 0x29; /* Extended boot signature */
		memset(buf + 0x02b, ' ', 11); /* Volume label */
		memcpy(buf + 0x036, type_name, 8);
	} else {
		w32(buf + 0x024, fl->fat_blocks << log2_bps);
		w32(buf + 0x02c, 2); /* Root directory cluster */
		w16(buf + 0x030, 1); /* FS informations sector */
		w16(buf + 0x032, BACKUP_SECTOR);
		buf[0x042] = 0x29; /* Extended boot signature */
		memset(buf + 0x047, ' ', 11); /* Volume label */
		memcpy(buf + 0x052, type_name, 8);
	}

	/* Write boot sector */
	if (dev->write(dev, 0, 1, buf) < 0)
		return -UFAT_ERR_IO;

	/* Write backup of boot sector in case of FAT32 */
	if (fl->type == UFAT_TYPE_FAT32 &&
	    dev->write(dev, BACKUP_SECTOR >> log2_bps, 1, buf) < 0)
		return -UFAT_ERR_IO;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq 0x38b6(%rip), %rax      # 0xc14d
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movb (%rax), %cl
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq %rax, -0x50(%rbp)
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x40(%rbp)
subl $0xc, %eax
je 0x890b
jmp 0x88f7
movl -0x40(%rbp), %eax
subl $0x10, %eax
je 0x8918
jmp 0x8901
movl -0x40(%rbp), %eax
subl $0x20, %eax
je 0x8925
jmp 0x8930
leaq 0x3844(%rip), %rax      # 0xc156
movq %rax, -0x20(%rbp)
jmp 0x8930
leaq 0x3840(%rip), %rax      # 0xc15f
movq %rax, -0x20(%rbp)
jmp 0x8930
leaq 0x383c(%rip), %rax      # 0xc168
movq %rax, -0x20(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rdx
xorl %esi, %esi
callq 0x2100
movq -0x48(%rbp), %rdi
movq 0x37f8(%rip), %rax      # 0xc142
movq %rax, (%rdi)
movw 0x37f6(%rip), %ax       # 0xc14a
movw %ax, 0x8(%rdi)
movb 0x37ee(%rip), %al       # 0xc14c
movb %al, 0xa(%rdi)
movb $0x55, 0x1fe(%rdi)
movb $-0x56, 0x1ff(%rdi)
addq $0xb, %rdi
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movzwl %ax, %esi
callq 0x8cb0
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
subl -0x24(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movb %al, 0xd(%rdi)
addq $0xe, %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
shlq %cl, %rax
movzwl %ax, %esi
callq 0x8cb0
movq -0x48(%rbp), %rdi
movb $0x2, 0x10(%rdi)
addq $0x11, %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
subl $0x5, %ecx
movl %ecx, %ecx
shlq %cl, %rax
movzwl %ax, %esi
callq 0x8cb0
movq -0x18(%rbp), %rax
cmpl $0x20, 0x2c(%rax)
je 0x8a1c
movq -0x18(%rbp), %rax
cmpq $0xffff, 0x20(%rax)     # imm = 0xFFFF
ja 0x8a1c
movq -0x48(%rbp), %rdi
addq $0x13, %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x24(%rbp), %ecx
shlq %cl, %rax
movzwl %ax, %esi
callq 0x8cb0
jmp 0x8a39
movq -0x48(%rbp), %rdi
addq $0x20, %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x24(%rbp), %ecx
shlq %cl, %rax
movl %eax, %esi
callq 0x8cf0
movq -0x48(%rbp), %rax
movb $-0x8, 0x15(%rax)
movq -0x18(%rbp), %rax
cmpl $0x20, 0x2c(%rax)
je 0x8a95
movq -0x48(%rbp), %rdi
addq $0x16, %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x24(%rbp), %ecx
shlq %cl, %rax
movzwl %ax, %esi
callq 0x8cb0
movq -0x48(%rbp), %rdi
movb $0x29, 0x26(%rdi)
addq $0x2b, %rdi
movl $0x20, %esi
movl $0xb, %edx
callq 0x2100
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x36(%rax)
jmp 0x8b12
movq -0x48(%rbp), %rdi
addq $0x24, %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x24(%rbp), %ecx
shlq %cl, %rax
movl %eax, %esi
callq 0x8cf0
movq -0x48(%rbp), %rdi
addq $0x2c, %rdi
movl $0x2, %esi
callq 0x8cf0
movq -0x48(%rbp), %rdi
addq $0x30, %rdi
movl $0x1, %esi
callq 0x8cb0
movq -0x48(%rbp), %rdi
addq $0x32, %rdi
movl $0x6, %esi
callq 0x8cb0
movq -0x48(%rbp), %rdi
movb $0x29, 0x42(%rdi)
addq $0x47, %rdi
movl $0x20, %esi
movl $0xb, %edx
callq 0x2100
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x52(%rax)
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
movl $0x1, %edx
callq *%rax
cmpl $0x0, %eax
jge 0x8b42
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x3c(%rbp)
jmp 0x8b93
movq -0x18(%rbp), %rax
cmpl $0x20, 0x2c(%rax)
jne 0x8b85
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %ecx
movl $0x6, %edx
sarl %cl, %edx
movq -0x48(%rbp), %rcx
movslq %edx, %rsi
movl $0x1, %edx
callq *%rax
cmpl $0x0, %eax
jge 0x8b85
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x3c(%rbp)
jmp 0x8b93
movl $0x0, -0x4(%rbp)
movl $0x1, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)