// File: /workspace/github_c_makefile_star2_O0/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_truncate
FRESULT f_truncate (
	FIL* fp		/* Pointer to the file object */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD ncl;


	res = validate(&fp->obj, &fs);	/* Check validity of the file object */
	if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);
	if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);	/* Check access mode */

	if (fp->fptr < fp->obj.objsize) {	/* Process when fptr is not on the eof */
		if (fp->fptr == 0) {	/* When set file size to zero, remove entire cluster chain */
			res = remove_chain(&fp->obj, fp->obj.sclust, 0);
			fp->obj.sclust = 0;
		} else {				/* When truncate a part of the file, remove remaining clusters */
			ncl = get_fat(&fp->obj, fp->clust);
			res = FR_OK;
			if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
			if (ncl == 1) res = FR_INT_ERR;
			if (res == FR_OK && ncl < fs->n_fatent) {
				res = remove_chain(&fp->obj, ncl, fp->clust);
			}
		}
		fp->obj.objsize = fp->fptr;	/* Set file size to current read/write point */
		fp->flag |= FA_MODIFIED;
#if !FF_FS_TINY
		if (res == FR_OK && (fp->flag & FA_DIRTY)) {
			if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) {
				res = FR_DISK_ERR;
			} else {
				fp->flag &= (BYTE)~FA_DIRTY;
			}
		}
#endif
		if (res != FR_OK) ABORT(fs, res);
	}

	LEAVE_FF(fs, res);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x2fe0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x49c2
movq -0x10(%rbp), %rax
movzbl 0x21(%rax), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
je 0x49cd
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4b34
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x49e9
movl $0x7, -0x4(%rbp)
jmp 0x4b34
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x4b2e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x4a30
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2770
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x4a9a
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x29e0
movq %rax, -0x28(%rbp)
movl $0x0, -0x14(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x28(%rbp)
jne 0x4a5e
movl $0x1, -0x14(%rbp)
cmpq $0x1, -0x28(%rbp)
jne 0x4a6c
movl $0x2, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x4a98
movq -0x28(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x4a98
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
callq 0x2770
movl %eax, -0x14(%rbp)
jmp 0x4a9a
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x20(%rax)
cmpl $0x0, -0x14(%rbp)
jne 0x4b12
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x4b12
movq -0x20(%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 0x14c0
cmpl $0x0, %eax
je 0x4b02
movl $0x1, -0x14(%rbp)
jmp 0x4b10
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %ecx
andl $0x7f, %ecx
movb %cl, 0x20(%rax)
jmp 0x4b12
cmpl $0x0, -0x14(%rbp)
je 0x4b2c
movl -0x14(%rbp), %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x21(%rax)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x4b34
jmp 0x4b2e
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)