// File: /workspace/github_c_makefile_star2_O0/lb3361[P]dosfs/ff.c
// Function: find_volume
static UINT find_volume (	/* Returns BS status found in the hosting drive */
	FATFS* fs,		/* Filesystem object */
	UINT part		/* Partition to fined = 0:auto, 1..:forced */
)
{
	UINT fmt, i;
	DWORD mbr_pt[4];


	fmt = check_fs(fs, 0);				/* Load sector 0 and check if it is an FAT VBR as SFD */
	if (fmt != 2 && (fmt >= 3 || part == 0)) return fmt;	/* Returns if it is a FAT VBR as auto scan, not a BS or disk error */

	/* Sector 0 is not an FAT VBR or forced partition number wants a partition */

#if FF_LBA64
	if (fs->win[MBR_Table + PTE_System] == 0xEE) {	/* GPT protective MBR? */
		DWORD n_ent, v_ent, ofs;
		QWORD pt_lba;

		if (move_window(fs, 1) != FR_OK) return 4;	/* Load GPT header sector (next to MBR) */
		if (!test_gpt_header(fs->win)) return 3;	/* Check if GPT header is valid */
		n_ent = ld_dword(fs->win + GPTH_PtNum);		/* Number of entries */
		pt_lba = ld_qword(fs->win + GPTH_PtOfs);	/* Table location */
		for (v_ent = i = 0; i < n_ent; i++) {		/* Find FAT partition */
			if (move_window(fs, pt_lba + i * SZ_GPTE / SS(fs)) != FR_OK) return 4;	/* PT sector */
			ofs = i * SZ_GPTE % SS(fs);												/* Offset in the sector */
			if (!mem_cmp(fs->win + ofs + GPTE_PtGuid, GUID_MS_Basic, 16)) {	/* MS basic data partition? */
				v_ent++;
				fmt = check_fs(fs, ld_qword(fs->win + ofs + GPTE_FstLba));	/* Load VBR and check status */
				if (part == 0 && fmt <= 1) return fmt;			/* Auto search (valid FAT volume found first) */
				if (part != 0 && v_ent == part) return fmt;		/* Forced partition order (regardless of it is valid or not) */
			}
		}
		return 3;	/* Not found */
	}
#endif
	if (FF_MULTI_PARTITION && part > 4) return 3;	/* MBR has 4 partitions max */
	for (i = 0; i < 4; i++) {		/* Load partition offset in the MBR */
		mbr_pt[i] = ld_dword(fs->win + MBR_Table + i * SZ_PTE + PTE_StLba);
	}
	i = part ? part - 1 : 0;		/* Table index to find first */
	do {							/* Find an FAT volume */
		fmt = mbr_pt[i] ? check_fs(fs, mbr_pt[i]) : 3;	/* Check if the partition is FAT */
	} while (part == 0 && fmt >= 2 && ++i < 4);
	return fmt;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xe0e0
movl %eax, -0x18(%rbp)
cmpl $0x2, -0x18(%rbp)
je 0xde7c
cmpl $0x3, -0x18(%rbp)
jae 0xde71
cmpl $0x0, -0x14(%rbp)
jne 0xde7c
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xe0cf
movq -0x10(%rbp), %rax
movzbl 0x222(%rax), %eax
cmpl $0xee, %eax
jne 0xdff5
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x6480
cmpl $0x0, %eax
je 0xdeb1
movl $0x4, -0x4(%rbp)
jmp 0xe0cf
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
callq 0xd280
cmpl $0x0, %eax
jne 0xdecf
movl $0x3, -0x4(%rbp)
jmp 0xe0cf
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
addq $0x50, %rdi
callq 0x6510
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
addq $0x48, %rdi
callq 0xd380
movq %rax, -0x48(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0xdfe9
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x1c(%rbp), %eax
shll $0x7, %eax
shrl $0x9, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x6480
cmpl $0x0, %eax
je 0xdf3e
movl $0x4, -0x4(%rbp)
jmp 0xe0cf
movl -0x1c(%rbp), %eax
shll $0x7, %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
leaq 0x44ef(%rip), %rsi      # 0x12450
movl $0x10, %edx
callq 0xd440
cmpl $0x0, %eax
jne 0xdfd9
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
movl -0x3c(%rbp), %eax
addq %rax, %rdi
addq $0x20, %rdi
callq 0xd380
movq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0xe0e0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0xdfbe
cmpl $0x1, -0x18(%rbp)
ja 0xdfbe
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xe0cf
cmpl $0x0, -0x14(%rbp)
je 0xdfd7
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0xdfd7
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xe0cf
jmp 0xdfd9
jmp 0xdfdb
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xdf06
movl $0x3, -0x4(%rbp)
jmp 0xe0cf
cmpl $0x4, -0x14(%rbp)
jbe 0xe007
movl $0x3, -0x4(%rbp)
jmp 0xe0cf
movl $0x0, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
jae 0xe04b
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
addq $0x1be, %rdi            # imm = 0x1BE
movl -0x1c(%rbp), %eax
shll $0x4, %eax
movl %eax, %eax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x6510
movl %eax, %ecx
movl -0x1c(%rbp), %eax
movl %ecx, -0x30(%rbp,%rax,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xe00e
cmpl $0x0, -0x14(%rbp)
je 0xe05c
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xe063
xorl %eax, %eax
movl %eax, -0x54(%rbp)
jmp 0xe063
movl -0x54(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl $0x0, -0x30(%rbp,%rax,4)
je 0xe08a
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl -0x30(%rbp,%rax,4), %eax
movl %eax, %esi
callq 0xe0e0
movl %eax, -0x58(%rbp)
jmp 0xe094
movl $0x3, %eax
movl %eax, -0x58(%rbp)
jmp 0xe094
movl -0x58(%rbp), %eax
movl %eax, -0x18(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x14(%rbp)
movb %al, -0x59(%rbp)
jne 0xe0c2
xorl %eax, %eax
cmpl $0x2, -0x18(%rbp)
movb %al, -0x59(%rbp)
jb 0xe0c2
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x4, %eax
setb %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0xe069
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)