// File: /workspace/github_c_makefile_star2_O1/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_open
FRESULT f_open (
	FIL* fp,			/* Pointer to the blank file object */
	const TCHAR* path,	/* Pointer to the file name */
	BYTE mode			/* Access mode and file open mode flags */
)
{
	FRESULT res;
	DIR dj;
	FATFS *fs;
#if !FF_FS_READONLY
	DWORD dw, cl, bcs, clst, sc;
	FSIZE_t ofs;
#endif
	DEF_NAMBUF


	if (!fp) return FR_INVALID_OBJECT;

	/* Get logical drive */
	mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND;
	res = find_volume(&path, &fs, mode);
	if (res == FR_OK) {
		dj.obj.fs = fs;
		INIT_NAMBUF(fs);
		res = follow_path(&dj, path);	/* Follow the file path */
#if !FF_FS_READONLY	/* Read/Write configuration */
		if (res == FR_OK) {
			if (dj.fn[NSFLAG] & NS_NONAME) {	/* Origin directory itself? */
				res = FR_INVALID_NAME;
			}
#if FF_FS_LOCK != 0
			else {
				res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);		/* Check if the file can be used */
			}
#endif
		}
		/* Create or Open a file */
		if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
			if (res != FR_OK) {					/* No file, create new */
				if (res == FR_NO_FILE) {		/* There is no file to open, create a new entry */
#if FF_FS_LOCK != 0
					res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
#else
					res = dir_register(&dj);
#endif
				}
				mode |= FA_CREATE_ALWAYS;		/* File is created */
			}
			else {								/* Any object with the same name is already existing */
				if (dj.obj.attr & (AM_RDO | AM_DIR)) {	/* Cannot overwrite it (R/O or DIR) */
					res = FR_DENIED;
				} else {
					if (mode & FA_CREATE_NEW) res = FR_EXIST;	/* Cannot create as new file */
				}
			}
			if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) {	/* Truncate the file if overwrite mode */
#if FF_FS_EXFAT
				if (fs->fs_type == FS_EXFAT) {
					/* Get current allocation info */
					fp->obj.fs = fs;
					init_alloc_info(fs, &fp->obj);
					/* Set directory entry block initial state */
					mem_set(fs->dirbuf + 2, 0, 30);		/* Clear 85 entry except for NumSec */
					mem_set(fs->dirbuf + 38, 0, 26);	/* Clear C0 entry except for NumName and NameHash */
					fs->dirbuf[XDIR_Attr] = AM_ARC;
					st_dword(fs->dirbuf + XDIR_CrtTime, GET_FATTIME());
					fs->dirbuf[XDIR_GenFlags] = 1;
					res = store_xdir(&dj);
					if (res == FR_OK && fp->obj.sclust != 0) {	/* Remove the cluster chain if exist */
						res = remove_chain(&fp->obj, fp->obj.sclust, 0);
						fs->last_clst = fp->obj.sclust - 1;		/* Reuse the cluster hole */
					}
				} else
#endif
				{
					/* Set directory entry initial state */
					cl = ld_clust(fs, dj.dir);			/* Get current cluster chain */
					st_dword(dj.dir + DIR_CrtTime, GET_FATTIME());	/* Set created time */
					dj.dir[DIR_Attr] = AM_ARC;			/* Reset attribute */
					st_clust(fs, dj.dir, 0);			/* Reset file allocation info */
					st_dword(dj.dir + DIR_FileSize, 0);
					fs->wflag = 1;
					if (cl != 0) {						/* Remove the cluster chain if exist */
						dw = fs->winsect;
						res = remove_chain(&dj.obj, cl, 0);
						if (res == FR_OK) {
							res = move_window(fs, dw);
							fs->last_clst = cl - 1;		/* Reuse the cluster hole */
						}
					}
				}
			}
		}
		else {	/* Open an existing file */
			if (res == FR_OK) {					/* Is the object exsiting? */
				if (dj.obj.attr & AM_DIR) {		/* File open against a directory */
					res = FR_NO_FILE;
				} else {
					if ((mode & FA_WRITE) && (dj.obj.attr & AM_RDO)) { /* Write mode open against R/O file */
						res = FR_DENIED;
					}
				}
			}
		}
		if (res == FR_OK) {
			if (mode & FA_CREATE_ALWAYS) mode |= FA_MODIFIED;	/* Set file change flag if created or overwritten */
			fp->dir_sect = fs->winsect;			/* Pointer to the directory entry */
			fp->dir_ptr = dj.dir;
#if FF_FS_LOCK != 0
			fp->obj.lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);	/* Lock the file for this session */
			if (fp->obj.lockid == 0) res = FR_INT_ERR;
#endif
		}
#else		/* R/O configuration */
		if (res == FR_OK) {
			if (dj.fn[NSFLAG] & NS_NONAME) {	/* Is it origin directory itself? */
				res = FR_INVALID_NAME;
			} else {
				if (dj.obj.attr & AM_DIR) {		/* Is it a directory? */
					res = FR_NO_FILE;
				}
			}
		}
#endif

		if (res == FR_OK) {
#if FF_FS_EXFAT
			if (fs->fs_type == FS_EXFAT) {
				fp->obj.c_scl = dj.obj.sclust;							/* Get containing directory info */
				fp->obj.c_size = ((DWORD)dj.obj.objsize & 0xFFFFFF00) | dj.obj.stat;
				fp->obj.c_ofs = dj.blk_ofs;
				init_alloc_info(fs, &fp->obj);
			} else
#endif
			{
				fp->obj.sclust = ld_clust(fs, dj.dir);					/* Get object allocation info */
				fp->obj.objsize = ld_dword(dj.dir + DIR_FileSize);
			}
#if FF_USE_FASTSEEK
			fp->cltbl = 0;			/* Disable fast seek mode */
#endif
			fp->obj.fs = fs;	 	/* Validate the file object */
			fp->obj.id = fs->id;
			fp->flag = mode;		/* Set file access mode */
			fp->err = 0;			/* Clear error flag */
			fp->sect = 0;			/* Invalidate current data sector */
			fp->fptr = 0;			/* Set file pointer top of the file */
#if !FF_FS_READONLY
#if !FF_FS_TINY
			mem_set(fp->buf, 0, FF_MAX_SS);	/* Clear sector buffer */
#endif
			if ((mode & FA_SEEKEND) && fp->obj.objsize > 0) {	/* Seek to end of file if FA_OPEN_APPEND is specified */
				fp->fptr = fp->obj.objsize;			/* Offset to seek */
				bcs = (DWORD)fs->csize * SS(fs);	/* Cluster size in byte */
				clst = fp->obj.sclust;				/* Follow the cluster chain */
				for (ofs = fp->obj.objsize; res == FR_OK && ofs > bcs; ofs -= bcs) {
					clst = get_fat(&fp->obj, clst);
					if (clst <= 1) res = FR_INT_ERR;
					if (clst == 0xFFFFFFFF) res = FR_DISK_ERR;
				}
				fp->clust = clst;
				if (res == FR_OK && ofs % SS(fs)) {	/* Fill sector buffer if not on the sector boundary */
					if ((sc = clst2sect(fs, clst)) == 0) {
						res = FR_INT_ERR;
					} else {
						fp->sect = sc + (DWORD)(ofs / SS(fs));
#if !FF_FS_TINY
						if (disk_read(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) res = FR_DISK_ERR;
#endif
					}
				}
			}
#endif
		}

		FREE_NAMBUF();
	}

	if (res != FR_OK) fp->obj.fs = 0;	/* Invalidate file object on error */

	LEAVE_FF(fs, res);
	return FR_OK;	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, -0x40(%rbp)
testq %rdi, %rdi
je 0x187b
movl %edx, %r15d
movq %rdi, %rbx
andb $0x3f, %r15b
movzbl %r15b, %r13d
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl %r13d, %edx
callq 0x146e
movl %eax, %r12d
testl %eax, %eax
jne 0x19c8
movq -0x48(%rbp), %rax
leaq -0x98(%rbp), %r12
movq %rax, -0x30(%rbp)
movq %rax, (%r12)
movq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x1b33
movb 0x4b(%r12), %cl
xorl %edx, %edx
testb %cl, %cl
movl $0x6, %r12d
cmovnsl %edx, %r12d
testl %eax, %eax
cmovnel %eax, %r12d
testb $0x1c, %r15b
je 0x1886
testl %r12d, %r12d
je 0x18ce
cmpl $0x4, %r12d
jne 0x1871
leaq -0x98(%rbp), %rdi
callq 0x1e2c
movl %eax, %r12d
orb $0x8, %r15b
movq -0x30(%rbp), %rsi
jmp 0x18eb
movl $0x9, %r12d
jmp 0x19d4
testb %cl, %cl
setns %cl
testl %eax, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x197f
movzbl -0x8e(%rbp), %eax
movl $0x4, %r12d
testb $0x10, %al
movq -0x30(%rbp), %rsi
jne 0x199e
shll $0x1e, %r13d
sarl $0x1f, %r13d
andl $0x1, %eax
negl %eax
andl %r13d, %eax
andl $0x7, %eax
movl %eax, %r12d
jmp 0x199e
movl $0x7, %r12d
testb $0x11, -0x8e(%rbp)
movq -0x30(%rbp), %rsi
jne 0x18eb
addl %r13d, %r13d
andl $0x8, %r13d
movl %r13d, %r12d
testl %r12d, %r12d
jne 0x199e
xorl %r12d, %r12d
testb $0x8, %r15b
je 0x199e
movq -0x60(%rbp), %r14
movzwl 0x1a(%r14), %r13d
cmpb $0x3, (%rsi)
jne 0x1924
movzbl 0x14(%r14), %eax
movzbl 0x15(%r14), %ecx
shll $0x18, %ecx
shll $0x10, %eax
orl %ecx, %eax
orq %rax, %r13
callq 0x13c7
movl %eax, 0xe(%r14)
movb $0x20, 0xb(%r14)
movw $0x0, 0x1a(%r14)
movq -0x30(%rbp), %rsi
cmpb $0x3, (%rsi)
jne 0x1949
movw $0x0, 0x14(%r14)
movl $0x0, 0x1c(%r14)
movb $0x1, 0x3(%rsi)
testq %r13, %r13
je 0x199e
movq 0x50(%rsi), %rax
movq %rax, -0x38(%rbp)
movq %rsi, %r14
leaq -0x98(%rbp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x1ec8
testl %eax, %eax
je 0x1985
movl %eax, %r12d
jmp 0x199b
movq -0x30(%rbp), %rsi
jmp 0x199e
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x1f73
movl %eax, %r12d
decq %r13
movq %r13, 0x10(%r14)
movq %r14, %rsi
testl %r12d, %r12d
jne 0x19c3
leal (,%r15,8), %eax
andb $0x40, %al
orb %r15b, %al
movq 0x50(%rsi), %rcx
movq %rcx, 0x40(%rbx)
movq -0x60(%rbp), %rcx
movq %rcx, 0x48(%rbx)
movl %eax, %r15d
testl %r12d, %r12d
je 0x19e6
testl %r12d, %r12d
je 0x19d4
movq $0x0, (%rbx)
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x60(%rbp), %rax
movzwl 0x1a(%rax), %r13d
cmpb $0x3, (%rsi)
jne 0x1a07
movzbl 0x14(%rax), %ecx
movzbl 0x15(%rax), %edx
shll $0x18, %edx
shll $0x10, %ecx
orl %edx, %ecx
orq %rcx, %r13
movq %r13, 0x10(%rbx)
movl 0x1c(%rax), %r14d
movq %r14, 0x18(%rbx)
movq %rsi, (%rbx)
movzwl 0x6(%rsi), %eax
movw %ax, 0x8(%rbx)
movb %r15b, 0x20(%rbx)
movb $0x0, 0x21(%rbx)
xorl %r12d, %r12d
movq %r12, 0x38(%rbx)
movq %r12, 0x28(%rbx)
leaq 0x50(%rbx), %rdi
movl $0x200, %edx            # imm = 0x200
movq %rdi, -0x38(%rbp)
xorl %esi, %esi
callq 0x1070
testb $0x20, %r15b
je 0x19c8
testq %r14, %r14
movq -0x30(%rbp), %rax
je 0x19c8
movq %r14, 0x28(%rbx)
movzwl 0xa(%rax), %r15d
shll $0x9, %r15d
cmpq %r15, %r14
jbe 0x1aad
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1fc1
movq %rax, %r13
xorl %r12d, %r12d
cmpq $0x2, %rax
setb %r12b
addl %r12d, %r12d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r13
movl $0x1, %eax
cmovel %eax, %r12d
subq %r15, %r14
testl %r12d, %r12d
sete %al
jne 0x1ab2
cmpq %r15, %r14
ja 0x1a6e
jmp 0x1ab2
movb $0x1, %al
xorl %r12d, %r12d
movq %r13, 0x30(%rbx)
testb %al, %al
movq -0x30(%rbp), %rcx
je 0x19c8
movq %r14, %rax
shlq $0x37, %rax
je 0x1aea
addq $-0x2, %r13
movq 0x20(%rcx), %rax
addq $-0x2, %rax
cmpq %rax, %r13
jae 0x1af2
movzwl 0xa(%rcx), %eax
imulq %rax, %r13
addq 0x48(%rcx), %r13
jmp 0x1af5
xorl %r12d, %r12d
jmp 0x19c8
xorl %r13d, %r13d
testq %r13, %r13
je 0x1b28
shrq $0x9, %r14
addq %r14, %r13
movq %r13, 0x38(%rbx)
movzbl 0x1(%rcx), %edi
movq -0x38(%rbp), %rsi
movq %r13, %rdx
movl $0x1, %ecx
callq 0x12c6
xorl %r12d, %r12d
testl %eax, %eax
setne %r12b
jmp 0x19c8
movl $0x2, %r12d
jmp 0x19c8