// File: /workspace/github_c_makefile_star2_O0/dlbeer[P]ufat/ufat.c
// Function: ufat_cache_open
int ufat_cache_open(struct ufat *uf, ufat_block_t blk_index, int skip_read)
{
	unsigned int i;
	int oldest = -1;
	int free = -1;
	int err;
	unsigned int oldest_age = 0;

	/* Scan the cache, looking for:
	 *
	 *   (a) the item, if we already have it.
	 *   (b) a free slot, if one exists.
	 *   (c) the oldest cache item.
	 */
	for (i = 0; i < uf->cache_size; i++) {
		struct ufat_cache_desc *d = &uf->cache_desc[i];
		unsigned int age = uf->next_seq - d->seq;

		if ((d->flags & UFAT_CACHE_FLAG_PRESENT) &&
		    d->index == blk_index) {
			d->seq = uf->next_seq++;
			uf->stat.cache_hit++;
			return i;
		}

		if (!(d->flags & UFAT_CACHE_FLAG_PRESENT))
			free = i;

		if (oldest < 0 || age > oldest_age) {
			oldest_age = age;
			oldest = i;
		}
	}

	/* We don't have the item. Find a place to put it. */
	if (free >= 0) {
		i = free;
	} else {
		err = cache_flush(uf, oldest);
		if (err < 0)
			return err;

		i = oldest;
	}

	if (skip_read == 0) {
		/* Read it in */
		err = uf->dev->read(uf->dev, blk_index, 1,
				    ufat_cache_data(uf, i));
		if (err < 0) {
			uf->cache_desc[i].flags = 0;
			return err;
		}

		uf->stat.read++;
		uf->stat.read_blocks++;
	} else
		memset(ufat_cache_data(uf, i), 0,
		       1 << uf->dev->log2_block_size);

	struct ufat_cache_desc *d = &uf->cache_desc[i];
	d->flags = UFAT_CACHE_FLAG_PRESENT;
	d->index = blk_index;
	d->seq = uf->next_seq++;

	uf->stat.cache_miss++;

	return i;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movl %edx, 0x4c(%rsp)
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x38(%rsp)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x74(%rcx), %eax
jae 0x276f
movq 0x58(%rsp), %rax
addq $0x80, %rax
movl 0x48(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rax
movl 0x70(%rax), %eax
movq 0x30(%rsp), %rcx
subl 0x4(%rcx), %eax
movl %eax, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x2725
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
cmpq 0x50(%rsp), %rax
jne 0x2725
movq 0x58(%rsp), %rax
movl 0x70(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x70(%rax)
movq 0x30(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x58(%rsp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
movl 0x48(%rsp), %eax
movl %eax, 0x64(%rsp)
jmp 0x28e8
movq 0x30(%rsp), %rax
movl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x273c
movl 0x48(%rsp), %eax
movl %eax, 0x40(%rsp)
cmpl $0x0, 0x44(%rsp)
jl 0x274d
movl 0x2c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jbe 0x275d
movl 0x2c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x275f
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x2692
cmpl $0x0, 0x40(%rsp)
jl 0x2780
movl 0x40(%rsp), %eax
movl %eax, 0x48(%rsp)
jmp 0x27ae
movq 0x58(%rsp), %rdi
movl 0x44(%rsp), %esi
callq 0x23c0
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jge 0x27a6
movl 0x3c(%rsp), %eax
movl %eax, 0x64(%rsp)
jmp 0x28e8
movl 0x44(%rsp), %eax
movl %eax, 0x48(%rsp)
cmpl $0x0, 0x4c(%rsp)
jne 0x285a
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rdi
movl 0x48(%rsp), %esi
callq 0x2900
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rcx
movq 0x18(%rsp), %rax
movl $0x1, %edx
callq *%rax
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jge 0x283c
movq 0x58(%rsp), %rax
addq $0x80, %rax
movl 0x48(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x0, (%rax)
movl 0x3c(%rsp), %eax
movl %eax, 0x64(%rsp)
jmp 0x28e8
movq 0x58(%rsp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movq 0x58(%rsp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0x2886
movq 0x58(%rsp), %rdi
movl 0x48(%rsp), %esi
callq 0x2900
movq %rax, %rdi
movq 0x58(%rsp), %rax
movq (%rax), %rax
movl (%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x2100
movq 0x58(%rsp), %rax
addq $0x80, %rax
movl 0x48(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movl $0x2, (%rax)
movq 0x50(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x58(%rsp), %rax
movl 0x70(%rax), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x70(%rax)
movq 0x20(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x58(%rsp), %rax
movl 0x1c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c(%rax)
movl 0x48(%rsp), %eax
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)