// File: /workspace/github_c_makefile_star2_O2/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 0x37e8
testl %eax, %eax
js 0x2496
addl $0xc, %eax
pushq $0xd
popq %rcx
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
leaq 0x1c(%r14), %r12
leaq 0x25(%r14), %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3359
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x353f
testl %eax, %eax
js 0x2496
je 0x23c6
movq %r12, %rdi
callq 0x34b4
jmp 0x23a4
movq %r12, %rdi
movq %r13, %rsi
callq 0x37ab
movl %eax, %ebp
leal 0x1(%r15), %esi
movq %rbx, %rdi
callq 0x3075
testl %eax, %eax
js 0x2496
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r14)
movl 0x8(%rbx), %eax
movq %r14, 0x8(%rsp)
movl %eax, 0x18(%r14)
movl %r15d, %esi
imull $0xd, %r15d, %r13d
addl $-0xd, %r13d
xorl %r12d, %r12d
movzbl %bpl, %ebp
leaq 0x10(%rsp), %r15
movq %rsi, %r14
subq $0x1, %r14
jb 0x2465
leal (%r12,%r13), %eax
cltq
leaq (%rsp,%rax,2), %rdi
addq $0x30, %rdi
xorl %edx, %edx
testq %r12, %r12
sete %dl
movq %r15, %rcx
movl %ebp, %r8d
callq 0x38eb
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x20
popq %rdx
callq 0x2e74
testl %eax, %eax
js 0x2496
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2eed
addq $-0xd, %r12
movq %r14, %rsi
testl %eax, %eax
jns 0x2415
jmp 0x2496
leaq 0x10(%rsp), %r15
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x325f
movq 0x10(%rbx), %rax
movq %rax, (%r14)
movl 0x8(%rbx), %eax
movl %eax, 0x8(%r14)
pushq $0x20
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2e74
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq