// File: /workspace/github_c_makefile_star2_O2/lb3361[P]dosfs/ff.c
// Function: dir_register
static FRESULT dir_register (	/* FR_OK:succeeded, FR_DENIED:no free entry or too many SFN collision, FR_DISK_ERR:disk error */
	DIR* dp						/* Target directory with object name to be created */
)
{
	FRESULT res;
	FATFS *fs = dp->obj.fs;
#if FF_USE_LFN		/* LFN configuration */
	UINT n, len, n_ent;
	BYTE sn[12], sum;


	if (dp->fn[NSFLAG] & (NS_DOT | NS_NONAME)) return FR_INVALID_NAME;	/* Check name validity */
	for (len = 0; fs->lfnbuf[len]; len++) ;	/* Get lfn length */

#if FF_FS_EXFAT
	if (fs->fs_type == FS_EXFAT) {	/* On the exFAT volume */
		n_ent = (len + 14) / 15 + 2;	/* Number of entries to allocate (85+C0+C1s) */
		res = dir_alloc(dp, n_ent);		/* Allocate directory entries */
		if (res != FR_OK) return res;
		dp->blk_ofs = dp->dptr - SZDIRE * (n_ent - 1);	/* Set the allocated entry block offset */

		if (dp->obj.stat & 4) {			/* Has the directory been stretched by new allocation? */
			dp->obj.stat &= ~4;
			res = fill_first_frag(&dp->obj);	/* Fill the first fragment on the FAT if needed */
			if (res != FR_OK) return res;
			res = fill_last_frag(&dp->obj, dp->clust, 0xFFFFFFFF);	/* Fill the last fragment on the FAT if needed */
			if (res != FR_OK) return res;
			if (dp->obj.sclust != 0) {		/* Is it a sub-directory? */
				DIR dj;

				res = load_obj_xdir(&dj, &dp->obj);	/* Load the object status */
				if (res != FR_OK) return res;
				dp->obj.objsize += (DWORD)fs->csize * SS(fs);			/* Increase the directory size by cluster size */
				st_qword(fs->dirbuf + XDIR_FileSize, dp->obj.objsize);
				st_qword(fs->dirbuf + XDIR_ValidFileSize, dp->obj.objsize);
				fs->dirbuf[XDIR_GenFlags] = dp->obj.stat | 1;			/* Update the allocation status */
				res = store_xdir(&dj);				/* Store the object status */
				if (res != FR_OK) return res;
			}
		}

		create_xdir(fs->dirbuf, fs->lfnbuf);	/* Create on-memory directory block to be written later */
		return FR_OK;
	}
#endif
	/* On the FAT/FAT32 volume */
	mem_cpy(sn, dp->fn, 12);
	if (sn[NSFLAG] & NS_LOSS) {			/* When LFN is out of 8.3 format, generate a numbered name */
		dp->fn[NSFLAG] = NS_NOLFN;		/* Find only SFN */
		for (n = 1; n < 100; n++) {
			gen_numname(dp->fn, sn, fs->lfnbuf, n);	/* Generate a numbered name */
			res = dir_find(dp);				/* Check if the name collides with existing SFN */
			if (res != FR_OK) break;
		}
		if (n == 100) return FR_DENIED;		/* Abort if too many collisions */
		if (res != FR_NO_FILE) return res;	/* Abort if the result is other than 'not collided' */
		dp->fn[NSFLAG] = sn[NSFLAG];
	}

	/* Create an SFN with/without LFNs. */
	n_ent = (sn[NSFLAG] & NS_LFN) ? (len + 12) / 13 + 1 : 1;	/* Number of entries to allocate */
	res = dir_alloc(dp, n_ent);		/* Allocate entries */
	if (res == FR_OK && --n_ent) {	/* Set LFN entry if needed */
		res = dir_sdi(dp, dp->dptr - n_ent * SZDIRE);
		if (res == FR_OK) {
			sum = sum_sfn(dp->fn);	/* Checksum value of the SFN tied to the LFN */
			do {					/* Store LFN entries in bottom first */
				res = move_window(fs, dp->sect);
				if (res != FR_OK) break;
				put_lfn(fs->lfnbuf, dp->dir, (BYTE)n_ent, sum);
				fs->wflag = 1;
				res = dir_next(dp, 0);	/* Next entry */
			} while (res == FR_OK && --n_ent);
		}
	}

#else	/* Non LFN configuration */
	res = dir_alloc(dp, 1);		/* Allocate an entry for SFN */

#endif

	/* Set SFN entry */
	if (res == FR_OK) {
		res = move_window(fs, dp->sect);
		if (res == FR_OK) {
			mem_set(dp->dir, 0, SZDIRE);	/* Clean the entry */
			mem_cpy(dp->dir + DIR_Name, dp->fn, 11);	/* Put SFN */
#if FF_USE_LFN
			dp->dir[DIR_NTres] = dp->fn[NSFLAG] & (NS_BODY | NS_EXT);	/* Put NT flag */
#endif
			fs->wflag = 1;
		}
	}

	return res;
}

// Assembly:
testb $-0x60, 0x53(%rdi)
je 0x5564
pushq $0x6
popq %rax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %r14
movq (%rdi), %rbp
leaq 0x48(%rdi), %r15
movq 0x10(%rbp), %rax
pushq $0xb
popq %r12
leal -0xb(%r12), %ecx
incl %r12d
cmpw $0x0, (%rax,%rcx,2)
jne 0x5584
cmpb $0x4, (%rbp)
jne 0x5672
addl $0x2, %r12d
pushq $0xf
popq %rcx
movl %r12d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
leal 0x2(%r15), %esi
movq %r14, %rdi
callq 0x8857
testl %eax, %eax
jne 0x5994
movl 0x30(%r14), %eax
shll $0x5, %r15d
negl %r15d
addl %r15d, %eax
addl $-0x20, %eax
movl %eax, 0x54(%r14)
movb 0xb(%r14), %al
testb $0x4, %al
jne 0x5903
movq 0x10(%rbp), %rdi
movq 0x18(%rbp), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x11(%rbx)
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x21(%rbx)
movups %xmm0, 0x1(%rbx)
movb $-0x7b, (%rbx)
movb $-0x40, 0x20(%rbx)
movw $0x1, %dx
pushq $0x40
popq %rax
xorl %r8d, %r8d
xorl %ecx, %ecx
movl %r8d, %esi
movl %eax, %r8d
movw $0xc1, (%rbx,%r8)
addl $0x2, %eax
testw %dx, %dx
je 0x5636
movzbl %cl, %ecx
movzwl (%rdi,%rcx,2), %edx
cmpw $0x1, %dx
sbbb $-0x1, %cl
jmp 0x5638
xorl %edx, %edx
movl %eax, %r8d
movw %dx, (%rbx,%r8)
addl $0x2, %eax
testb $0x1f, %al
jne 0x5621
leal 0x1(%rsi), %r8d
movzbl %cl, %r9d
cmpw $0x0, (%rdi,%r9,2)
jne 0x5611
movb %cl, 0x23(%rbx)
addb $0x2, %sil
movb %sil, 0x1(%rbx)
callq 0xa82a
movw %ax, 0x24(%rbx)
xorl %eax, %eax
jmp 0x5994
leaq 0x18(%rsp), %r13
pushq $0xc
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x60c7
movb 0xb(%r13), %al
testb $0x1, %al
je 0x57aa
movb %al, 0x7(%rsp)
pushq $0x1
popq %r13
movb $0x40, 0x53(%r14)
pushq $0x10
popq %rbx
movq %rbp, 0x10(%rsp)
cmpl $0x64, %r13d
je 0x5991
movq 0x10(%rbp), %rbp
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
pushq $0xb
popq %rdx
callq 0x60c7
movl %r13d, %ecx
movl %r13d, %eax
cmpl $0x6, %r13d
jb 0x5707
movzwl (%rbp), %ecx
testw %cx, %cx
je 0x5705
movl %ebx, %edx
subl $0x1, %edx
jb 0x56ff
movl %ecx, %esi
andl $0x1, %esi
movzwl %cx, %ecx
leal (%rsi,%rax,2), %esi
shrl %ecx
movl %esi, %edi
xorl $0x11021, %edi          # imm = 0x11021
testw %ax, %ax
cmovnsl %esi, %edi
movl %edi, %eax
jmp 0x56db
addq $0x2, %rbp
jmp 0x56d0
movl %eax, %ecx
pushq $0x6
popq %rsi
pushq $0x7
popq %rax
movl %ecx, %edx
movl %ecx, %edi
andb $0xf, %dil
leal 0x30(%rdi), %r8d
addb $0x37, %dil
cmpb $0x3a, %r8b
movzbl %dil, %edi
movzbl %r8b, %r8d
cmovael %edi, %r8d
movl %esi, %edi
movl %eax, %esi
decl %eax
movb %r8b, 0x8(%rsp,%rsi)
shrl $0x4, %edx
leal -0x1(%rdi), %esi
cmpl $0xf, %ecx
movl %edx, %ecx
ja 0x570f
movl %eax, %ecx
movb $0x7e, 0x8(%rsp,%rcx)
movl %edi, %edx
xorl %ecx, %ecx
movq 0x10(%rsp), %rbp
cmpq %rcx, %rdx
je 0x5767
cmpb $0x20, 0x48(%r14,%rcx)
je 0x5769
incq %rcx
jmp 0x5755
movl %eax, %ecx
movb $0x20, %dl
cmpl $0x7, %eax
ja 0x5778
movl %eax, %edx
incl %eax
movb 0x8(%rsp,%rdx), %dl
movl %ecx, %esi
incl %ecx
movb %dl, (%r15,%rsi)
cmpl $0x8, %ecx
jb 0x5769
movq %r14, %rdi
callq 0xa5a0
testl %eax, %eax
jne 0x5799
incl %r13d
jmp 0x56a6
cmpl $0x4, %eax
jne 0x5994
movb 0x7(%rsp), %al
movb %al, 0x53(%r14)
testb $0x2, %al
jne 0x57b4
pushq $0x1
popq %r12
jmp 0x57c4
pushq $0xd
popq %rcx
movl %r12d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r12d
incl %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x8857
testl %eax, %eax
jne 0x5994
decl %r12d
je 0x58bc
movl 0x30(%r14), %esi
movl %r12d, %eax
shll $0x5, %eax
subl %eax, %esi
movq %r14, %rdi
callq 0x6d3b
testl %eax, %eax
jne 0x5994
movq %r15, %rdi
callq 0xa87f
movl %eax, %r13d
leaq 0x6c52(%rip), %rbx      # 0xc460
movq 0x38(%r14), %rsi
movq %rbp, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0x5994
movq 0x10(%rbp), %rcx
movq 0x40(%r14), %rax
movb %r13b, 0xd(%rax)
movw $0xf, 0xb(%rax)
andw $0x0, 0x1a(%rax)
movzbl %r12b, %edx
imull $0xd, %edx, %edx
addl $-0xd, %edx
xorl %esi, %esi
xorl %edi, %edi
cmpw $-0x1, %di
je 0x5857
movl %edx, %edi
incl %edx
movzwl (%rcx,%rdi,2), %edi
jmp 0x585b
movw $0xffff, %di            # imm = 0xFFFF
movzbl (%rsi,%rbx), %r8d
movw %di, (%rax,%r8)
testw %di, %di
je 0x5872
cmpq $0xc, %rsi
jne 0x587c
jmp 0x5881
movw $0xffff, %di            # imm = 0xFFFF
cmpq $0xc, %rsi
je 0x5895
incq %rsi
jmp 0x5847
cmpw $-0x1, %di
je 0x5895
movl %edx, %edx
cmpw $0x0, (%rcx,%rdx,2)
je 0x5895
movl %r12d, %ecx
jmp 0x589b
movl %r12d, %ecx
orb $0x40, %cl
movb %cl, (%rax)
movb $0x1, 0x3(%rbp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x7336
testl %eax, %eax
jne 0x5994
decl %r12d
jne 0x580e
movq 0x38(%r14), %rsi
movq %rbp, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0x5994
movq 0x40(%r14), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x40(%r14), %rdi
pushq $0xb
popq %rdx
movq %r15, %rsi
callq 0x60c7
movb 0x53(%r14), %al
andb $0x18, %al
movq 0x40(%r14), %rcx
movb %al, 0xc(%rcx)
movb $0x1, 0x3(%rbp)
jmp 0x566b
andb $-0x5, %al
movb %al, 0xb(%r14)
movq %r14, %rdi
callq 0x6820
testl %eax, %eax
jne 0x5994
movl 0x34(%r14), %esi
pushq $-0x1
popq %rdx
movq %r14, %rdi
callq 0x6868
testl %eax, %eax
jne 0x5994
cmpl $0x0, 0xc(%r14)
je 0x55e3
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x68a9
testl %eax, %eax
jne 0x5994
movzwl 0xa(%rbp), %esi
shll $0x9, %esi
addq 0x10(%r14), %rsi
movq %rsi, 0x10(%r14)
movq 0x18(%rbp), %rdi
addq $0x38, %rdi
callq 0x68e6
movq 0x18(%rbp), %rdi
addq $0x28, %rdi
movq 0x10(%r14), %rsi
callq 0x68e6
movb 0xb(%r14), %al
orb $0x1, %al
movq 0x18(%rbp), %rcx
movb %al, 0x21(%rcx)
leaq 0x18(%rsp), %rdi
callq 0x59d7
testl %eax, %eax
jne 0x5994
jmp 0x55e3
pushq $0x7
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq