// File: /workspace/github_c_makefile_star2_O2/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, -0x38(%rbp)
testq %rdi, %rdi
je 0x17c5
movl %edx, %r15d
movq %rdi, %r14
andb $0x3f, %r15b
movzbl %r15b, %edx
leaq -0x38(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1480
testl %eax, %eax
je 0x17db
andq $0x0, (%r14)
movl %eax, %r13d
jmp 0x17c9
pushq $0x9
popq %r13
movl %r13d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x48(%rbp), %r13
leaq -0x98(%rbp), %r12
movq %r13, (%r12)
movq -0x38(%rbp), %rsi
movq %r12, %rdi
callq 0x1a6d
movl %eax, %ecx
movb 0x4b(%r12), %dl
xorl %esi, %esi
testb %dl, %dl
pushq $0x6
popq %rax
cmovnsl %esi, %eax
testl %ecx, %ecx
cmovnel %ecx, %eax
testb $0x1c, %r15b
je 0x18d4
testl %eax, %eax
je 0x1901
cmpl $0x4, %eax
jne 0x182f
leaq -0x98(%rbp), %rdi
callq 0x1d24
testl %eax, %eax
jne 0x17bc
orb $0x8, %r15b
testb $0x8, %r15b
je 0x1927
movq %r14, %rbx
movq -0x60(%rbp), %r14
movzwl 0x1a(%r14), %r12d
cmpb $0x3, (%r13)
jne 0x185f
movzwl 0x14(%r14), %eax
shll $0x10, %eax
orq %rax, %r12
callq 0x13c3
movl %eax, 0xe(%r14)
movb $0x20, 0xb(%r14)
andw $0x0, 0x1a(%r14)
cmpb $0x3, (%r13)
jne 0x1880
andw $0x0, 0x14(%r14)
andl $0x0, 0x1c(%r14)
movb $0x1, 0x3(%r13)
testq %r12, %r12
je 0x1924
movq 0x50(%r13), %rax
movq %rax, -0x30(%rbp)
leaq -0x98(%rbp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x1dde
testl %eax, %eax
movq %rbx, %r14
jne 0x17bc
movq %r13, %rdi
movq -0x30(%rbp), %rsi
callq 0x1e87
decq %r12
movq %r12, 0x10(%r13)
testl %eax, %eax
jne 0x17bc
jmp 0x1927
testb %dl, %dl
setns %dl
testl %ecx, %ecx
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x17bc
movzbl -0x8e(%rbp), %eax
testb $0x10, %al
jne 0x190e
testb $0x2, %r15b
je 0x1927
andl $0x1, %eax
jne 0x190a
jmp 0x1927
testb $0x11, -0x8e(%rbp)
je 0x1912
pushq $0x7
jmp 0x191e
pushq $0x4
jmp 0x191e
testb $0x4, %r15b
je 0x1837
pushq $0x8
popq %rax
jmp 0x17bc
movq %rbx, %r14
leal (,%r15,8), %eax
andb $0x40, %al
orb %r15b, %al
movq 0x50(%r13), %rcx
movq %rcx, 0x40(%r14)
movq -0x60(%rbp), %rcx
movq %rcx, 0x48(%r14)
movzwl 0x1a(%rcx), %r12d
cmpb $0x3, (%r13)
jne 0x195a
movzwl 0x14(%rcx), %edx
shll $0x10, %edx
orq %rdx, %r12
movq %r12, 0x10(%r14)
movl 0x1c(%rcx), %ebx
movq %rbx, 0x18(%r14)
movq %r13, (%r14)
movq %r13, -0x30(%rbp)
movzwl 0x6(%r13), %ecx
movw %cx, 0x8(%r14)
movb %al, 0x20(%r14)
movb $0x0, 0x21(%r14)
andq $0x0, 0x38(%r14)
andq $0x0, 0x28(%r14)
leaq 0x50(%r14), %rdi
xorl %r13d, %r13d
movl $0x200, %edx            # imm = 0x200
movq %rdi, -0x40(%rbp)
xorl %esi, %esi
callq 0x1070
testb $0x20, %r15b
je 0x17c9
testq %rbx, %rbx
je 0x17c9
movq %r14, %r15
movq %rbx, 0x28(%r14)
movq -0x30(%rbp), %rax
movzwl 0xa(%rax), %r13d
shll $0x9, %r13d
xorl %eax, %eax
testl %eax, %eax
jne 0x1a01
movq %rbx, %r14
subq %r13, %r14
jbe 0x1a01
movq %r15, %rdi
movq %r12, %rsi
callq 0x1ed8
movq %rax, %r12
xorl %eax, %eax
cmpq $0x2, %r12
setb %al
addl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r12
pushq $0x1
popq %rcx
cmovel %ecx, %eax
movq %r14, %rbx
jmp 0x19c9
movq %r15, %r14
movq %r12, 0x30(%r15)
testl %eax, %eax
jne 0x17bc
xorl %r13d, %r13d
movq %rbx, %rax
shlq $0x37, %rax
je 0x17c9
movq -0x30(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x201a
testq %rax, %rax
je 0x1a66
shrq $0x9, %rbx
addq %rbx, %rax
movq %rax, 0x38(%r14)
movzbl 0x1(%r15), %edi
movq -0x40(%rbp), %rsi
movq %rax, %rdx
pushq $0x1
popq %rbx
movl %ebx, %ecx
callq 0x12c4
movl %eax, %ecx
movl %ebx, %eax
testl %ecx, %ecx
jne 0x17bc
jmp 0x17c9
pushq $0x2
jmp 0x191e