// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x90, %rsp
leaq 0x8(%rsp), %rax
movq %rdi, (%rax)
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
movl $0x2, %edx
callq 0x4ada
testl %eax, %eax
je 0x8da9
movl %eax, %ebp
movl %ebp, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rbx
cmpb $0x4, (%rbx)
jne 0x8e68
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x1e(%rsp)
movq 0x8(%rsp), %rax
cmpb $0x20, (%rax)
jb 0x8f55
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
leaq 0x4672(%rip), %r12      # 0xd457
movq %r14, %rdi
callq 0x9045
cmpl $0x10000, %eax          # imm = 0x10000
jb 0x8e1e
movl $0x6, %ebp
cmpl $-0x1, %eax
je 0x8d97
cmpl $0x9, %r15d
ja 0x8d97
movl %r15d, %ecx
movl %eax, %edx
shrl $0x10, %edx
movb %dl, 0x10(%rsp,%rcx,2)
movl %eax, %edx
shrl $0x18, %edx
movb %dl, 0x11(%rsp,%rcx,2)
incl %r15d
jmp 0x8e26
testl %eax, %eax
je 0x8feb
movq %r12, %rdx
movsbl (%rdx), %ecx
testl %ecx, %ecx
je 0x8e37
incq %rdx
cmpl %ecx, %eax
jne 0x8e29
movl $0x6, %ebp
testb %cl, %cl
jne 0x8d97
cmpl $0xa, %r15d
ja 0x8d97
movl %r15d, %ecx
movw %ax, 0x10(%rsp,%rcx,2)
incl %r15d
movq 0x8(%rsp), %rax
cmpb $0x1f, (%rax)
ja 0x8de5
jmp 0x8f58
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x10(%rsp)
movl $0x20202020, 0x17(%rsp) # imm = 0x20202020
movq 0x8(%rsp), %rax
xorl %r12d, %r12d
cmpb $0x20, (%rax)
jb 0x8f3c
leaq 0x8(%rsp), %r14
leaq 0x45b4(%rip), %r15      # 0xd450
movl $0x6, %ebp
movq %r14, %rdi
callq 0x9045
cmpl $0xffff, %eax           # imm = 0xFFFF
ja 0x8d97
movl %eax, %edi
callq 0xbab9
movl %eax, %edi
movl $0x1b5, %esi            # imm = 0x1B5
callq 0xba34
testw %ax, %ax
je 0x8d97
movzwl %ax, %ecx
movq %r15, %rdx
movsbl (%rdx), %esi
testl %esi, %esi
je 0x8ee4
incq %rdx
cmpl %ecx, %esi
jne 0x8ed6
testb %sil, %sil
jne 0x8d97
cmpl $0x100, %ecx            # imm = 0x100
movl $0x0, %edx
adcl $0xa, %edx
cmpl %edx, %r12d
jae 0x8d97
cmpl $0x100, %ecx            # imm = 0x100
jb 0x8f16
movl %r12d, %ecx
incl %r12d
movb %ah, 0x10(%rsp,%rcx)
movl %r12d, %ecx
incl %r12d
movb %al, 0x10(%rsp,%rcx)
movq 0x8(%rsp), %rax
cmpb $0x1f, (%rax)
ja 0x8ea1
cmpb $-0x1b, 0x10(%rsp)
je 0x8d97
movl %r12d, %r12d
movq %r12, %r15
subq $0x1, %r15
jb 0x8f55
cmpb $0x20, 0xf(%rsp,%r12)
movq %r15, %r12
je 0x8f3f
incl %r15d
jmp 0x8f58
xorl %r15d, %r15d
leaq 0x30(%rsp), %rdi
movq %rbx, (%rdi)
movl $0x0, 0xc(%rdi)
xorl %esi, %esi
callq 0x72ea
testl %eax, %eax
jne 0x8d95
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x74aa
cmpl $0x4, %eax
je 0x8fb6
movl %eax, %ebp
testl %eax, %eax
jne 0x8d97
cmpb $0x4, (%rbx)
movq 0x70(%rsp), %rax
jne 0x8ff5
movb %r15b, 0x1(%rax)
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x2(%rax)
movq 0x1e(%rsp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x902a
testl %r15d, %r15d
je 0x900b
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x9155
testl %eax, %eax
jne 0x8d95
movq 0x70(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
cmpb $0x4, (%rbx)
jne 0x9017
movb $-0x7d, (%rax)
jmp 0x8f9e
movl $0x6, %ebp
jmp 0x8d97
testl %r15d, %r15d
je 0x9012
movl 0x17(%rsp), %ecx
movl %ecx, 0x7(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x902a
xorl %ebp, %ebp
jmp 0x8d97
movb $-0x1b, (%rax)
jmp 0x902a
movb $0x8, 0xb(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movl 0x17(%rsp), %ecx
movl %ecx, 0x7(%rax)
movb $0x1, 0x3(%rbx)
movq %rbx, %rdi
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x6ea3