// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movl (%rsi), %r12d
movq %rdi, -0x40(%rbp)
movl (%rdi), %r13d
movl $0x1, %eax
movl %r13d, %ecx
shlq %cl, %rax
movl %r12d, %ecx
subl %r13d, %ecx
movl %ecx, -0x2c(%rbp)
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
movq %rsp, %rbx
subq %rcx, %rbx
movq %rbx, %rsp
movl 0x2c(%rsi), %r15d
cmpl $0x20, %r15d
je 0x4cf0
cmpl $0x10, %r15d
je 0x4ce7
cmpl $0xc, %r15d
jne 0x4cf9
leaq 0x24b1(%rip), %rcx      # 0x7196
jmp 0x4d00
leaq 0x24b1(%rip), %rcx      # 0x719f
jmp 0x4d00
leaq 0x24b1(%rip), %rcx      # 0x71a8
jmp 0x4d00
leaq 0x248d(%rip), %rcx      # 0x718d
movq %rcx, -0x38(%rbp)
addq $-0xb, %rax
xorl %edx, %edx
cmpl $0x4, %r13d
cmovaeq %rax, %rdx
leaq 0xb(%rbx), %rdi
xorl %esi, %esi
callq 0x1110
movabsq $0x207461667590feeb, %rax # imm = 0x207461667590FEEB
movq %rax, (%rbx)
movl $0x20202020, 0x7(%rbx)  # imm = 0x20202020
movw $0xaa55, 0x1fe(%rbx)    # imm = 0xAA55
movl $0x1, %eax
movl $0x1, %edx
movl %r12d, %ecx
shll %cl, %edx
movb %dl, 0xb(%rbx)
movb %dh, 0xc(%rbx)
movl 0x4(%r14), %ecx
movl -0x2c(%rbp), %edx
subl %edx, %ecx
shll %cl, %eax
movb %al, 0xd(%rbx)
movq 0x8(%r14), %rax
movl %edx, %ecx
shlq %cl, %rax
movb %al, 0xe(%rbx)
movb %ah, 0xf(%rbx)
movb $0x2, 0x10(%rbx)
movq 0x10(%r14), %rax
addb $-0x5, %r13b
movl %r13d, %ecx
shlq %cl, %rax
movl %edx, %ecx
movw %ax, 0x11(%rbx)
cmpl $0x20, %r15d
je 0x4d9f
movq 0x20(%r14), %rax
cmpq $0xffff, %rax           # imm = 0xFFFF
ja 0x4d9f
shlq %cl, %rax
movw %ax, 0x13(%rbx)
jmp 0x4da9
movq 0x20(%r14), %rax
shlq %cl, %rax
movl %eax, 0x20(%rbx)
movb $-0x8, 0x15(%rbx)
movq 0x18(%r14), %rax
shlq %cl, %rax
movl %eax, %edx
shrl $0x8, %edx
cmpl $0x20, %r15d
jne 0x4df1
movb %al, 0x24(%rbx)
movb %dl, 0x25(%rbx)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x26(%rbx)
shrl $0x18, %eax
movb %al, 0x27(%rbx)
movabsq $0x6000100000002, %rax  # imm = 0x6000100000002
movq %rax, 0x2c(%rbx)
leaq 0x42(%rbx), %rcx
movl $0x52, %eax
movl $0x47, %edx
jmp 0x4e05
leaq 0x26(%rbx), %rcx
movb %al, 0x16(%rbx)
movb %dl, 0x17(%rbx)
movl $0x36, %eax
movl $0x2b, %edx
movq -0x40(%rbp), %r15
movb $0x29, (%rcx)
movabsq $0x2020202020202020, %rcx # imm = 0x2020202020202020
movq %rcx, (%rbx,%rdx)
movl $0x20202020, 0x7(%rbx,%rdx) # imm = 0x20202020
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rbx,%rax)
movl $0x1, %edx
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq *0x10(%r15)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x4e6f
cmpl $0x20, 0x2c(%r14)
jne 0x4e6c
movl $0x6, %esi
movl -0x2c(%rbp), %ecx
shrl %cl, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq *0x10(%r15)
testl %eax, %eax
js 0x4e6f
xorl %r12d, %r12d
movl %r12d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq