// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/amiga-dsk.c
// Function: de_run_amiga_adf
static void de_run_amiga_adf(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 root_block_reported;
	int saved_indent_level;
	de_ucstring *flags_descr;

	de_dbg_indent_save(c, &saved_indent_level);

	d = de_malloc(c, sizeof(lctx));
	d->bsize = 512;

	de_dbg(c, "header at %d", 0);
	de_dbg_indent(c, 1);

	d->bootblock_flags = (de_getbyte(3) & 0x07);
	flags_descr = ucstring_create(c);

	if(d->bootblock_flags & 0x1) {
		d->is_ffs = 1;
	}
	if(d->bootblock_flags & 0x2) {
		d->intnl_mode = 1;
	}
	if(d->bootblock_flags & 0x4) {
		d->intnl_mode = 1;
		d->dirc_mode = 1;
	}
	ucstring_append_flags_item(flags_descr, d->is_ffs?"FFS":"OFS");
	if(d->intnl_mode) {
		ucstring_append_flags_item(flags_descr, "international mode");
	}
	if(d->dirc_mode) {
		ucstring_append_flags_item(flags_descr, "dircache mode");
	}
	de_dbg(c, "flags: 0x%02x (%s)", (UI)d->bootblock_flags, ucstring_getpsz_d(flags_descr));
	ucstring_destroy(flags_descr);

	de_declare_fmtf(c, "Amiga ADF, %s", d->is_ffs?"FFS":"OFS");

	root_block_reported = de_getu32be(8);
	de_dbg(c, "root block (reported): %"I64_FMT, root_block_reported);

	d->num_blocks = de_pad_to_n(c->infile->len, 512) / 512;
	if(d->num_blocks > MAX_ADF_BLOCKS) {
		d->num_blocks = MAX_ADF_BLOCKS;
	}
	de_dbg_indent(c, -1);

	if(d->dirc_mode || d->intnl_mode) {
		de_warn(c, "This type of ADF file might not be supported correctly");
	}

	if(!find_root_block(c, d, root_block_reported)) {
		de_err(c, "Root block not found");
		goto done;
	}

	d->curpath = de_strarray_create(c, MAX_NESTING_LEVEL+10);

	if(!do_header_block(c, d, d->root_block)) goto done;

done:
	if(d) {
		de_free(c, d->block_used_flags);
		de_strarray_destroy(d->curpath);
		de_free(c, d);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x4(%rsp), %rsi
callq 0xe8a1c
movl $0x38, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movq $0x200, 0x8(%rax)       # imm = 0x200
leaq 0x110e53(%rip), %rsi    # 0x1396ee
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl $0x3, %esi
callq 0xeead3
andb $0x7, %al
movb %al, 0x24(%r14)
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r15
movb 0x24(%r14), %al
testb $0x1, %al
je 0x288df
movb $0x1, (%r14)
testb $0x2, %al
je 0x288e8
movb $0x1, 0x1(%r14)
testb $0x4, %al
je 0x288f3
movw $0x101, 0x1(%r14)       # imm = 0x101
cmpb $0x0, (%r14)
leaq 0xec431(%rip), %r13     # 0x114d2f
leaq 0xec426(%rip), %r12     # 0x114d2b
movq %r12, %rsi
cmoveq %r13, %rsi
movq %r15, %rdi
callq 0xf7105
cmpb $0x0, 0x1(%r14)
je 0x2892a
leaq 0xec411(%rip), %rsi     # 0x114d33
movq %r15, %rdi
callq 0xf7105
cmpb $0x0, 0x2(%r14)
je 0x28940
leaq 0xec40e(%rip), %rsi     # 0x114d46
movq %r15, %rdi
callq 0xf7105
movzbl 0x24(%r14), %ebp
movq %r15, %rdi
callq 0xf70f6
leaq 0x105974(%rip), %rsi    # 0x12e2c8
movq %rbx, %rdi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
callq 0xf6753
cmpb $0x0, (%r14)
cmoveq %r13, %r12
leaq 0xec3da(%rip), %rsi     # 0x114d54
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xeb152
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xeed0d
movq %rax, %r15
leaq 0xec3c3(%rip), %rsi     # 0x114d62
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0xe9cb4
leaq 0x1ff(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x9, %rcx
cmpq $0x1b8200, %rax         # imm = 0x1B8200
movl $0xdc0, %eax            # imm = 0xDC0
cmovlq %rcx, %rax
movq %rax, 0x18(%r14)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, 0x2(%r14)
jne 0x289fe
cmpb $0x0, 0x1(%r14)
je 0x28a0f
leaq 0xec378(%rip), %rsi     # 0x114d7d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rax
cmpq $0x1b8000, 0x18(%rax)   # imm = 0x1B8000
jl 0x28a24
movl $0x6e0, %edx            # imm = 0x6E0
jmp 0x28a29
movl $0x370, %edx            # imm = 0x370
movq %rbx, %rdi
movq %r14, %rsi
callq 0x29999
testl %eax, %eax
jne 0x28a4a
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x29999
testl %eax, %eax
je 0x28aa5
movl $0x1a, %esi
movq %rbx, %rdi
callq 0xf7205
movq %rax, 0x30(%r14)
movq 0x10(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28b91
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0x30(%r14), %rdi
callq 0xf7228
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
cmpq $0x1b7fff, %rax         # imm = 0x1B7FFF
jg 0x28adb
movl $0xdc000, %ecx          # imm = 0xDC000
addq 0x8(%r14), %rcx
cmpq %rcx, %rax
jl 0x28adb
movl $0x6e0, %edx            # imm = 0x6E0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x29999
testl %eax, %eax
jne 0x28a4a
leaq 0xec2d2(%rip), %rsi     # 0x114db4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x28a6a