// File: /workspace/github_c_makefile_star2_O1/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 0x425f
movl %eax, %r15d
testl %eax, %eax
js 0x2c0b
leaq 0x1c(%r14), %r12
leaq 0x25(%r14), %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3d27
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3f3f
testl %eax, %eax
js 0x2c0e
leal 0xc(%r15), %ecx
imulq $0x4ec4ec4f, %rcx, %rbp # imm = 0x4EC4EC4F
shrq $0x22, %rbp
testl %eax, %eax
je 0x2c20
movq %r12, %rdi
callq 0x3e7c
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3f3f
testl %eax, %eax
jns 0x2beb
jmp 0x2c0e
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 0x420a
movl %eax, %r12d
leal 0x1(%rbp), %esi
movq %rbx, %rdi
callq 0x39e9
testl %eax, %eax
js 0x2c0e
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r14)
movl 0x8(%rbx), %eax
movl %eax, 0x18(%r14)
testl %r15d, %r15d
je 0x2ce9
movabsq $-0xd00000000, %rax     # imm = 0xFFFFFFF300000000
movl %ebp, %r15d
movabsq $0xd00000000, %r13      # imm = 0xD00000000
imulq %r15, %r13
addq %rax, %r13
movl %r12d, %eax
xorl %r12d, %r12d
movzbl %al, %eax
movl %eax, 0xc(%rsp)
leaq (%r12,%r13), %rax
sarq $0x1f, %rax
leaq (%rsp,%rax), %rdi
addq $0x30, %rdi
xorl %edx, %edx
testq %r12, %r12
sete %dl
movl %r15d, %esi
leaq 0x10(%rsp), %rbp
movq %rbp, %rcx
movl 0xc(%rsp), %r8d
callq 0x4395
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x20, %edx
callq 0x37d8
testl %eax, %eax
js 0x2c0e
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3853
testl %eax, %eax
js 0x2c0e
movabsq $-0xd00000000, %rax     # imm = 0xFFFFFFF300000000
addq %rax, %r12
decq %r15
jne 0x2c80
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3c2a
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 0x37d8
jmp 0x2c0e