// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x30(%rsp), %rsi
movq %rdx, %rdi
callq 0x403c
movl %eax, %r15d
testl %eax, %eax
js 0x2ae3
leaq 0x1c(%r14), %r12
leaq 0x25(%r14), %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3b1a
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3d44
testl %eax, %eax
js 0x2ae6
leal 0xc(%r15), %ecx
imulq $0x4ec4ec4f, %rcx, %rbp # imm = 0x4EC4EC4F
shrq $0x22, %rbp
testl %eax, %eax
je 0x2af8
movq %r12, %rdi
callq 0x3c79
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3d44
testl %eax, %eax
jns 0x2ac3
jmp 0x2ae6
movl %r15d, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq %r13, %rsi
callq 0x3fe1
movl %eax, %r12d
leal 0x1(%rbp), %esi
movq %rbx, %rdi
callq 0x3839
testl %eax, %eax
js 0x2ae6
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r14)
movl 0x8(%rbx), %eax
movl %eax, 0x18(%r14)
testl %r15d, %r15d
je 0x2ba0
movl %ebp, %r15d
leal (%rbp,%rbp,2), %eax
leal -0xd(%rbp,%rax,4), %eax
movq %rax, 0x8(%rsp)
xorl %r13d, %r13d
movzbl %r12b, %ebp
leaq 0x10(%rsp), %r12
movq 0x8(%rsp), %rax
addl %r13d, %eax
cltq
leaq (%rsp,%rax,2), %rdi
addq $0x30, %rdi
xorl %edx, %edx
testq %r13, %r13
sete %dl
movl %r15d, %esi
movq %r12, %rcx
movl %ebp, %r8d
callq 0x4157
movq %rbx, %rdi
movq %r12, %rsi
movl $0x20, %edx
callq 0x3624
testl %eax, %eax
js 0x2ae6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x369f
testl %eax, %eax
js 0x2ae6
addq $-0xd, %r13
decq %r15
jne 0x2b45
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3a1d
movq 0x10(%rbx), %rax
movq %rax, (%r14)
movl 0x8(%rbx), %eax
movl %eax, 0x8(%r14)
movq %rbx, %rdi
movq %r15, %rsi
movl $0x20, %edx
callq 0x3624
jmp 0x2ae6