// File: /workspace/github_c_makefile_star2_O0/dlbeer[P]ufat/ufat_dir.c
// Function: insert_dirent
static int insert_dirent(struct ufat_directory *dir, struct ufat_dirent *ent,
			 const char *long_name)
{
	uint16_t ucs2_name[UFAT_LFN_MAX_CHARS];
	uint8_t data[UFAT_DIRENT_SIZE];
	uint8_t checksum;
	int ucs2_len = ufat_utf8_to_ucs2(long_name, ucs2_name);
	int num_lfn_frags;
	int err;
	int i;

	/* Check that the UTF8 was encoded correctly */
	if (ucs2_len < 0)
		return ucs2_len;
	num_lfn_frags = (ucs2_len + 12) / 13;

	/* Choose a suitable short-name */
	ufat_short_first(long_name, ent->short_name, ent->short_ext);
	for (;;) {
		err = ufat_short_exists(dir, ent->short_name, ent->short_ext);
		if (err < 0)
			return err;

		if (!err)
			break;

		ufat_short_next(ent->short_name);
	}

	checksum = ufat_short_checksum(ent->short_name, ent->short_ext);

	/* Find a space in the directory */
	err = ufat_allocate_raw_dirent(dir, num_lfn_frags + 1);
	if (err < 0)
		return err;

	ent->lfn_block = dir->cur_block;
	ent->lfn_pos = dir->cur_pos;

	/* Write LFN fragments and the DOS dirent */
	for (i = 0; i < num_lfn_frags; i++) {
		ufat_lfn_pack_fragment(ucs2_name + (num_lfn_frags - i - 1) * 13,
				       num_lfn_frags - i, !i,
				       data, checksum);

		err = ufat_write_raw_dirent(dir, data, sizeof(data));
		if (err < 0)
			return err;

		err = ufat_advance_raw_dirent(dir, 0);
		if (err < 0)
			return err;
	}

	ufat_pack_dirent(ent, data);
	ent->dirent_block = dir->cur_block;
	ent->dirent_pos = dir->cur_pos;

	return ufat_write_raw_dirent(dir, data, sizeof(data));
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x258(%rsp)
movq %rsi, 0x250(%rsp)
movq %rdx, 0x248(%rsp)
movq 0x248(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x7680
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x18(%rsp)
jge 0x46dc
movl 0x18(%rsp), %eax
movl %eax, 0x264(%rsp)
jmp 0x490e
movl 0x18(%rsp), %eax
addl $0xc, %eax
movl $0xd, %ecx
cltd
idivl %ecx
movl %eax, 0x14(%rsp)
movq 0x248(%rsp), %rdi
movq 0x250(%rsp), %rsi
addq $0x1c, %rsi
movq 0x250(%rsp), %rdx
addq $0x25, %rdx
callq 0x6b70
movq 0x258(%rsp), %rdi
movq 0x250(%rsp), %rsi
addq $0x1c, %rsi
movq 0x250(%rsp), %rdx
addq $0x25, %rdx
callq 0x6fa0
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x4754
movl 0x10(%rsp), %eax
movl %eax, 0x264(%rsp)
jmp 0x490e
cmpl $0x0, 0x10(%rsp)
jne 0x475d
jmp 0x4770
movq 0x250(%rsp), %rdi
addq $0x1c, %rdi
callq 0x6e40
jmp 0x4714
movq 0x250(%rsp), %rdi
addq $0x1c, %rdi
movq 0x250(%rsp), %rsi
addq $0x25, %rsi
callq 0x7560
movb %al, 0x1f(%rsp)
movq 0x258(%rsp), %rdi
movl 0x14(%rsp), %esi
addl $0x1, %esi
callq 0x6440
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x47c0
movl 0x10(%rsp), %eax
movl %eax, 0x264(%rsp)
jmp 0x490e
movq 0x258(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x250(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x258(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x250(%rsp), %rax
movl %ecx, 0x18(%rax)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x14(%rsp), %eax
jge 0x48b1
leaq 0x40(%rsp), %rdi
movl 0x14(%rsp), %eax
subl 0xc(%rsp), %eax
subl $0x1, %eax
imull $0xd, %eax, %eax
cltq
shlq %rax
addq %rax, %rdi
movl 0x14(%rsp), %esi
subl 0xc(%rsp), %esi
cmpl $0x0, 0xc(%rsp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %edx
leaq 0x20(%rsp), %rcx
movzbl 0x1f(%rsp), %r8d
callq 0x78b0
movq 0x258(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x20, %edx
callq 0x5fc0
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x4878
movl 0x10(%rsp), %eax
movl %eax, 0x264(%rsp)
jmp 0x490e
movq 0x258(%rsp), %rdi
xorl %esi, %esi
callq 0x60e0
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x489f
movl 0x10(%rsp), %eax
movl %eax, 0x264(%rsp)
jmp 0x490e
jmp 0x48a1
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x47f6
movq 0x250(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x68a0
movq 0x258(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x250(%rsp), %rax
movq %rcx, (%rax)
movq 0x258(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x250(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x258(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl $0x20, %edx
callq 0x5fc0
movl %eax, 0x264(%rsp)
movl 0x264(%rsp), %eax
addq $0x268, %rsp            # imm = 0x268
retq
nopl (%rax)