// File: /workspace/github_c_makefile_star2_O3/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 $0x4f8, %rsp            # imm = 0x4F8
movq %rsi, 0x10(%rsp)
testq %rdi, %rdi
je 0x515f
movl %edx, %r15d
movq %rdi, %rbx
andb $0x3f, %r15b
movzbl %r15b, %edx
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x4ada
testl %eax, %eax
je 0x517a
movl %eax, %ebp
movq $0x0, (%rbx)
movl %ebp, %r12d
jmp 0x5165
movl $0x9, %r12d
movl %r12d, %eax
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %r14
leaq 0x30(%rsp), %r12
movq %r14, (%r12)
leaq 0x90(%rsp), %rax
movq %rax, 0x10(%r14)
leaq 0x290(%rsp), %rax
movq %rax, 0x18(%r14)
movq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x554e
movb 0x53(%r12), %cl
xorl %edx, %edx
testb %cl, %cl
movl $0x6, %ebp
cmovnsl %edx, %ebp
testl %eax, %eax
cmovnel %eax, %ebp
testb $0x1c, %r15b
je 0x5255
testl %ebp, %ebp
je 0x5298
cmpl $0x4, %ebp
jne 0x51e6
leaq 0x30(%rsp), %rdi
callq 0x5a07
movl %eax, %ebp
testl %ebp, %ebp
jne 0x5153
orb $0x8, %r15b
testb $0x8, %r15b
jne 0x52bc
leal (,%r15,8), %ebp
andb $0x40, %bpl
orb %r15b, %bpl
movq 0x58(%r14), %rax
movq %rax, 0x50(%rbx)
movq 0x70(%rsp), %rax
movq %rax, 0x58(%rbx)
movb (%r14), %dl
cmpb $0x4, %dl
jne 0x5346
movl 0x3c(%rsp), %eax
movl %eax, 0x20(%rbx)
movl 0x40(%rsp), %eax
movb 0x3b(%rsp), %al
movl %eax, 0x24(%rbx)
movl 0x84(%rsp), %eax
movl %eax, 0x28(%rbx)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x5ec9
jmp 0x5362
testb %cl, %cl
setns %cl
testl %eax, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x5153
movzbl 0x3a(%rsp), %eax
movl $0x4, %ebp
testb $0x10, %al
jne 0x5153
testb $0x2, %r15b
je 0x51fc
movl $0x7, %ebp
andl $0x1, %eax
jne 0x5153
jmp 0x51fc
movl $0x7, %ebp
testb $0x11, 0x3a(%rsp)
jne 0x5153
movl $0x8, %ebp
testb $0x4, %r15b
jne 0x5153
jmp 0x51f2
cmpb $0x4, (%r14)
jne 0x5428
movq %r14, (%rbx)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0x5ec9
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 %r14, %r12
movq 0x18(%r14), %r14
callq 0x2e66
movl %eax, 0x8(%r14)
movq %r12, %r14
movq 0x18(%r12), %rax
movb $0x1, 0x21(%rax)
leaq 0x30(%rsp), %rdi
callq 0x5ee7
testl %eax, %eax
jne 0x5151
movl 0xc(%rbx), %esi
testl %esi, %esi
je 0x51fc
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5f9a
movl %eax, %ebp
movl 0xc(%rbx), %r12d
jmp 0x54c5
movzwl 0x1a(%rax), %ecx
cmpb $0x3, %dl
jne 0x5358
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)
xorl %r12d, %r12d
movq %r12, 0x48(%rbx)
movq %r12, 0x38(%rbx)
leaq 0x60(%rbx), %r13
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x2110
testb $0x20, %r15b
je 0x5165
movq 0x10(%rbx), %r15
testq %r15, %r15
je 0x5165
movq %r13, 0x18(%rsp)
movq %r15, 0x38(%rbx)
movq %r14, 0x8(%rsp)
movzwl 0xa(%r14), %ecx
movl %ecx, %r14d
shll $0x9, %r14d
movl 0xc(%rbx), %eax
cmpq %r14, %r15
jbe 0x54d9
shll $0x9, %ecx
negq %rcx
movq %rcx, 0x20(%rsp)
movq %r15, %r13
movq %rbx, %rdi
movl %eax, %esi
callq 0x616a
xorl %ebp, %ebp
cmpl $0x2, %eax
setb %bpl
addl %ebp, %ebp
cmpl $-0x1, %eax
movl $0x1, %ecx
cmovel %ecx, %ebp
subq %r14, %r13
testl %ebp, %ebp
jne 0x5413
addq 0x20(%rsp), %r15
cmpq %r14, %r15
movq %r13, %r15
ja 0x53df
movl %eax, 0x40(%rbx)
testl %ebp, %ebp
movq 0x8(%rsp), %rsi
jne 0x5153
jmp 0x54e4
callq 0x2e66
movq 0x70(%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), %r12d
cmpb $0x3, (%r14)
jne 0x5472
movzwl 0x14(%rcx), %eax
shll $0x10, %eax
orl %eax, %r12d
movb $0x20, 0xb(%rcx)
movw $0x0, 0x1a(%rcx)
cmpb $0x3, (%r14)
jne 0x5488
movw $0x0, 0x14(%rcx)
movl $0x0, 0x1c(%rcx)
movb $0x1, 0x3(%r14)
testl %r12d, %r12d
je 0x51fc
movq 0x58(%r14), %r13
leaq 0x30(%rsp), %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x5f9a
testl %eax, %eax
jne 0x5151
movq %r14, %rdi
movq %r13, %rsi
callq 0x6114
movl %eax, %ebp
decl %r12d
movl %r12d, 0x20(%r14)
testl %ebp, %ebp
jne 0x5153
jmp 0x51fc
movl %eax, 0x40(%rbx)
movq %r15, %r13
movq 0x8(%rsp), %rsi
movq %r13, %rcx
shlq $0x37, %rcx
je 0x5165
addl $-0x2, %eax
movl 0x28(%rsi), %ecx
addl $-0x2, %ecx
movl $0x2, %ebp
cmpl %ecx, %eax
jae 0x5153
movzwl 0xa(%rsi), %ecx
movl %eax, %edx
imulq %rcx, %rdx
addq 0x48(%rsi), %rdx
je 0x5153
shrq $0x9, %r13
movl %r13d, %eax
addq %rax, %rdx
movq %rdx, 0x48(%rbx)
movzbl 0x1(%rsi), %edi
movl $0x1, %ebp
movq 0x18(%rsp), %rsi
movl $0x1, %ecx
callq 0x2cfe
testl %eax, %eax
jne 0x5153
jmp 0x5165