// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rdi, %r15
movq (%rdi), %rbx
testq %rsi, %rsi
je 0x2719
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1fb2
movq %rax, %rcx
movl $0x1, %eax
cmpq $0x2, %rcx
jb 0x27e1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
je 0x27e1
movq 0x20(%rbx), %rdx
movq %r13, %r14
movq %rcx, %rax
cmpq %rdx, %rcx
jae 0x272b
jmp 0x27e1
movq 0x10(%rbx), %r14
testq %r14, %r14
je 0x2789
movq 0x20(%rbx), %rdx
cmpq %rdx, %r14
jae 0x2789
cmpq $0x0, 0x18(%rbx)
je 0x27df
leaq 0x18(%rbx), %rax
movq %rax, -0x30(%rbp)
cmpq %r13, %r14
jne 0x279e
leaq 0x1(%r13), %rax
cmpq %rdx, %rax
movl $0x2, %r12d
cmovbq %rax, %r12
movq (%r15), %rdi
movq %r12, %rsi
callq 0x1fb2
cmpl $-0x1, %eax
je 0x27e1
testl %eax, %eax
je 0x27f0
cmpl $0x1, %eax
je 0x27e1
movq 0x10(%rbx), %r14
cmpq $0x2, %r14
jb 0x285f
cmpq 0x20(%rbx), %r14
cmovaeq %r13, %r14
jmp 0x279e
cmpq $0x0, 0x18(%rbx)
je 0x27df
leaq 0x18(%rbx), %rax
movq %rax, -0x30(%rbp)
movl $0x1, %r14d
movq %r14, %r12
cmpq $0x2, %r14
setae %al
incq %r12
cmpq 0x20(%rbx), %r12
setb %cl
jb 0x27ba
movl $0x2, %r12d
orb %al, %cl
cmpb $0x1, %cl
jne 0x27df
movq (%r15), %rdi
movq %r12, %rsi
callq 0x1fb2
cmpl $-0x1, %eax
je 0x27e1
testl %eax, %eax
je 0x27f8
cmpl $0x1, %eax
je 0x27e1
cmpq %r14, %r12
jne 0x27a1
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %r14
testq %r12, %r12
je 0x279e
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4483
testq %r13, %r13
je 0x2825
testl %eax, %eax
jne 0x2825
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x4483
testl %eax, %eax
je 0x2837
cmpl $0x1, %eax
movl $0x1, %eax
cmoveq %r14, %rax
jmp 0x27e1
movq %r12, 0x10(%rbx)
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
ja 0x2856
decq %rax
movq -0x30(%rbp), %rcx
movq %rax, (%rcx)
orb $0x1, 0x4(%rbx)
movq %r12, %rax
jmp 0x27e1
movq %r13, %r14
jmp 0x279e