// File: /workspace/github_c_makefile_star2_O0/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
subq $0xc0, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x1fdb
movl $0x9, -0x4(%rbp)
jmp 0x242f
movzbl -0x19(%rbp), %eax
andl $0x3f, %eax
movb %al, -0x19(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x78(%rbp), %rsi
movzbl -0x19(%rbp), %edx
callq 0x1770
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x2418
movq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2440
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x2038
movzbl -0x25(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x2036
movl $0x6, -0x20(%rbp)
jmp 0x2038
movzbl -0x19(%rbp), %eax
andl $0x1c, %eax
cmpl $0x0, %eax
je 0x2177
cmpl $0x0, -0x20(%rbp)
je 0x206c
cmpl $0x4, -0x20(%rbp)
jne 0x2060
leaq -0x70(%rbp), %rdi
callq 0x2590
movl %eax, -0x20(%rbp)
movzbl -0x19(%rbp), %eax
orl $0x8, %eax
movb %al, -0x19(%rbp)
jmp 0x2098
movzbl -0x66(%rbp), %eax
andl $0x11, %eax
cmpl $0x0, %eax
je 0x2081
movl $0x7, -0x20(%rbp)
jmp 0x2096
movzbl -0x19(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x2094
movl $0x8, -0x20(%rbp)
jmp 0x2096
jmp 0x2098
cmpl $0x0, -0x20(%rbp)
jne 0x2175
movzbl -0x19(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x2175
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2620
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
addq $0xe, %rax
movq %rax, -0xb0(%rbp)
callq 0x15c0
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x2680
movq -0x38(%rbp), %rax
movb $0x20, 0xb(%rax)
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2710
movq -0x38(%rbp), %rdi
addq $0x1c, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2680
movq -0x78(%rbp), %rax
movb $0x1, 0x3(%rax)
cmpq $0x0, -0x88(%rbp)
je 0x2173
movq -0x78(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x80(%rbp)
leaq -0x70(%rbp), %rdi
movq -0x88(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2770
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x2171
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x28b0
movl %eax, -0x20(%rbp)
movq -0x88(%rbp), %rcx
subq $0x1, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2173
jmp 0x2175
jmp 0x21b7
cmpl $0x0, -0x20(%rbp)
jne 0x21b5
movzbl -0x66(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x2192
movl $0x4, -0x20(%rbp)
jmp 0x21b3
movzbl -0x19(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x21b1
movzbl -0x66(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x21b1
movl $0x7, -0x20(%rbp)
jmp 0x21b3
jmp 0x21b5
jmp 0x21b7
cmpl $0x0, -0x20(%rbp)
jne 0x21ef
movzbl -0x19(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x21d3
movzbl -0x19(%rbp), %eax
orl $0x40, %eax
movb %al, -0x19(%rbp)
movq -0x78(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
cmpl $0x0, -0x20(%rbp)
jne 0x2416
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2620
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rdi
addq $0x1c, %rdi
callq 0x2940
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x78(%rbp), %rax
movw 0x6(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x8(%rax)
movb -0x19(%rbp), %cl
movq -0x10(%rbp), %rax
movb %cl, 0x20(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x21(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rdi
addq $0x50, %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x29a0
movzbl -0x19(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x2414
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jbe 0x2414
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x78(%rbp), %rax
movzwl 0xa(%rax), %eax
shlq $0x9, %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa8(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x20(%rbp)
movb %al, -0xb1(%rbp)
jne 0x2307
movq -0xa8(%rbp), %rax
cmpq -0x90(%rbp), %rax
seta %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x2313
jmp 0x236f
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x29e0
movq %rax, -0x98(%rbp)
cmpq $0x1, -0x98(%rbp)
ja 0x233b
movl $0x2, -0x20(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x98(%rbp)
jne 0x2350
movl $0x1, -0x20(%rbp)
jmp 0x2352
movq -0x90(%rbp), %rcx
movq -0xa8(%rbp), %rax
subq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x22e2
movq -0x98(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
cmpl $0x0, -0x20(%rbp)
jne 0x2412
movq -0xa8(%rbp), %rax
andq $0x1ff, %rax            # imm = 0x1FF
cmpq $0x0, %rax
je 0x2412
movq -0x78(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x2bd0
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
jne 0x23c1
movl $0x2, -0x20(%rbp)
jmp 0x2410
movq -0xa0(%rbp), %rcx
movq -0xa8(%rbp), %rax
shrq $0x9, %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x78(%rbp), %rax
movb 0x1(%rax), %al
movq -0x10(%rbp), %rsi
addq $0x50, %rsi
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rdx
movl $0x1, %ecx
movzbl %al, %edi
callq 0x1450
cmpl $0x0, %eax
je 0x240e
movl $0x1, -0x20(%rbp)
jmp 0x2410
jmp 0x2412
jmp 0x2414
jmp 0x2416
jmp 0x2418
cmpl $0x0, -0x20(%rbp)
je 0x2429
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)