// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %rsi, -0x48(%rbp)
testq %rdi, %rdi
je 0x1810
movl %edx, %r15d
movq %rdi, %rbx
andb $0x3f, %r15b
movzbl %r15b, %edx
leaq -0x48(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x144b
testl %eax, %eax
je 0x182b
movl %eax, %r14d
movq $0x0, (%rbx)
movl %r14d, %r12d
jmp 0x1816
movl $0x9, %r12d
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x58(%rbp), %rax
leaq -0xa8(%rbp), %r14
movq %rax, -0x38(%rbp)
movq %rax, (%r14)
movq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b32
movb 0x4b(%r14), %cl
xorl %edx, %edx
testb %cl, %cl
movl $0x6, %r14d
cmovnsl %edx, %r14d
testl %eax, %eax
cmovnel %eax, %r14d
testb $0x1c, %r15b
je 0x1938
testl %r14d, %r14d
je 0x1978
cmpl $0x4, %r14d
jne 0x1889
leaq -0xa8(%rbp), %rdi
callq 0x1e19
movl %eax, %r14d
testl %r14d, %r14d
jne 0x1804
orb $0x8, %r15b
testb $0x8, %r15b
je 0x19a4
movq %rbx, -0x30(%rbp)
movq -0x70(%rbp), %rbx
movzwl 0x1a(%rbx), %r12d
movq -0x38(%rbp), %r13
cmpb $0x3, (%r13)
jne 0x18c2
movzwl 0x14(%rbx), %eax
shll $0x10, %eax
orq %rax, %r12
callq 0x13c5
movl %eax, 0xe(%rbx)
movb $0x20, 0xb(%rbx)
movw $0x0, 0x1a(%rbx)
cmpb $0x3, (%r13)
jne 0x18e1
movw $0x0, 0x14(%rbx)
movl $0x0, 0x1c(%rbx)
movb $0x1, 0x3(%r13)
testq %r12, %r12
je 0x19a0
movq 0x50(%r13), %rax
movq %rax, -0x40(%rbp)
leaq -0xa8(%rbp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x1eb2
testl %eax, %eax
movq -0x30(%rbp), %rbx
jne 0x1801
movq %r13, %rdi
movq -0x40(%rbp), %rsi
callq 0x1f5d
decq %r12
movq %r12, 0x10(%r13)
testl %eax, %eax
jne 0x1801
jmp 0x19a4
testb %cl, %cl
setns %cl
testl %eax, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x1804
movzbl -0x9e(%rbp), %eax
movl $0x4, %r14d
testb $0x10, %al
jne 0x1804
testb $0x2, %r15b
je 0x19a4
movl $0x7, %r14d
andl $0x1, %eax
jne 0x1804
jmp 0x19a4
movl $0x7, %r14d
testb $0x11, -0x9e(%rbp)
jne 0x1804
movl $0x8, %r14d
testb $0x4, %r15b
jne 0x1804
jmp 0x1896
movq -0x30(%rbp), %rbx
leal (,%r15,8), %eax
andb $0x40, %al
orb %r15b, %al
movq -0x38(%rbp), %r14
movq 0x50(%r14), %rcx
movq %rcx, 0x40(%rbx)
movq -0x70(%rbp), %rcx
movq %rcx, 0x48(%rbx)
movzwl 0x1a(%rcx), %r13d
cmpb $0x3, (%r14)
jne 0x19da
movzwl 0x14(%rcx), %edx
shll $0x10, %edx
orq %rdx, %r13
movq %r13, 0x10(%rbx)
movq %rbx, %rdx
movl 0x1c(%rcx), %ebx
movq %rbx, 0x18(%rdx)
movq %r14, (%rdx)
movzwl 0x6(%r14), %ecx
movw %cx, 0x8(%rdx)
movb %al, 0x20(%rdx)
movb $0x0, 0x21(%rdx)
xorl %r12d, %r12d
movq %r12, 0x38(%rdx)
movq %r12, 0x28(%rdx)
movq %rdx, -0x30(%rbp)
leaq 0x50(%rdx), %rdi
movl $0x200, %edx            # imm = 0x200
movq %rdi, -0x50(%rbp)
xorl %esi, %esi
callq 0x1070
testb $0x20, %r15b
je 0x1816
testq %rbx, %rbx
je 0x1816
movq -0x30(%rbp), %rax
movq %rbx, 0x28(%rax)
movzwl 0xa(%r14), %ecx
movl %ecx, %edx
shll $0x9, %edx
cmpq %rdx, %rbx
jbe 0x1ab3
shll $0x9, %ecx
negq %rcx
movq %rcx, -0x40(%rbp)
movq %rbx, %r15
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq %r13, %rsi
movq %rdx, %r14
callq 0x1fb2
movq %r14, %rdx
movq %rax, %r13
xorl %r14d, %r14d
cmpq $0x2, %rax
setb %r14b
addl %r14d, %r14d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r13
movl $0x1, %eax
cmovel %eax, %r14d
subq %rdx, %r15
testl %r14d, %r14d
jne 0x1aa0
addq -0x40(%rbp), %rbx
cmpq %rdx, %rbx
movq %r15, %rbx
ja 0x1a55
movq -0x30(%rbp), %rbx
movq %r13, 0x30(%rbx)
testl %r14d, %r14d
jne 0x1804
jmp 0x1abd
movq %r13, 0x30(%rax)
movq %rbx, %r15
movq %rax, %rbx
movq %r15, %rax
shlq $0x37, %rax
je 0x1816
addq $-0x2, %r13
movq -0x38(%rbp), %rcx
movq 0x20(%rcx), %rax
addq $-0x2, %rax
movl $0x2, %r14d
cmpq %rax, %r13
jae 0x1804
movzwl 0xa(%rcx), %eax
imulq %rax, %r13
addq 0x48(%rcx), %r13
je 0x1804
shrq $0x9, %r15
addq %r15, %r13
movq %r13, 0x38(%rbx)
movq -0x38(%rbp), %rax
movzbl 0x1(%rax), %edi
movl $0x1, %r14d
movq -0x50(%rbp), %rsi
movq %r13, %rdx
movl $0x1, %ecx
callq 0x12c4
testl %eax, %eax
jne 0x1804
jmp 0x1816