// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x358d
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3583
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jb 0x358b
movq $0x1, -0x30(%rbp)
jmp 0x35ed
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x29e0
movq %rax, -0x20(%rbp)
cmpq $0x2, -0x20(%rbp)
jae 0x35b2
movq $0x1, -0x8(%rbp)
jmp 0x37ee
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x20(%rbp)
jne 0x35ca
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x37ee
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x35e5
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x37ee
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x3605
movq $0x0, -0x8(%rbp)
jmp 0x37ee
movq $0x0, -0x28(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x36a7
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jb 0x363d
movq $0x2, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x29e0
movq %rax, -0x20(%rbp)
cmpq $0x1, -0x20(%rbp)
je 0x3660
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x20(%rbp)
jne 0x366d
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x37ee
cmpq $0x0, -0x20(%rbp)
je 0x36a5
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x2, -0x20(%rbp)
jb 0x369d
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x369d
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
jmp 0x36a7
cmpq $0x0, -0x28(%rbp)
jne 0x374c
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jb 0x36f5
movq $0x2, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jbe 0x36f3
movq $0x0, -0x8(%rbp)
jmp 0x37ee
jmp 0x36f5
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x29e0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x370f
jmp 0x374a
cmpq $0x1, -0x20(%rbp)
je 0x3721
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x20(%rbp)
jne 0x372e
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x37ee
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0x3745
movq $0x0, -0x8(%rbp)
jmp 0x37ee
jmp 0x36ba
jmp 0x374c
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6830
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x3782
cmpq $0x0, -0x18(%rbp)
je 0x3782
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x6830
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x37cd
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x20(%rcx), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
ja 0x37bd
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rax
movzbl 0x4(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x4(%rax)
jmp 0x37e6
movl -0x34(%rbp), %edx
movl $0x1, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, %eax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)