// File: /workspace/github_c_makefile_star2_O2/lb3361[P]dosfs/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 cl, bcs, clst,  tm;
	LBA_t sc;
	FSIZE_t ofs;
#endif
	DEF_NAMBUF


	if (!fp) return FR_INVALID_OBJECT;

	/* Get logical drive number */
	mode &= FF_FS_READONLY ? FA_READ : FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_CREATE_NEW | FA_OPEN_ALWAYS | FA_OPEN_APPEND;
	res = mount_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 */
					tm = GET_FATTIME();					/* Set created time */
					st_dword(dj.dir + DIR_CrtTime, tm);
					st_dword(dj.dir + DIR_ModTime, tm);
					cl = ld_clust(fs, dj.dir);			/* Get current cluster chain */
					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 */
						sc = fs->winsect;
						res = remove_chain(&dj.obj, cl, 0);
						if (res == FR_OK) {
							res = move_window(fs, sc);
							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, sizeof fp->buf);	/* 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 */
					sc = clst2sect(fs, clst);
					if (sc == 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);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rsi, 0x8(%rsp)
testq %rdi, %rdi
je 0x4d2f
movl %edx, %r15d
movq %rdi, %rbx
andb $0x3f, %r15b
movzbl %r15b, %edx
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x473a
testl %eax, %eax
je 0x4d48
andq $0x0, (%rbx)
movl %eax, %r13d
jmp 0x4d33
pushq $0x9
popq %r13
movl %r13d, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %r14
leaq 0x20(%rsp), %r12
movq %r14, (%r12)
leaq 0x80(%rsp), %rax
movq %rax, 0x10(%r14)
leaq 0x280(%rsp), %rax
movq %rax, 0x18(%r14)
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x50b1
movl %eax, %ecx
movb 0x53(%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 0x4e1d
testl %eax, %eax
je 0x4e4f
cmpl $0x4, %eax
jne 0x4db2
leaq 0x20(%rsp), %rdi
callq 0x555a
testl %eax, %eax
jne 0x4d26
orb $0x8, %r15b
testb $0x8, %r15b
jne 0x4e61
leal (,%r15,8), %ebp
andb $0x40, %bpl
orb %r15b, %bpl
movq 0x58(%r14), %rax
movq %rax, 0x50(%rbx)
movq 0x60(%rsp), %rax
movq %rax, 0x58(%rbx)
movb (%r14), %dl
cmpb $0x4, %dl
jne 0x4ee8
movl 0x2c(%rsp), %eax
movl %eax, 0x20(%rbx)
movl 0x30(%rsp), %eax
movb 0x2b(%rsp), %al
movl %eax, 0x24(%rbx)
movl 0x74(%rsp), %eax
movl %eax, 0x28(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x59a3
jmp 0x4f04
testb %dl, %dl
setns %dl
testl %ecx, %ecx
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x4d26
movzbl 0x2a(%rsp), %eax
testb $0x10, %al
jne 0x4ee1
testb $0x2, %r15b
je 0x4dc8
andl $0x1, %eax
jne 0x4e5a
jmp 0x4dc8
testb $0x11, 0x2a(%rsp)
je 0x4f94
pushq $0x7
jmp 0x4fa0
cmpb $0x4, (%r14)
jne 0x5005
movq %r14, (%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x59a3
movq 0x18(%r14), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x2(%rax)
movq 0x18(%r14), %rax
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x26(%rax)
movq 0x18(%r14), %rax
movb $0x20, 0x4(%rax)
movq 0x18(%r14), %r12
callq 0x2992
movl %eax, 0x8(%r12)
movq 0x18(%r14), %rax
movb $0x1, 0x21(%rax)
leaq 0x20(%rsp), %rdi
callq 0x59d7
testl %eax, %eax
jne 0x4d26
movl 0xc(%rbx), %esi
testl %esi, %esi
je 0x4dc8
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5a5f
movl 0xc(%rbx), %ebp
jmp 0x5097
pushq $0x4
jmp 0x4fa0
movzwl 0x1a(%rax), %ecx
cmpb $0x3, %dl
jne 0x4efa
movzwl 0x14(%rax), %edx
shll $0x10, %edx
orl %edx, %ecx
movl %ecx, 0xc(%rbx)
movl 0x1c(%rax), %eax
movq %rax, 0x10(%rbx)
movq %r14, (%rbx)
movzwl 0x6(%r14), %eax
movw %ax, 0x8(%rbx)
movb %bpl, 0x30(%rbx)
movb $0x0, 0x31(%rbx)
andq $0x0, 0x48(%rbx)
andq $0x0, 0x38(%rbx)
leaq 0x60(%rbx), %r12
xorl %r13d, %r13d
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x2110
testb $0x20, %r15b
je 0x4d33
movq %r12, 0x10(%rsp)
movq 0x10(%rbx), %r12
testq %r12, %r12
je 0x4d33
movq %r12, 0x38(%rbx)
movzwl 0xa(%r14), %r13d
shll $0x9, %r13d
movl 0xc(%rbx), %esi
xorl %eax, %eax
pushq $0x1
popq %rbp
testl %eax, %eax
jne 0x4fa6
movq %r12, %r15
subq %r13, %r15
jbe 0x4fa6
movq %rbx, %rdi
callq 0x5c48
movl %eax, %esi
xorl %eax, %eax
cmpl $0x2, %esi
setb %al
addl %eax, %eax
cmpl $-0x1, %esi
cmovel %ebp, %eax
movq %r15, %r12
jmp 0x4f69
testb $0x4, %r15b
je 0x4dbe
pushq $0x8
popq %rax
jmp 0x4d26
movl %esi, 0x40(%rbx)
testl %eax, %eax
jne 0x4d26
xorl %r13d, %r13d
movq %r12, %rax
shlq $0x37, %rax
je 0x4d33
movq %r14, %rdi
callq 0x5e21
testq %rax, %rax
je 0x50aa
shrq $0x9, %r12
movl %r12d, %ecx
addq %rcx, %rax
movq %rax, 0x48(%rbx)
movzbl 0x1(%r14), %edi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
movl %ebp, %ecx
callq 0x283b
movl %eax, %ecx
movl %ebp, %eax
testl %ecx, %ecx
jne 0x4d26
jmp 0x4d33
callq 0x2992
movq 0x60(%rsp), %rcx
movb %al, 0xe(%rcx)
movl %eax, %edx
shrl $0x8, %edx
movb %dl, 0xf(%rcx)
movl %eax, %esi
shrl $0x10, %esi
movb %sil, 0x10(%rcx)
movl %eax, %edi
shrl $0x18, %edi
movb %dil, 0x11(%rcx)
movb %al, 0x16(%rcx)
movb %dl, 0x17(%rcx)
movb %sil, 0x18(%rcx)
movb %dil, 0x19(%rcx)
movzwl 0x1a(%rcx), %ebp
cmpb $0x3, (%r14)
jne 0x504d
movzwl 0x14(%rcx), %eax
shll $0x10, %eax
orl %eax, %ebp
movb $0x20, 0xb(%rcx)
andw $0x0, 0x1a(%rcx)
cmpb $0x3, (%r14)
jne 0x5061
andw $0x0, 0x14(%rcx)
andl $0x0, 0x1c(%rcx)
movb $0x1, 0x3(%r14)
testl %ebp, %ebp
je 0x4dc8
movq 0x58(%r14), %r12
leaq 0x20(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x5a5f
testl %eax, %eax
jne 0x4d26
movq %r14, %rdi
movq %r12, %rsi
callq 0x5bf6
decl %ebp
movl %ebp, 0x20(%r14)
testl %eax, %eax
jne 0x4d26
jmp 0x4dc8
pushq $0x2
jmp 0x4fa0