// File: /workspace/github_c_makefile_star2_O0/dlbeer[P]ufat/ufat_file.c
// Function: ufat_file_truncate
int ufat_file_truncate(struct ufat_file *f)
{
	const unsigned int
		cluster_size = 1 << (f->uf->bpb.log2_blocks_per_cluster +
				     f->uf->dev->log2_block_size);
	int err;

	err = set_size(f, f->cur_pos);
	if (err < 0)
		return err;

	if (!f->file_size) {
		ufat_cluster_t old_start = f->start;

		err = set_start(f, 0);
		if (err < 0)
			return err;

		if (UFAT_CLUSTER_IS_PTR(old_start)) {
			err = ufat_free_chain(f->uf, old_start);
			if (err < 0)
				return err;
		}

		f->cur_cluster = 0;
		return 0;
	}

	if (f->file_size & (cluster_size - 1)) {
		ufat_cluster_t tail;

		err = ufat_read_fat(f->uf, f->cur_cluster, &tail);
		if (err < 0)
			return err;

		err = ufat_write_fat(f->uf, f->cur_cluster, UFAT_CLUSTER_EOC);
		if (err < 0)
			return err;

		if (UFAT_CLUSTER_IS_PTR(tail))
			return ufat_free_chain(f->uf, tail);

		return 0;
	}

	if (UFAT_CLUSTER_IS_PTR(f->cur_cluster)) {
		err = ufat_write_fat(f->uf, f->prev_cluster, UFAT_CLUSTER_EOC);
		if (err < 0)
			return err;

		ufat_free_chain(f->uf, f->cur_cluster);
		f->cur_cluster = 0;
		return 0;
	}

	return 0;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl 0x2c(%rax), %ecx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
addl (%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rdi
movq 0x18(%rsp), %rax
movl 0x24(%rax), %esi
callq 0x5a30
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x5b26
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x5ce8
movq 0x18(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x5bba
movq 0x18(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x5d00
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x5b65
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x5ce8
cmpl $0x2, 0xc(%rsp)
jb 0x5ba1
cmpl $0xffffff0, 0xc(%rsp)   # imm = 0xFFFFFF0
jae 0x5ba1
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl 0xc(%rsp), %esi
callq 0x3340
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x5b9f
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x5ce8
jmp 0x5ba1
movq 0x18(%rsp), %rax
movl $0x0, 0x20(%rax)
movl $0x0, 0x24(%rsp)
jmp 0x5ce8
movq 0x18(%rsp), %rax
movl 0x18(%rax), %eax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x5c6d
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movl 0x20(%rax), %esi
leaq 0x8(%rsp), %rdx
callq 0x2b80
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x5c06
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x5ce8
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movl 0x20(%rax), %esi
movl $0xffffff8, %edx        # imm = 0xFFFFFF8
callq 0x3000
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x5c38
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x5ce8
cmpl $0x2, 0x8(%rsp)
jb 0x5c63
cmpl $0xffffff0, 0x8(%rsp)   # imm = 0xFFFFFF0
jae 0x5c63
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl 0x8(%rsp), %esi
callq 0x3340
movl %eax, 0x24(%rsp)
jmp 0x5ce8
movl $0x0, 0x24(%rsp)
jmp 0x5ce8
movq 0x18(%rsp), %rax
cmpl $0x2, 0x20(%rax)
jb 0x5ce0
movq 0x18(%rsp), %rax
cmpl $0xffffff0, 0x20(%rax)  # imm = 0xFFFFFF0
jae 0x5ce0
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movl 0x1c(%rax), %esi
movl $0xffffff8, %edx        # imm = 0xFFFFFF8
callq 0x3000
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jge 0x5cb5
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x5ce8
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x3340
movq 0x18(%rsp), %rax
movl $0x0, 0x20(%rax)
movl $0x0, 0x24(%rsp)
jmp 0x5ce8
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)