// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movl $0x2, %edx
callq 0x4940
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0xa9b5
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xadeb
movq -0x80(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4, %eax
jne 0xaad5
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x16, %edx
callq 0x5f80
movl $0x0, -0xa4(%rbp)
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jb 0xaad0
leaq -0x10(%rbp), %rdi
callq 0xae00
movl %eax, -0xac(%rbp)
cmpl $0x10000, -0xac(%rbp)   # imm = 0x10000
jb 0xaa61
cmpl $-0x1, -0xac(%rbp)
je 0xaa1f
cmpl $0xa, -0xa4(%rbp)
jb 0xaa2b
movl $0x0, -0xac(%rbp)
jmp 0xaa5f
leaq -0xa0(%rbp), %rdi
movl -0xa4(%rbp), %eax
shll %eax
movl %eax, %eax
addq %rax, %rdi
movl -0xac(%rbp), %eax
shrl $0x10, %eax
movzwl %ax, %esi
callq 0x7c50
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0xaa61
cmpl $0x0, -0xac(%rbp)
je 0xaa8e
movl -0xac(%rbp), %esi
leaq 0x7999(%rip), %rdi      # 0x12410
addq $0x7, %rdi
callq 0xaf70
cmpl $0x0, %eax
jne 0xaa8e
cmpl $0xb, -0xa4(%rbp)
jb 0xaa9a
movl $0x6, -0x4(%rbp)
jmp 0xadeb
leaq -0xa0(%rbp), %rdi
movl -0xa4(%rbp), %eax
shll %eax
movl %eax, %eax
addq %rax, %rdi
movl -0xac(%rbp), %eax
movzwl %ax, %esi
callq 0x7c50
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0xa9e2
jmp 0xac74
leaq -0xa0(%rbp), %rdi
movl $0x20, %esi
movl $0xb, %edx
callq 0x5f80
movl $0x0, -0xa4(%rbp)
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jb 0xac09
leaq -0x10(%rbp), %rdi
callq 0xae00
movl %eax, -0xac(%rbp)
cmpl $0x10000, -0xac(%rbp)   # imm = 0x10000
jae 0xab42
movl -0xac(%rbp), %edi
callq 0xff60
movl %eax, %edi
movl $0x1b5, %esi            # imm = 0x1B5
callq 0xfe30
movzwl %ax, %eax
movl %eax, -0xb0(%rbp)
jmp 0xab4c
xorl %eax, %eax
movl %eax, -0xb0(%rbp)
jmp 0xab4c
movl -0xb0(%rbp), %eax
movw %ax, -0xa6(%rbp)
movzwl -0xa6(%rbp), %eax
cmpl $0x0, %eax
je 0xaba1
movzwl -0xa6(%rbp), %esi
leaq 0x789d(%rip), %rdi      # 0x12410
callq 0xaf70
cmpl $0x0, %eax
jne 0xaba1
movl -0xa4(%rbp), %eax
movzwl -0xa6(%rbp), %esi
movl $0xb, %ecx
movl $0xa, %edx
cmpl $0x100, %esi            # imm = 0x100
cmovgel %edx, %ecx
cmpl %ecx, %eax
jb 0xabad
movl $0x6, -0x4(%rbp)
jmp 0xadeb
movzwl -0xa6(%rbp), %eax
cmpl $0x100, %eax            # imm = 0x100
jl 0xabe1
movzwl -0xa6(%rbp), %eax
sarl $0x8, %eax
movb %al, %cl
movl -0xa4(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xa4(%rbp)
movl %eax, %eax
movb %cl, -0xa0(%rbp,%rax)
movw -0xa6(%rbp), %ax
movb %al, %cl
movl -0xa4(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xa4(%rbp)
movl %eax, %eax
movb %cl, -0xa0(%rbp,%rax)
jmp 0xaaf5
movzbl -0xa0(%rbp), %eax
cmpl $0xe5, %eax
jne 0xac23
movl $0x6, -0x4(%rbp)
jmp 0xadeb
jmp 0xac25
xorl %eax, %eax
cmpl $0x0, -0xa4(%rbp)
movb %al, -0xb1(%rbp)
je 0xac55
movl -0xa4(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movzbl -0xa0(%rbp,%rax), %eax
cmpl $0x20, %eax
sete %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0xac61
jmp 0xac72
movl -0xa4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0xac25
jmp 0xac74
movq -0x80(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x6c(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x82b0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0xade5
leaq -0x78(%rbp), %rdi
movl $0x1, %esi
callq 0x8550
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0xad2a
movq -0x80(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4, %eax
jne 0xace8
movl -0xa4(%rbp), %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x38(%rbp), %rdi
addq $0x2, %rdi
leaq -0xa0(%rbp), %rsi
movl $0x16, %edx
callq 0x6d20
jmp 0xad11
cmpl $0x0, -0xa4(%rbp)
je 0xad08
movq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0xb, %edx
callq 0x6d20
jmp 0xad0f
movq -0x38(%rbp), %rax
movb $-0x1b, (%rax)
jmp 0xad11
movq -0x80(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x80(%rbp), %rdi
callq 0x7b10
movl %eax, -0x14(%rbp)
jmp 0xade3
cmpl $0x4, -0x14(%rbp)
jne 0xade1
movl $0x0, -0x14(%rbp)
cmpl $0x0, -0xa4(%rbp)
je 0xaddf
leaq -0x78(%rbp), %rdi
movl $0x1, %esi
callq 0xafc0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0xaddd
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x5f80
movq -0x80(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4, %eax
jne 0xadac
movq -0x38(%rbp), %rax
movb $-0x7d, (%rax)
movl -0xa4(%rbp), %eax
movb %al, %cl
movq -0x38(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x38(%rbp), %rdi
addq $0x2, %rdi
leaq -0xa0(%rbp), %rsi
movl $0x16, %edx
callq 0x6d20
jmp 0xadc9
movq -0x38(%rbp), %rax
movb $0x8, 0xb(%rax)
movq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0xb, %edx
callq 0x6d20
movq -0x80(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x80(%rbp), %rdi
callq 0x7b10
movl %eax, -0x14(%rbp)
jmp 0xaddf
jmp 0xade1
jmp 0xade3
jmp 0xade5
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw (%rax,%rax)