// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rax
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 0x41f3
cmpl $0x10, %r15d
je 0x41ea
cmpl $0xc, %r15d
jne 0x41fc
leaq 0x1fae(%rip), %rcx      # 0x6196
jmp 0x4203
leaq 0x1fae(%rip), %rcx      # 0x619f
jmp 0x4203
leaq 0x1fae(%rip), %rcx      # 0x61a8
jmp 0x4203
leaq 0x1f8a(%rip), %rcx      # 0x618d
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
pushq $0x1
popq %rax
movl %eax, %edx
movl %r12d, %ecx
shll %cl, %edx
movb %dl, 0xb(%rbx)
movb %dh, 0xc(%rbx)
movl 0x4(%r14), %ecx
movl -0x2c(%rbp), %r8d
subl %r8d, %ecx
shll %cl, %eax
movb %al, 0xd(%rbx)
movq 0x8(%r14), %rax
movl %r8d, %ecx
shlq %cl, %rax
movb %al, 0xe(%rbx)
movb %ah, 0xf(%rbx)
movb $0x2, 0x10(%rbx)
movq 0x10(%r14), %rdx
addb $-0x5, %r13b
movl %r13d, %ecx
shlq %cl, %rdx
movl %r8d, %eax
movw %dx, 0x11(%rbx)
cmpl $0x20, %r15d
setne %sil
movq 0x20(%r14), %rdx
cmpq $0x10000, %rdx          # imm = 0x10000
setb %dil
movl %r8d, %ecx
shlq %cl, %rdx
andb %sil, %dil
movl %edx, %ecx
shrl $0x8, %ecx
cmpb $0x1, %dil
jne 0x42c1
movb %dl, 0x13(%rbx)
movb %cl, 0x14(%rbx)
movb $-0x8, 0x15(%rbx)
movq -0x40(%rbp), %r15
jmp 0x42e7
movl %edx, %esi
shrl $0x10, %esi
movl %edx, %edi
shrl $0x18, %edi
movb %dl, 0x20(%rbx)
movb %cl, 0x21(%rbx)
movb %sil, 0x22(%rbx)
movb %dil, 0x23(%rbx)
movb $-0x8, 0x15(%rbx)
cmpl $0x20, %r15d
movq -0x40(%rbp), %r15
je 0x4304
movq 0x18(%r14), %rdx
movl %eax, %ecx
shlq %cl, %rdx
movb %dl, 0x16(%rbx)
shrl $0x8, %edx
pushq $0x36
popq %rax
pushq $0x2b
popq %rcx
pushq $0x26
popq %rsi
pushq $0x17
popq %rdi
jmp 0x4340
movq 0x18(%r14), %rdx
movl %eax, %ecx
shlq %cl, %rdx
movb %dl, 0x24(%rbx)
movb %dh, 0x25(%rbx)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x26(%rbx)
shrl $0x18, %edx
movb %dl, 0x27(%rbx)
movl $0x2, 0x2c(%rbx)
movw $0x1, 0x30(%rbx)
movb $0x6, 0x32(%rbx)
pushq $0x52
popq %rax
pushq $0x47
popq %rcx
pushq $0x42
popq %rsi
pushq $0x33
popq %rdi
xorl %edx, %edx
movb %dl, (%rbx,%rdi)
movb $0x29, (%rbx,%rsi)
movabsq $0x2020202020202020, %rdx # imm = 0x2020202020202020
movq %rdx, (%rbx,%rcx)
movl $0x20202020, 0x7(%rbx,%rcx) # imm = 0x20202020
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rbx,%rax)
pushq $0x1
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq *0x10(%r15)
pushq $-0x1
popq %r12
testl %eax, %eax
js 0x43a2
cmpl $0x20, 0x2c(%r14)
jne 0x439f
pushq $0x6
popq %rsi
movl -0x2c(%rbp), %ecx
shrl %cl, %esi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rcx
callq *0x10(%r15)
testl %eax, %eax
js 0x43a2
xorl %r12d, %r12d
movl %r12d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq