// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/applesd.c
// Function: de_run_sd_internal
static void de_run_sd_internal(deark *c, lctx *d)
{
	i64 pos = 0;
	i64 nentries;
	i64 k;
	i64 entry_descriptors_pos;

	if(d->is_appledouble) {
		de_declare_fmt(c, "AppleDouble header file");
	}
	else {
		de_declare_fmt(c, "AppleSingle");
	}

	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_MACROMAN);

	d->advf = de_advfile_create(c);
	d->advf->userdata = (void*)d;
	d->advf->writefork_cbfn = my_advfile_cbfn;
	ucstring_append_sz(d->advf->filename, "bin", DE_ENCODING_LATIN1);

	pos += 4; // signature
	d->version = (u32)de_getu32be_p(&pos);
	de_dbg(c, "version: 0x%08x", (unsigned int)d->version);

	// For v1, this field is "Home file system" (TODO: Decode this.)
	// For v2, it is unused.
	pos += 16;

	nentries = de_getu16be_p(&pos);
	de_dbg(c, "number of entries: %d", (int)nentries);

	entry_descriptors_pos = pos;

	for(k=0; k<nentries; k++) {
		if(pos+12>c->infile->len) break;
		de_dbg(c, "entry[%u]", (unsigned int)k);
		de_dbg_indent(c, 1);
		do_sd_entry(c, d, (unsigned int)k, entry_descriptors_pos+12*k);
		de_dbg_indent(c, -1);
	}

	d->advf->originally_appledouble = d->is_appledouble;

	de_advfile_run(d->advf);

	de_advfile_destroy(d->advf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x4(%rax)
je 0x3c704
movq -0x8(%rbp), %rdi
leaq 0x166169(%rip), %rsi    # 0x1a2866
callq 0x15e380
jmp 0x3c714
movq -0x8(%rbp), %rdi
leaq 0x16616f(%rip), %rsi    # 0x1a287e
callq 0x15e380
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x11, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
callq 0x17aa20
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
leaq 0x16e(%rip), %rcx       # 0x3c8d0
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x68(%rax), %rdi
leaq 0x184a8e(%rip), %rsi    # 0x1c1207
movl $0x5, %edx
callq 0x1715e0
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1654b0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %edx
leaq 0x1660d1(%rip), %rsi    # 0x1a288a
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x165210
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x1660a8(%rip), %rsi    # 0x1a289a
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x3c88f
movq -0x18(%rbp), %rax
addq $0xc, %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x3c82b
jmp 0x3c88f
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x166074(%rip), %rsi    # 0x1a28b0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rcx
imulq $0xc, -0x28(%rbp), %rax
addq %rax, %rcx
callq 0x3c960
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x3c809
movq -0x10(%rbp), %rax
movb 0x4(%rax), %cl
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movb %cl, 0x7a(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x17ac20
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x17aaa0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)