// File: /workspace/github_c_makefile_star2_O1/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), %r12
addq $0x20, %r12
movq 0x30(%rdi), %rax
testq %rax, %rax
sete %cl
cmpq $0x200000, %r12         # imm = 0x200000
setae %dl
orb %cl, %dl
movl $0x4, %r15d
jne 0x3047
movq %rdi, %rbx
movq (%rdi), %rdx
movq %r12, %r14
andq $0x1ff, %r14            # imm = 0x1FF
je 0x3059
movq %r12, 0x20(%rbx)
leaq (%rdx,%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, %r13d
incq %rax
movq %rax, 0x30(%rbx)
movq 0x28(%rbx), %rsi
movq %r12, %rax
testq %rsi, %rsi
je 0x30d9
shrq $0x9, %rax
movzwl 0xa(%rdx), %ecx
addl $0xfff, %ecx            # imm = 0xFFF
testl %eax, %ecx
jne 0x3034
movq %rdx, -0x30(%rbp)
movq %rbx, %rdi
callq 0x1fc1
movl $0x2, %r15d
cmpq $0x2, %rax
jb 0x3047
movq %rax, %rcx
movl $0x1, %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
je 0x3047
movq -0x30(%rbp), %rdx
cmpq 0x20(%rdx), %rcx
jae 0x30f7
movq %rcx, 0x28(%rbx)
addq $-0x2, %rcx
movq 0x20(%rdx), %rax
addq $-0x2, %rax
cmpq %rax, %rcx
jae 0x312c
movzwl 0xa(%rdx), %eax
imulq %rax, %rcx
addq 0x48(%rdx), %rcx
jmp 0x312e
shrq $0x5, %rax
movzwl 0x8(%rdx), %ecx
cmpq %rcx, %rax
jb 0x3034
movq $0x0, 0x30(%rbx)
jmp 0x3047
testl %r13d, %r13d
je 0x3137
movq 0x28(%rbx), %rsi
movq %rbx, %rdi
callq 0x26c3
testq %rax, %rax
je 0x314a
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x3047
cmpq $0x1, %rax
jne 0x3155
movl $0x2, %r15d
jmp 0x3047
xorl %ecx, %ecx
movq %rcx, 0x30(%rbx)
jmp 0x3034
movq $0x0, 0x30(%rbx)
movl $0x4, %r15d
jmp 0x3047
movl $0x7, %r15d
jmp 0x3047
movq -0x30(%rbp), %rdi
movq %rax, %r13
movq %rax, %rsi
callq 0x380f
movq %r13, %rcx
movq -0x30(%rbp), %rdx
testl %eax, %eax
jne 0x3047
jmp 0x30b6