// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x520, %rsp            # imm = 0x520
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x530b
movl $0x9, -0x4(%rbp)
jmp 0x58fd
movzbl -0x19(%rbp), %eax
andl $0x3f, %eax
movb %al, -0x19(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x88(%rbp), %rsi
movzbl -0x19(%rbp), %edx
callq 0x4940
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x58e6
movq -0x88(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x2b0(%rbp), %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq -0x510(%rbp), %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x5910
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x5392
movzbl -0x2d(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x5390
movl $0x6, -0x20(%rbp)
jmp 0x5392
movzbl -0x19(%rbp), %eax
andl $0x1c, %eax
cmpl $0x0, %eax
je 0x55e5
cmpl $0x0, -0x20(%rbp)
je 0x53c6
cmpl $0x4, -0x20(%rbp)
jne 0x53ba
leaq -0x80(%rbp), %rdi
callq 0x5ac0
movl %eax, -0x20(%rbp)
movzbl -0x19(%rbp), %eax
orl $0x8, %eax
movb %al, -0x19(%rbp)
jmp 0x53f2
movzbl -0x76(%rbp), %eax
andl $0x11, %eax
cmpl $0x0, %eax
je 0x53db
movl $0x7, -0x20(%rbp)
jmp 0x53f0
movzbl -0x19(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x53ee
movl $0x8, -0x20(%rbp)
jmp 0x53f0
jmp 0x53f2
cmpl $0x0, -0x20(%rbp)
jne 0x55e3
movzbl -0x19(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x55e3
movq -0x88(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4, %eax
jne 0x5504
movq -0x88(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5f10
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x2, %rdi
xorl %esi, %esi
movl $0x1e, %edx
callq 0x5f80
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x26, %rdi
xorl %esi, %esi
movl $0x1a, %edx
callq 0x5f80
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movb $0x20, 0x4(%rax)
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
addq $0x8, %rax
movq %rax, -0x518(%rbp)
callq 0x1be0
movq -0x518(%rbp), %rdi
movl %eax, %esi
callq 0x5fc0
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movb $0x1, 0x21(%rax)
leaq -0x80(%rbp), %rdi
callq 0x6040
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x54ff
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x54ff
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
xorl %edx, %edx
callq 0x6140
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %ecx
subl $0x1, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x55e1
callq 0x1be0
movl %eax, -0x98(%rbp)
movq -0x40(%rbp), %rdi
addq $0xe, %rdi
movl -0x98(%rbp), %esi
callq 0x5fc0
movq -0x40(%rbp), %rdi
addq $0x16, %rdi
movl -0x98(%rbp), %esi
callq 0x5fc0
movq -0x88(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x63d0
movl %eax, -0x8c(%rbp)
movq -0x40(%rbp), %rax
movb $0x20, 0xb(%rax)
movq -0x88(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x6430
movq -0x40(%rbp), %rdi
addq $0x1c, %rdi
xorl %esi, %esi
callq 0x5fc0
movq -0x88(%rbp), %rax
movb $0x1, 0x3(%rax)
cmpl $0x0, -0x8c(%rbp)
je 0x55df
movq -0x88(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0xa0(%rbp)
leaq -0x80(%rbp), %rdi
movl -0x8c(%rbp), %esi
xorl %edx, %edx
callq 0x6140
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x55dd
movq -0x88(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x6480
movl %eax, -0x20(%rbp)
movl -0x8c(%rbp), %ecx
subl $0x1, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x55df
jmp 0x55e1
jmp 0x55e3
jmp 0x5625
cmpl $0x0, -0x20(%rbp)
jne 0x5623
movzbl -0x76(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x5600
movl $0x4, -0x20(%rbp)
jmp 0x5621
movzbl -0x19(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x561f
movzbl -0x76(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x561f
movl $0x7, -0x20(%rbp)
jmp 0x5621
jmp 0x5623
jmp 0x5625
cmpl $0x0, -0x20(%rbp)
jne 0x5660
movzbl -0x19(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x5641
movzbl -0x19(%rbp), %eax
orl $0x40, %eax
movb %al, -0x19(%rbp)
movq -0x88(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
cmpl $0x0, -0x20(%rbp)
jne 0x58e4
movq -0x88(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x4, %eax
jne 0x56b8
movl -0x74(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x70(%rbp), %rax
movl %eax, %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
movzbl -0x75(%rbp), %eax
orl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5f10
jmp 0x56ea
movq -0x88(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x63d0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x40(%rbp), %rdi
addq $0x1c, %rdi
callq 0x6510
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x88(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
movw 0x6(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x8(%rax)
movb -0x19(%rbp), %cl
movq -0x10(%rbp), %rax
movb %cl, 0x30(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x31(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x5f80
movzbl -0x19(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x58e2
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jbe 0x58e2
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x88(%rbp), %rax
movzwl 0xa(%rax), %eax
shll $0x9, %eax
movl %eax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x94(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xa8(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x20(%rbp)
movb %al, -0x519(%rbp)
jne 0x57cf
movq -0xa8(%rbp), %rax
movl -0x90(%rbp), %ecx
cmpq %rcx, %rax
seta %al
movb %al, -0x519(%rbp)
movb -0x519(%rbp), %al
testb $0x1, %al
jne 0x57db
jmp 0x5830
movq -0x10(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0x6560
movl %eax, -0x94(%rbp)
cmpl $0x1, -0x94(%rbp)
ja 0x5800
movl $0x2, -0x20(%rbp)
cmpl $-0x1, -0x94(%rbp)
jne 0x5810
movl $0x1, -0x20(%rbp)
jmp 0x5812
movl -0x90(%rbp), %eax
movl %eax, %ecx
movq -0xa8(%rbp), %rax
subq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x57a8
movl -0x94(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
cmpl $0x0, -0x20(%rbp)
jne 0x58e0
movq -0xa8(%rbp), %rax
andq $0x1ff, %rax            # imm = 0x1FF
cmpq $0x0, %rax
je 0x58e0
movq -0x88(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0x6870
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jne 0x588a
movl $0x2, -0x20(%rbp)
jmp 0x58de
movq -0xa0(%rbp), %rcx
movq -0xa8(%rbp), %rax
shrq $0x9, %rax
movl %eax, %eax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x88(%rbp), %rax
movb 0x1(%rax), %al
movq -0x10(%rbp), %rsi
addq $0x60, %rsi
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rdx
movl $0x1, %ecx
movzbl %al, %edi
callq 0x1970
cmpl $0x0, %eax
je 0x58dc
movl $0x1, -0x20(%rbp)
jmp 0x58de
jmp 0x58e0
jmp 0x58e2
jmp 0x58e4
jmp 0x58e6
cmpl $0x0, -0x20(%rbp)
je 0x58f7
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x520, %rsp            # imm = 0x520
popq %rbp
retq
nopl (%rax)