// File: /workspace/github_c_makefile_star2_O2/lb3361[P]dosfs/ff.c
// Function: f_setlabel
FRESULT f_setlabel (
	const TCHAR* label	/* Volume label to set with heading logical drive number */
)
{
	FRESULT res;
	DIR dj;
	FATFS *fs;
	BYTE dirvn[22];
	UINT di;
	WCHAR wc;
	static const char badchr[] = "+.,;=[]/\\\"*:<>\?|\x7F";	/* [0..] for FAT, [7..] for exFAT */
#if FF_USE_LFN
	DWORD dc;
#endif

	/* Get logical drive */
	res = mount_volume(&label, &fs, FA_WRITE);
	if (res != FR_OK) LEAVE_FF(fs, res);

#if FF_FS_EXFAT
	if (fs->fs_type == FS_EXFAT) {	/* On the exFAT volume */
		mem_set(dirvn, 0, 22);
		di = 0;
		while ((UINT)*label >= ' ') {	/* Create volume label */
			dc = tchar2uni(&label);	/* Get a Unicode character */
			if (dc >= 0x10000) {
				if (dc == 0xFFFFFFFF || di >= 10) {	/* Wrong surrogate or buffer overflow */
					dc = 0;
				} else {
					st_word(dirvn + di * 2, (WCHAR)(dc >> 16)); di++;
				}
			}
			if (dc == 0 || chk_chr(badchr + 7, (int)dc) || di >= 11) {	/* Check validity of the volume label */
				LEAVE_FF(fs, FR_INVALID_NAME);
			}
			st_word(dirvn + di * 2, (WCHAR)dc); di++;
		}
	} else
#endif
	{	/* On the FAT/FAT32 volume */
		mem_set(dirvn, ' ', 11);
		di = 0;
		while ((UINT)*label >= ' ') {	/* Create volume label */
#if FF_USE_LFN
			dc = tchar2uni(&label);
			wc = (dc < 0x10000) ? ff_uni2oem(ff_wtoupper(dc), CODEPAGE) : 0;
#else									/* ANSI/OEM input */
			wc = (BYTE)*label++;
			if (dbc_1st((BYTE)wc)) wc = dbc_2nd((BYTE)*label) ? wc << 8 | (BYTE)*label++ : 0;
			if (IsLower(wc)) wc -= 0x20;		/* To upper ASCII characters */
#if FF_CODE_PAGE == 0
			if (ExCvt && wc >= 0x80) wc = ExCvt[wc - 0x80];	/* To upper extended characters (SBCS cfg) */
#elif FF_CODE_PAGE < 900
			if (wc >= 0x80) wc = ExCvt[wc - 0x80];	/* To upper extended characters (SBCS cfg) */
#endif
#endif
			if (wc == 0 || chk_chr(badchr + 0, (int)wc) || di >= (UINT)((wc >= 0x100) ? 10 : 11)) {	/* Reject invalid characters for volume label */
				LEAVE_FF(fs, FR_INVALID_NAME);
			}
			if (wc >= 0x100) dirvn[di++] = (BYTE)(wc >> 8);
			dirvn[di++] = (BYTE)wc;
		}
		if (dirvn[0] == DDEM) LEAVE_FF(fs, FR_INVALID_NAME);	/* Reject illegal name (heading DDEM) */
		while (di && dirvn[di - 1] == ' ') di--;				/* Snip trailing spaces */
	}

	/* Set volume label */
	dj.obj.fs = fs; dj.obj.sclust = 0;	/* Open root directory */
	res = dir_sdi(&dj, 0);
	if (res == FR_OK) {
		res = DIR_READ_LABEL(&dj);	/* Get volume label entry */
		if (res == FR_OK) {
			if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {
				dj.dir[XDIR_NumLabel] = (BYTE)di;	/* Change the volume label */
				mem_cpy(dj.dir + XDIR_Label, dirvn, 22);
			} else {
				if (di != 0) {
					mem_cpy(dj.dir, dirvn, 11);	/* Change the volume label */
				} else {
					dj.dir[DIR_Name] = DDEM;	/* Remove the volume label */
				}
			}
			fs->wflag = 1;
			res = sync_fs(fs);
		} else {			/* No volume label entry or an error */
			if (res == FR_NO_FILE) {
				res = FR_OK;
				if (di != 0) {	/* Create a volume label entry */
					res = dir_alloc(&dj, 1);	/* Allocate an entry */
					if (res == FR_OK) {
						mem_set(dj.dir, 0, SZDIRE);	/* Clean the entry */
						if (FF_FS_EXFAT && fs->fs_type == FS_EXFAT) {
							dj.dir[XDIR_Type] = ET_VLABEL;	/* Create volume label entry */
							dj.dir[XDIR_NumLabel] = (BYTE)di;
							mem_cpy(dj.dir + XDIR_Label, dirvn, 22);
						} else {
							dj.dir[DIR_Attr] = AM_VOL;		/* Create volume label entry */
							mem_cpy(dj.dir, dirvn, 11);
						}
						fs->wflag = 1;
						res = sync_fs(fs);
					}
				}
			}
		}
	}

	LEAVE_FF(fs, res);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
leaq 0x20(%rsp), %rax
movq %rdi, (%rax)
leaq 0x30(%rsp), %rsi
pushq $0x2
popq %rdx
movq %rax, %rdi
callq 0x473a
testl %eax, %eax
je 0x8511
movl %eax, %r15d
movl %r15d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x30(%rsp), %rbp
cmpb $0x4, (%rbp)
jne 0x85ba
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0xe(%rsp)
xorl %r13d, %r13d
leaq 0x20(%rsp), %r14
pushq $0x6
popq %r15
leaq 0x3e77(%rip), %rbx      # 0xc3b7
movq 0x20(%rsp), %rax
cmpb $0x20, (%rax)
jb 0x866e
movq %r14, %rdi
callq 0x8767
movl %eax, %r12d
cmpl $0x10000, %eax          # imm = 0x10000
jb 0x8587
cmpl $-0x1, %r12d
je 0x84fc
cmpl $0x9, %r13d
ja 0x84fc
movl %r13d, %eax
movl %r12d, %ecx
shrl $0x10, %ecx
movb %cl, (%rsp,%rax,2)
movl %r12d, %ecx
shrl $0x18, %ecx
movb %cl, 0x1(%rsp,%rax,2)
incl %r13d
jmp 0x8590
testl %r12d, %r12d
je 0x84fc
movq %rbx, %rdi
movl %r12d, %esi
callq 0x8848
testl %eax, %eax
jne 0x84fc
cmpl $0xa, %r13d
ja 0x84fc
movl %r13d, %eax
movw %r12w, (%rsp,%rax,2)
incl %r13d
jmp 0x8540
movq %rbp, 0x28(%rsp)
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, (%rsp)
movl $0x20202020, 0x7(%rsp)  # imm = 0x20202020
xorl %r13d, %r13d
leaq 0x20(%rsp), %r14
pushq $0x6
popq %r15
leaq 0x3dc8(%rip), %r12      # 0xc3b0
movq 0x20(%rsp), %rax
cmpb $0x20, (%rax)
movl %r13d, %ebp
jb 0x86c9
movq %r14, %rdi
callq 0x8767
cmpl $0xffff, %eax           # imm = 0xFFFF
ja 0x84fc
movl %eax, %edi
callq 0xad7c
movl %eax, %edi
movl $0x1b5, %esi            # imm = 0x1B5
callq 0xad04
testw %ax, %ax
je 0x84fc
movl %eax, %ebx
movzwl %ax, %esi
movq %r12, %rdi
callq 0x8848
testl %eax, %eax
jne 0x84fc
cmpw $0x100, %bx             # imm = 0x100
movl $0x0, %eax
adcl $0xa, %eax
cmpl %eax, %r13d
jae 0x84fc
cmpw $0x100, %bx             # imm = 0x100
jb 0x8660
incl %r13d
movb %bh, (%rsp,%rbp)
movl %r13d, %eax
incl %r13d
movb %bl, (%rsp,%rax)
jmp 0x85e8
movq %rbp, 0x28(%rsp)
leaq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rbx
movq %rbx, (%rdi)
andl $0x0, 0xc(%rdi)
xorl %esi, %esi
callq 0x6d3b
testl %eax, %eax
jne 0x84f9
leaq 0x38(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x6ee7
cmpl $0x4, %eax
je 0x86ee
movl %eax, %r15d
testl %eax, %eax
jne 0x84fc
cmpb $0x4, (%rbx)
movq 0x78(%rsp), %rdi
jne 0x8729
movb %r13b, 0x1(%rdi)
addq $0x2, %rdi
movq %rsp, %rsi
pushq $0x16
jmp 0x8744
cmpb $-0x1b, (%rsp)
je 0x84fc
incl %r13d
movq %rbp, %rax
subq $0x1, %rax
jb 0x8721
decl %r13d
cmpb $0x20, -0x1(%rsp,%rbp)
movq %rax, %rbp
je 0x86d9
jmp 0x8673
testl %r13d, %r13d
je 0x8733
leaq 0x38(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x8857
testl %eax, %eax
jne 0x84f9
movq 0x78(%rsp), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
cmpb $0x4, (%rbx)
jne 0x873b
movb $-0x7d, (%rdi)
jmp 0x86ba
xorl %r13d, %r13d
jmp 0x8673
testl %r13d, %r13d
jne 0x873f
movb $-0x1b, (%rdi)
jmp 0x874a
xorl %r15d, %r15d
jmp 0x84fc
movb $0x8, 0xb(%rdi)
movq %rsp, %rsi
pushq $0xb
popq %rdx
callq 0x60c7
movb $0x1, 0x3(%rbx)
movq %rbx, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x68ea