// File: /workspace/github_c_makefile_star2_O2/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: dir_next
static
FRESULT dir_next (	/* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */
	DIR* dp,		/* Pointer to the directory object */
	int stretch		/* 0: Do not stretch table, 1: Stretch table if needed */
)
{
	DWORD ofs, clst;
	FATFS *fs = dp->obj.fs;


	ofs = dp->dptr + SZDIRE;	/* Next entry */
	if (dp->sect == 0 || ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) return FR_NO_FILE;	/* Report EOT when offset has reached max value */

	if (ofs % SS(fs) == 0) {	/* Sector changed? */
		dp->sect++;				/* Next sector */

		if (dp->clust == 0) {	/* Static table */
			if (ofs / SZDIRE >= fs->n_rootdir) {	/* Report EOT if it reached end of static table */
				dp->sect = 0; return FR_NO_FILE;
			}
		}
		else {					/* Dynamic table */
			if ((ofs / SS(fs) & (fs->csize - 1)) == 0) {	/* Cluster changed? */
				clst = get_fat(&dp->obj, dp->clust);		/* Get next cluster */
				if (clst <= 1) return FR_INT_ERR;			/* Internal error */
				if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	/* Disk error */
				if (clst >= fs->n_fatent) {					/* It reached end of dynamic table */
#if !FF_FS_READONLY
					if (!stretch) {								/* If no stretch, report EOT */
						dp->sect = 0; return FR_NO_FILE;
					}
					clst = create_chain(&dp->obj, dp->clust);	/* Allocate a cluster */
					if (clst == 0) return FR_DENIED;			/* No free cluster */
					if (clst == 1) return FR_INT_ERR;			/* Internal error */
					if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	/* Disk error */
					if (dir_clear(fs, clst) != FR_OK) return FR_DISK_ERR;	/* Clean up the stretched table */
					if (FF_FS_EXFAT) dp->obj.stat |= 4;			/* exFAT: The directory has been stretched */
#else
					if (!stretch) dp->sect = 0;					/* (this line is to suppress compiler warning) */
					dp->sect = 0; return FR_NO_FILE;			/* Report EOT */
#endif
				}
				dp->clust = clst;		/* Initialize data for new cluster */
				dp->sect = clst2sect(fs, clst);
			}
		}
	}
	dp->dptr = ofs;						/* Current entry */
	dp->dir = fs->win + ofs % SS(fs);	/* Pointer to the entry in the win[] */

	return FR_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x20(%rdi), %r13
addq $0x20, %r13
movq 0x30(%rdi), %rax
testq %rax, %rax
sete %cl
cmpq $0x200000, %r13         # imm = 0x200000
setae %dl
orb %cl, %dl
pushq $0x4
popq %rdx
je 0x2e7d
movl %edx, %r15d
jmp 0x2ea2
movq %rdi, %rbx
movq (%rdi), %r15
movq %r13, %r14
andq $0x1ff, %r14            # imm = 0x1FF
je 0x2eb4
movq %r13, 0x20(%rbx)
leaq (%r15,%r14), %rax
addq $0x58, %rax
movq %rax, 0x38(%rbx)
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %r12d
incq %rax
movq %rax, 0x30(%rbx)
movq 0x28(%rbx), %rsi
movq %r13, %rax
testq %rsi, %rsi
je 0x2ef3
shrq $0x9, %rax
movzwl 0xa(%r15), %ecx
addl $0xfff, %ecx            # imm = 0xFFF
testl %eax, %ecx
jne 0x2e8f
movq %rbx, %rdi
callq 0x1ed8
pushq $0x2
popq %rcx
cmpq $0x2, %rax
jae 0x2f0b
movl %ecx, %r15d
jmp 0x2ea2
shrq $0x5, %rax
movzwl 0x8(%r15), %ecx
cmpq %rcx, %rax
jb 0x2e8f
andq $0x0, 0x30(%rbx)
jmp 0x2e78
movq %rax, %rsi
movq %r15, -0x30(%rbp)
pushq $0x1
popq %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rsi
je 0x2ea2
movq -0x30(%rbp), %rdx
cmpq 0x20(%rdx), %rsi
jae 0x2f42
movq %rsi, 0x28(%rbx)
movq %rdx, %r15
movq %rdx, %rdi
callq 0x201a
movq %rax, 0x30(%rbx)
jmp 0x2e8f
testl %r12d, %r12d
je 0x2f77
movl %ecx, %r12d
movq 0x28(%rbx), %rsi
movq %rbx, %rdi
callq 0x2595
testq %rax, %rax
je 0x2f80
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x2ea2
cmpq $0x1, %rax
jne 0x2f89
movl %r12d, %r15d
jmp 0x2ea2
andq $0x0, 0x30(%rbx)
pushq $0x4
jmp 0x2f82
pushq $0x7
popq %r15
jmp 0x2ea2
movq -0x30(%rbp), %rdi
movq %rax, %r12
movq %rax, %rsi
callq 0x35f1
movq -0x30(%rbp), %rdx
movq %r12, %rsi
testl %eax, %eax
jne 0x2ea2
jmp 0x2f2a