// File: /workspace/github_c_makefile_star2_O1/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 0x4c82
testl %eax, %eax
je 0x9078
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rbx
cmpb $0x4, (%rbx)
jne 0x9136
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x1e(%rsp)
movq 0x8(%rsp), %rax
cmpb $0x20, (%rax)
jb 0x922d
xorl %ebp, %ebp
leaq 0x8(%rsp), %r14
leaq 0x43c4(%rip), %r15      # 0xd477
movq %r14, %rdi
callq 0x930e
movl %eax, %ecx
cmpl $0x10000, %eax          # imm = 0x10000
jb 0x90ee
cmpl $-0x1, %ecx
sete %al
cmpl $0xa, %ebp
setae %dl
orb %al, %dl
je 0x90d8
xorl %ecx, %ecx
jmp 0x90ee
movl %ebp, %eax
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x10(%rsp,%rax,2)
movl %ecx, %edx
shrl $0x18, %edx
movb %dl, 0x11(%rsp,%rax,2)
incl %ebp
movl $0x6, %eax
testl %ecx, %ecx
je 0x9068
movq %r15, %rdx
movsbl (%rdx), %esi
testl %esi, %esi
je 0x910c
incq %rdx
cmpl %esi, %ecx
jne 0x90fe
testb %sil, %sil
jne 0x9068
cmpl $0xa, %ebp
ja 0x9068
movl %ebp, %eax
movw %cx, 0x10(%rsp,%rax,2)
incl %ebp
movq 0x8(%rsp), %rax
cmpb $0x1f, (%rax)
ja 0x90b3
jmp 0x922f
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x10(%rsp)
movl $0x20202020, 0x17(%rsp) # imm = 0x20202020
movq 0x8(%rsp), %rax
xorl %r15d, %r15d
cmpb $0x20, (%rax)
jb 0x9201
leaq 0x8(%rsp), %r14
leaq 0x4306(%rip), %r12      # 0xd470
movq %r14, %rdi
callq 0x930e
cmpl $0xffff, %eax           # imm = 0xFFFF
ja 0x9190
movl %eax, %edi
callq 0xbe55
movl %eax, %edi
movl $0x1b5, %esi            # imm = 0x1B5
callq 0xbdcc
movl %eax, %ecx
jmp 0x9192
xorl %ecx, %ecx
movl $0x6, %eax
testw %cx, %cx
je 0x9068
movzwl %cx, %edx
movq %r12, %rsi
movsbl (%rsi), %edi
testl %edi, %edi
je 0x91b4
incq %rsi
cmpl %edx, %edi
jne 0x91a6
testb %dil, %dil
jne 0x9068
cmpl $0x100, %edx            # imm = 0x100
movl $0x0, %esi
adcl $0xa, %esi
cmpl %esi, %r15d
jae 0x9068
cmpl $0x100, %edx            # imm = 0x100
jb 0x91e6
movl %r15d, %eax
incl %r15d
movb %ch, 0x10(%rsp,%rax)
movl %r15d, %eax
incl %r15d
movb %cl, 0x10(%rsp,%rax)
movq 0x8(%rsp), %rax
cmpb $0x1f, (%rax)
ja 0x916a
movl %r15d, %r15d
movl $0x6, %eax
cmpb $-0x1b, 0x10(%rsp)
je 0x9068
leal 0x1(%r15), %ebp
movq %r15, %rax
subq $0x1, %rax
jb 0x922d
decl %ebp
cmpb $0x20, 0xf(%rsp,%r15)
movq %rax, %r15
je 0x9218
jmp 0x922f
xorl %ebp, %ebp
leaq 0x30(%rsp), %rdi
movq %rbx, (%rdi)
movl $0x0, 0xc(%rdi)
xorl %esi, %esi
callq 0x7545
testl %eax, %eax
jne 0x9068
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x7708
cmpl $0x4, %eax
je 0x928b
testl %eax, %eax
jne 0x9068
cmpb $0x4, (%rbx)
movq 0x70(%rsp), %rax
jne 0x92bf
movb %bpl, 0x1(%rax)
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x2(%rax)
movq 0x1e(%rsp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x92f3
testl %ebp, %ebp
je 0x92d4
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x941e
testl %eax, %eax
jne 0x9068
movq 0x70(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
cmpb $0x4, (%rbx)
jne 0x92e0
movb $-0x7d, (%rax)
jmp 0x9273
testl %ebp, %ebp
je 0x92db
movl 0x17(%rsp), %ecx
movl %ecx, 0x7(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x92f3
xorl %eax, %eax
jmp 0x9068
movb $-0x1b, (%rax)
jmp 0x92f3
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 0x70fe