// File: /workspace/github_c_makefile_star2_O0/dlbeer[P]ufat/ufat_ent.c
// Function: advance_block_in_chain
static int advance_block_in_chain(struct ufat_directory *dir, int can_alloc)
{
	const struct ufat_bpb *bpb = &dir->uf->bpb;
	const ufat_cluster_t cur_cluster =
		block_to_cluster(bpb, dir->cur_block);
	ufat_block_t next_block = dir->cur_block + 1;
	ufat_cluster_t next_cluster = block_to_cluster(bpb, next_block);
	int err;

	if (cur_cluster == next_cluster) {
		dir->cur_block = next_block;
		return 0;
	}

	/* We've crossed a cluster boundary. Look up the next cluster in
	 * the FAT.
	 */
	err = ufat_read_fat(dir->uf, cur_cluster, &next_cluster);
	if (err < 0)
		return err;

	if (UFAT_CLUSTER_IS_PTR(next_cluster)) {
		dir->cur_block = cluster_to_block(bpb, next_cluster);
		return 0;
	}

	/* This is the end of the chain. If we can't allocate, we're done. */
	if (!can_alloc) {
		dir->cur_block = UFAT_BLOCK_NONE;
		return 0;
	}

	/* Try to get a new cluster */
	err = ufat_alloc_chain(dir->uf, 1, &next_cluster);
	if (err < 0)
		return err;

	err = ufat_init_dirent_cluster(dir->uf, next_cluster);
	if (err < 0) {
		ufat_free_chain(dir->uf, next_cluster);
		return err;
	}

	err = ufat_write_fat(dir->uf, cur_cluster, next_cluster);
	if (err < 0) {
		ufat_free_chain(dir->uf, next_cluster);
		return err;
	}

	dir->cur_block = cluster_to_block(bpb, next_cluster);
	return 0;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movl %esi, 0x24(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rax
addq $0x28, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x7c10
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x7c10
movl %eax, 0x4(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x4(%rsp), %eax
jne 0x622f
movq 0x8(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, 0x34(%rsp)
jmp 0x636d
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movl 0x14(%rsp), %esi
leaq 0x4(%rsp), %rdx
callq 0x2b80
movl %eax, (%rsp)
cmpl $0x0, (%rsp)
jge 0x625a
movl (%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x636d
cmpl $0x2, 0x4(%rsp)
jb 0x6292
cmpl $0xffffff0, 0x4(%rsp)   # imm = 0xFFFFFF0
jae 0x6292
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x6640
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, 0x34(%rsp)
jmp 0x636d
cmpl $0x0, 0x24(%rsp)
jne 0x62b3
movq 0x28(%rsp), %rax
movq $-0x1, 0x10(%rax)
movl $0x0, 0x34(%rsp)
jmp 0x636d
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movl $0x1, %esi
leaq 0x4(%rsp), %rdx
callq 0x33e0
movl %eax, (%rsp)
cmpl $0x0, (%rsp)
jge 0x62df
movl (%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x636d
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movl 0x4(%rsp), %esi
callq 0x6560
movl %eax, (%rsp)
cmpl $0x0, (%rsp)
jge 0x6313
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movl 0x4(%rsp), %esi
callq 0x3340
movl (%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x636d
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movl 0x14(%rsp), %esi
movl 0x4(%rsp), %edx
callq 0x3000
movl %eax, (%rsp)
cmpl $0x0, (%rsp)
jge 0x634b
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movl 0x4(%rsp), %esi
callq 0x3340
movl (%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x636d
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x6640
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)