// File: /workspace/github_c_makefile_star2_O1/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 0x53d0
movl %edx, %r15d
movq %rdi, %rbx
andb $0x3f, %r15b
movzbl %r15b, %ebp
leaq 0x8(%rsp), %rdi
movq %rsp, %rsi
movl %ebp, %edx
callq 0x4c82
movl %eax, %r12d
testl %eax, %eax
jne 0x5726
movq (%rsp), %r13
leaq 0x20(%rsp), %r12
movq %r13, (%r12)
leaq 0x80(%rsp), %rax
movq %rax, 0x10(%r13)
leaq 0x280(%rsp), %rax
movq %rax, 0x18(%r13)
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x5747
movb 0x53(%r12), %cl
xorl %edx, %edx
testb %cl, %cl
movl $0x6, %r12d
cmovnsl %edx, %r12d
testl %eax, %eax
cmovnel %eax, %r12d
testb $0x1c, %r15b
je 0x53db
testl %r12d, %r12d
je 0x5417
cmpl $0x4, %r12d
jne 0x53ca
leaq 0x20(%rsp), %rdi
callq 0x5bd1
movl %eax, %r12d
orb $0x8, %r15b
jmp 0x542c
movl $0x9, %r12d
jmp 0x5732
testb %cl, %cl
setns %cl
testl %eax, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x5576
movzbl 0x2a(%rsp), %eax
movl $0x4, %r12d
testb $0x10, %al
jne 0x5576
shll $0x1e, %ebp
sarl $0x1f, %ebp
andl $0x1, %eax
negl %eax
andl %ebp, %eax
andl $0x7, %eax
jmp 0x54a1
movl $0x7, %r12d
testb $0x11, 0x2a(%rsp)
jne 0x542c
addl %ebp, %ebp
andl $0x8, %ebp
movl %ebp, %r12d
testl %r12d, %r12d
jne 0x5576
xorl %r12d, %r12d
testb $0x8, %r15b
je 0x5576
cmpb $0x4, (%r13)
jne 0x54a9
movq %r13, (%rbx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x60b0
movq 0x18(%r13), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x2(%rax)
movq 0x18(%r13), %rax
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x26(%rax)
movq 0x18(%r13), %rax
movb $0x20, 0x4(%rax)
movq 0x18(%r13), %r14
callq 0x2e38
movl %eax, 0x8(%r14)
movq 0x18(%r13), %rax
movb $0x1, 0x21(%rax)
leaq 0x20(%rsp), %rdi
callq 0x60e7
testl %eax, %eax
je 0x5540
movl %eax, %r12d
jmp 0x5576
callq 0x2e38
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, (%r13)
jne 0x54fb
movzbl 0x15(%rcx), %eax
movzbl 0x14(%rcx), %edx
shll $0x18, %eax
shll $0x10, %edx
orl %eax, %edx
orl %edx, %ebp
movb $0x20, 0xb(%rcx)
movw $0x0, 0x1a(%rcx)
cmpb $0x3, (%r13)
jne 0x5512
movw $0x0, 0x14(%rcx)
movl $0x0, 0x1c(%rcx)
movb $0x1, 0x3(%r13)
testl %ebp, %ebp
je 0x5576
movq %r13, %r14
movq 0x58(%r13), %r13
leaq 0x20(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x619a
testl %eax, %eax
je 0x555f
movl %eax, %r12d
jmp 0x5573
movl 0xc(%rbx), %esi
testl %esi, %esi
je 0x5576
movq %rbx, %rdi
xorl %edx, %edx
callq 0x619a
movl %eax, %r12d
movl 0xc(%rbx), %eax
decl %eax
movl %eax, 0x20(%r13)
jmp 0x5576
movq %r14, %rdi
movq %r13, %rsi
callq 0x6314
movl %eax, %r12d
decl %ebp
movl %ebp, 0x20(%r14)
movq %r14, %r13
testl %r12d, %r12d
jne 0x559c
leal (,%r15,8), %eax
andb $0x40, %al
orb %r15b, %al
movq 0x58(%r13), %rcx
movq %rcx, 0x50(%rbx)
movq 0x60(%rsp), %rcx
movq %rcx, 0x58(%rbx)
movl %eax, %r15d
testl %r12d, %r12d
jne 0x5726
movb (%r13), %dl
cmpb $0x4, %dl
jne 0x55d4
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 %r13, %rdi
movq %rbx, %rsi
callq 0x60b0
jmp 0x55fe
movq 0x60(%rsp), %rax
movzwl 0x1a(%rax), %ecx
cmpb $0x3, %dl
jne 0x55f4
movzbl 0x15(%rax), %edx
movzbl 0x14(%rax), %esi
shll $0x18, %edx
shll $0x10, %esi
orl %edx, %esi
orl %esi, %ecx
movl %ecx, 0xc(%rbx)
movl 0x1c(%rax), %eax
movq %rax, 0x10(%rbx)
movq %r13, (%rbx)
movzwl 0x6(%r13), %eax
movw %ax, 0x8(%rbx)
movb %r15b, 0x30(%rbx)
movb $0x0, 0x31(%rbx)
xorl %r12d, %r12d
movq %r12, 0x48(%rbx)
movq %r12, 0x38(%rbx)
leaq 0x60(%rbx), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x2120
testb $0x20, %r15b
je 0x5726
movq %r14, 0x10(%rsp)
movq 0x10(%rbx), %r14
testq %r14, %r14
je 0x5726
movq %r14, 0x38(%rbx)
movq %r13, 0x18(%rsp)
movzwl 0xa(%r13), %r13d
movl %r13d, %ebp
shll $0x9, %ebp
movl 0xc(%rbx), %eax
cmpq %rbp, %r14
jbe 0x56ad
shll $0x9, %r13d
negq %r13
movq %r14, %r15
movq %rbx, %rdi
movl %eax, %esi
callq 0x6363
xorl %r12d, %r12d
cmpl $0x2, %eax
setb %r12b
addl %r12d, %r12d
cmpl $-0x1, %eax
movl $0x1, %ecx
cmovel %ecx, %r12d
subq %rbp, %r14
testl %r12d, %r12d
sete %cl
jne 0x56b2
addq %r13, %r15
cmpq %rbp, %r15
movq %r14, %r15
ja 0x5672
jmp 0x56b2
movb $0x1, %cl
xorl %r12d, %r12d
movl %eax, 0x40(%rbx)
testb %cl, %cl
movq 0x18(%rsp), %rsi
je 0x5726
movq %r14, %rcx
shlq $0x37, %rcx
je 0x56e4
addl $-0x2, %eax
movl 0x28(%rsi), %ecx
addl $-0x2, %ecx
cmpl %ecx, %eax
jae 0x56e9
movzwl 0xa(%rsi), %ecx
movl %eax, %edx
imulq %rcx, %rdx
addq 0x48(%rsi), %rdx
jmp 0x56eb
xorl %r12d, %r12d
jmp 0x5726
xorl %edx, %edx
testq %rdx, %rdx
je 0x5720
shrq $0x9, %r14
movl %r14d, %eax
addq %rax, %rdx
movq %rdx, 0x48(%rbx)
movq (%rsp), %rax
movzbl 0x1(%rax), %edi
movq 0x10(%rsp), %rsi
movl $0x1, %ecx
callq 0x2cd0
xorl %r12d, %r12d
testl %eax, %eax
setne %r12b
jmp 0x5726
movl $0x2, %r12d
testl %r12d, %r12d
je 0x5732
movq $0x0, (%rbx)
movl %r12d, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq