// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movb 0x53(%rdi), %bpl
movl $0x6, %eax
testb $-0x60, %bpl
je 0x5a33
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
movq (%rdi), %rbx
leaq 0x48(%rdi), %r12
movq 0x10(%rbx), %rax
movl $0xb, %r13d
leal -0xb(%r13), %ecx
incl %r13d
cmpw $0x0, (%rax,%rcx,2)
jne 0x5a47
cmpb $0x4, (%rbx)
jne 0x5b32
addl $0x2, %r13d
movl $0x88888889, %r15d      # imm = 0x88888889
imulq %r13, %r15
shrq $0x23, %r15
leal 0x2(%r15), %esi
movq %r14, %rdi
callq 0x9155
testl %eax, %eax
jne 0x5a24
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 0x5df9
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %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
movl $0x40, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
movl %r8d, %esi
movl %eax, %r8d
movw $0xc1, (%rbx,%r8)
addl $0x2, %eax
testw %dx, %dx
je 0x5af6
movzbl %cl, %ecx
movzwl (%rdi,%rcx,2), %edx
cmpw $0x1, %dx
sbbb $-0x1, %cl
jmp 0x5af8
xorl %edx, %edx
movl %eax, %r8d
movw %dx, (%rbx,%r8)
addl $0x2, %eax
testb $0x1f, %al
jne 0x5ae1
leal 0x1(%rsi), %r8d
movzbl %cl, %r9d
cmpw $0x0, (%rdi,%r9,2)
jne 0x5ad1
movb %cl, 0x23(%rbx)
addb $0x2, %sil
movb %sil, 0x1(%rbx)
callq 0xb5bd
movw %ax, 0x24(%rbx)
xorl %eax, %eax
jmp 0x5a24
movl 0x7(%r12), %eax
movl %eax, 0xf(%rsp)
movq (%r12), %rax
movq %rax, 0x8(%rsp)
testb $0x1, %bpl
je 0x5c6c
movb $0x40, 0x53(%r14)
movl $0x1, %r15d
movq 0x10(%rbx), %rax
movl 0xf(%rsp), %ecx
movl %ecx, 0x7(%r12)
movq 0x8(%rsp), %rcx
movq %rcx, (%r12)
movl %r15d, %ecx
cmpl $0x5, %r15d
jbe 0x5bbd
movzwl (%rax), %edx
movl %r15d, %ecx
movl %r15d, %edi
testw %dx, %dx
je 0x5bbd
movl $0x10, %esi
movl %edi, %ecx
movl %edx, %edi
andl $0x1, %edi
movzwl %dx, %edx
leal (%rdi,%rcx,2), %edi
shrl %edx
movl %edi, %r8d
xorl $0x11021, %r8d          # imm = 0x11021
testw %cx, %cx
movl %r8d, %ecx
cmovnsl %edi, %ecx
decl %esi
jne 0x5b8d
movzwl 0x2(%rax), %edx
addq $0x2, %rax
movl %ecx, %edi
jmp 0x5b81
movl $0x7, %eax
movl $0x6, %esi
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, (%rsp,%rsi)
shrl $0x4, %edx
leal -0x1(%rdi), %esi
cmpl $0xf, %ecx
movl %edx, %ecx
ja 0x5bc9
movl %eax, %ecx
movb $0x7e, (%rsp,%rcx)
testl %eax, %eax
je 0x5c1f
movl %edi, %edx
xorl %ecx, %ecx
cmpb $0x20, (%r12,%rcx)
je 0x5c21
incq %rcx
cmpq %rcx, %rdx
jne 0x5c0c
movl %eax, %ecx
jmp 0x5c21
xorl %ecx, %ecx
movb $0x20, %dl
cmpl $0x7, %eax
ja 0x5c2f
movl %eax, %edx
incl %eax
movb (%rsp,%rdx), %dl
movl %ecx, %esi
incl %ecx
movb %dl, (%r12,%rsi)
cmpl $0x8, %ecx
jb 0x5c21
movq %r14, %rdi
callq 0xb30e
testl %eax, %eax
jne 0x5c5f
incl %r15d
cmpl $0x64, %r15d
jne 0x5b59
movl $0x7, %eax
jmp 0x5a24
cmpl $0x4, %eax
jne 0x5a24
movb %bpl, 0x53(%r14)
movl $0x1, %r15d
testb $0x2, %bpl
je 0x5c89
movl %r13d, %eax
imulq $0x4ec4ec4f, %rax, %r15 # imm = 0x4EC4EC4F
shrq $0x22, %r15
incl %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x9155
testl %eax, %eax
jne 0x5a24
decl %r15d
je 0x5dac
movl 0x30(%r14), %esi
movl %r15d, %eax
shll $0x5, %eax
subl %eax, %esi
movq %r14, %rdi
callq 0x72ea
testl %eax, %eax
jne 0x5a24
movl $0x48, %eax
xorl %ebp, %ebp
rorb %bpl
addb (%r14,%rax), %bpl
incq %rax
cmpl $0x53, %eax
jne 0x5cc8
leaq 0x7752(%rip), %r13      # 0xd430
movq 0x38(%r14), %rsi
movq %rbx, %rdi
callq 0x6114
testl %eax, %eax
jne 0x5a24
movq 0x10(%rbx), %rcx
movq 0x40(%r14), %rax
movb %bpl, 0xd(%rax)
movw $0xf, 0xb(%rax)
movw $0x0, 0x1a(%rax)
movzbl %r15b, %edx
leal (%rdx,%rdx,2), %esi
leal (%rdx,%rsi,4), %edx
addl $-0xd, %edx
xorl %esi, %esi
xorl %edi, %edi
cmpw $-0x1, %di
sete %dil
testb $0x1, %dil
je 0x5d42
movzbl (%rsi,%r13), %edi
movw $0xffff, (%rax,%rdi)    # imm = 0xFFFF
incq %rsi
movb $0x1, %dil
cmpq $0xd, %rsi
jne 0x5d23
jmp 0x5d85
movl %edx, %edi
incl %edx
movzwl (%rcx,%rdi,2), %r8d
movzbl (%rsi,%r13), %edi
movw %r8w, (%rax,%rdi)
cmpw $0x1, %r8w
movl $0x0, %edi
sbbl %edi, %edi
orl %r8d, %edi
leaq 0x1(%rsi), %r8
cmpq $0xc, %rsi
movq %r8, %rsi
jne 0x5d1b
cmpw $-0x1, %di
je 0x5d85
movl %edx, %edx
cmpw $0x0, (%rcx,%rdx,2)
je 0x5d85
movl %r15d, %ecx
jmp 0x5d8b
movl %r15d, %ecx
orb $0x40, %cl
movb %cl, (%rax)
movb $0x1, 0x3(%rbx)
movq %r14, %rdi
xorl %esi, %esi
callq 0x7952
testl %eax, %eax
jne 0x5a24
decl %r15d
jne 0x5cde
movq 0x38(%r14), %rsi
movq %rbx, %rdi
callq 0x6114
testl %eax, %eax
jne 0x5a24
movq 0x40(%r14), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x40(%r14), %rax
xorl %ecx, %ecx
movb (%r12,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpl $0xb, %ecx
jne 0x5dd4
movb 0x53(%r14), %al
andb $0x18, %al
movq 0x40(%r14), %rcx
movb %al, 0xc(%rcx)
movb $0x1, 0x3(%rbx)
jmp 0x5b2b
andb $-0x5, %al
movb %al, 0xb(%r14)
movq %r14, %rdi
callq 0x6dc4
testl %eax, %eax
jne 0x5a24
movl 0x34(%r14), %esi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6e11
testl %eax, %eax
jne 0x5a24
cmpl $0x0, 0xc(%r14)
je 0x5aa1
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x6e63
testl %eax, %eax
jne 0x5a24
movzwl 0xa(%rbx), %eax
shll $0x9, %eax
movq 0x10(%r14), %rdx
addq %rdx, %rax
movq %rax, 0x10(%r14)
movq 0x18(%rbx), %rcx
movb %dl, 0x38(%rcx)
movb %ah, 0x39(%rcx)
movl %eax, %edx
shrl $0x10, %edx
movb %dl, 0x3a(%rcx)
movl %eax, %edx
shrl $0x18, %edx
movb %dl, 0x3b(%rcx)
movq %rax, %rdx
shrq $0x20, %rdx
movb %dl, 0x3c(%rcx)
movq %rax, %rdx
shrq $0x28, %rdx
movb %dl, 0x3d(%rcx)
movq %rax, %rdx
shrq $0x30, %rdx
movb %dl, 0x3e(%rcx)
shrq $0x38, %rax
movb %al, 0x3f(%rcx)
movq 0x18(%rbx), %rax
movq 0x10(%r14), %rcx
movq %rcx, 0x28(%rax)
movb 0xb(%r14), %al
orb $0x1, %al
movq 0x18(%rbx), %rcx
movb %al, 0x21(%rcx)
leaq 0x8(%rsp), %rdi
callq 0x5ee7
testl %eax, %eax
jne 0x5a24
jmp 0x5aa1