// File: /workspace/github_c_makefile_star2_O1/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: create_chain
static
DWORD create_chain (	/* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
	FFOBJID* obj,		/* Corresponding object */
	DWORD clst			/* Cluster# to stretch, 0:Create a new chain */
)
{
	DWORD cs, ncl, scl;
	FRESULT res;
	FATFS *fs = obj->fs;


	if (clst == 0) {	/* Create a new chain */
		scl = fs->last_clst;				/* Suggested cluster to start to find */
		if (scl == 0 || scl >= fs->n_fatent) scl = 1;
	}
	else {				/* Stretch a chain */
		cs = get_fat(obj, clst);			/* Check the cluster status */
		if (cs < 2) return 1;				/* Test for insanity */
		if (cs == 0xFFFFFFFF) return cs;	/* Test for disk error */
		if (cs < fs->n_fatent) return cs;	/* It is already followed by next cluster */
		scl = clst;							/* Cluster to start to find */
	}
	if (fs->free_clst == 0) return 0;		/* No free cluster */

#if FF_FS_EXFAT
	if (fs->fs_type == FS_EXFAT) {	/* On the exFAT volume */
		ncl = find_bitmap(fs, scl, 1);				/* Find a free cluster */
		if (ncl == 0 || ncl == 0xFFFFFFFF) return ncl;	/* No free cluster or hard error? */
		res = change_bitmap(fs, ncl, 1, 1);			/* Mark the cluster 'in use' */
		if (res == FR_INT_ERR) return 1;
		if (res == FR_DISK_ERR) return 0xFFFFFFFF;
		if (clst == 0) {							/* Is it a new chain? */
			obj->stat = 2;							/* Set status 'contiguous' */
		} else {									/* It is a stretched chain */
			if (obj->stat == 2 && ncl != scl + 1) {	/* Is the chain got fragmented? */
				obj->n_cont = scl - obj->sclust;	/* Set size of the contiguous part */
				obj->stat = 3;						/* Change status 'just fragmented' */
			}
		}
		if (obj->stat != 2) {	/* Is the file non-contiguous? */
			if (ncl == clst + 1) {	/* Is the cluster next to previous one? */
				obj->n_frag = obj->n_frag ? obj->n_frag + 1 : 2;	/* Increment size of last framgent */
			} else {				/* New fragment */
				if (obj->n_frag == 0) obj->n_frag = 1;
				res = fill_last_frag(obj, clst, ncl);	/* Fill last fragment on the FAT and link it to new one */
				if (res == FR_OK) obj->n_frag = 1;
			}
		}
	} else
#endif
	{	/* On the FAT/FAT32 volume */
		ncl = 0;
		if (scl == clst) {						/* Stretching an existing chain? */
			ncl = scl + 1;						/* Test if next cluster is free */
			if (ncl >= fs->n_fatent) ncl = 2;
			cs = get_fat(obj, ncl);				/* Get next cluster status */
			if (cs == 1 || cs == 0xFFFFFFFF) return cs;	/* Test for error */
			if (cs != 0) {						/* Not free? */
				cs = fs->last_clst;				/* Start at suggested cluster if it is valid */
				if (cs >= 2 && cs < fs->n_fatent) scl = cs;
				ncl = 0;
			}
		}
		if (ncl == 0) {	/* The new cluster cannot be contiguous and find another fragment */
			ncl = scl;	/* Start cluster */
			for (;;) {
				ncl++;							/* Next cluster */
				if (ncl >= fs->n_fatent) {		/* Check wrap-around */
					ncl = 2;
					if (ncl > scl) return 0;	/* No free cluster found? */
				}
				cs = get_fat(obj, ncl);			/* Get the cluster status */
				if (cs == 0) break;				/* Found a free cluster? */
				if (cs == 1 || cs == 0xFFFFFFFF) return cs;	/* Test for error */
				if (ncl == scl) return 0;		/* No free cluster found? */
			}
		}
		res = put_fat(fs, ncl, 0xFFFFFFFF);		/* Mark the new cluster 'EOC' */
		if (res == FR_OK && clst != 0) {
			res = put_fat(fs, clst, ncl);		/* Link it from the previous one if needed */
		}
	}

	if (res == FR_OK) {			/* Update FSINFO if function succeeded. */
		fs->last_clst = ncl;
		if (fs->free_clst <= fs->n_fatent - 2) fs->free_clst--;
		fs->fsi_flag |= 1;
	} else {
		ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;	/* Failed. Generate error status */
	}

	return ncl;		/* Return new cluster number or error status */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r15
movq (%rdi), %rdi
testq %rsi, %rsi
movq %rdi, -0x30(%rbp)
je 0x2722
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1fc1
movq -0x30(%rbp), %rdi
movl $0x1, %r12d
cmpq $0x2, %rax
jb 0x285f
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %r12, %rax
je 0x285f
movq %rbx, %r14
movq %rax, %r12
cmpq 0x20(%rdi), %rax
jae 0x2737
jmp 0x285f
movq 0x10(%rdi), %r14
testq %r14, %r14
je 0x2731
cmpq 0x20(%rdi), %r14
jb 0x2737
movl $0x1, %r14d
cmpq $0x0, 0x18(%rdi)
je 0x283a
cmpq %rbx, %r14
jne 0x279a
leaq 0x1(%r14), %rax
cmpq 0x20(%rdi), %rax
movl $0x2, %r13d
cmovbq %rax, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x1fc1
movq %rax, %r12
cmpl $-0x1, %r12d
je 0x285f
testl %r12d, %r12d
je 0x2871
cmpl $0x1, %r12d
movq -0x30(%rbp), %rdi
je 0x285f
movq 0x10(%rdi), %rax
cmpq $0x2, %rax
jb 0x279a
cmpq 0x20(%rdi), %rax
cmovbq %rax, %r14
xorl %r13d, %r13d
movq %r13, %r12
testq %r13, %r13
je 0x27e7
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r12, %rsi
callq 0x44b7
testq %rbx, %rbx
je 0x27d0
testl %eax, %eax
jne 0x27d0
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x44b7
testl %eax, %eax
movq -0x30(%rbp), %rdx
je 0x283f
cmpl $0x1, %eax
movl $0x1, %r12d
cmoveq %r14, %r12
jmp 0x285f
movq %r14, %r13
cmpq $0x2, %r14
setae %al
incq %r13
cmpq 0x20(%rdi), %r13
setb %cl
jb 0x2803
movl $0x2, %r13d
orb %al, %cl
cmpb $0x1, %cl
jne 0x283a
movq %r15, %rdi
movq %r13, %rsi
callq 0x1fc1
movq %rax, %r12
cmpl $-0x1, %r12d
je 0x285f
testl %r12d, %r12d
movq -0x30(%rbp), %rdi
je 0x287a
cmpl $0x1, %r12d
je 0x285f
movl $0x0, %r12d
cmpq %r14, %r13
jne 0x27ea
jmp 0x285f
xorl %r12d, %r12d
jmp 0x285f
movq %r12, 0x10(%rdx)
movq 0x18(%rdx), %rax
movq 0x20(%rdx), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
ja 0x285b
decq %rax
movq %rax, 0x18(%rdx)
orb $0x1, 0x4(%rdx)
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %rdi
jmp 0x279d
movq %r13, %r12
jmp 0x27a5