// File: /workspace/github_c_makefile_star2_O2/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 0xdebae
pushq $0x38
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
movq $0x200, 0x8(%rax)       # imm = 0x200
leaq 0x104565(%rip), %rsi    # 0x12c3ee
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
pushq $0x3
popq %rsi
callq 0xe3eb4
andb $0x7, %al
movb %al, 0x24(%r14)
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r15
movb 0x24(%r14), %al
testb $0x1, %al
je 0x27ec9
movb $0x1, (%r14)
testb $0x2, %al
je 0x27ed2
movb $0x1, 0x1(%r14)
testb $0x4, %al
je 0x27edd
movw $0x101, 0x1(%r14)       # imm = 0x101
cmpb $0x0, (%r14)
leaq 0xdfde7(%rip), %r13     # 0x107ccf
leaq 0xdfddc(%rip), %r12     # 0x107ccb
movq %r12, %rsi
cmoveq %r13, %rsi
movq %r15, %rdi
callq 0xeb884
cmpb $0x0, 0x1(%r14)
je 0x27f14
leaq 0xdfdc7(%rip), %rsi     # 0x107cd3
movq %r15, %rdi
callq 0xeb884
cmpb $0x0, 0x2(%r14)
je 0x27f2a
leaq 0xdfdc4(%rip), %rsi     # 0x107ce6
movq %r15, %rdi
callq 0xeb884
movzbl 0x24(%r14), %ebp
movq %r15, %rdi
callq 0xeb877
leaq 0xf911a(%rip), %rsi     # 0x121058
movq %rbx, %rdi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0xde896
movq %r15, %rdi
callq 0xeaf3c
cmpb $0x0, (%r14)
cmoveq %r13, %r12
leaq 0xdfd90(%rip), %rsi     # 0x107cf4
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe0824
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0xe404f
movq %rax, %r15
leaq 0xdfd7b(%rip), %rsi     # 0x107d02
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rdi
movl $0x200, %r12d           # imm = 0x200
movl $0x200, %esi            # imm = 0x200
callq 0xdfbb6
movq %rax, %rcx
cqto
idivq %r12
cmpq $0x1b8200, %rcx         # imm = 0x1B8200
movl $0xdc0, %ecx            # imm = 0xDC0
cmovlq %rax, %rcx
movq %rcx, 0x18(%r14)
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, 0x2(%r14)
jne 0x27fe1
cmpb $0x0, 0x1(%r14)
je 0x27ff2
leaq 0xdfd35(%rip), %rsi     # 0x107d1d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x18(%rbx), %rax
cmpq $0x1b8000, 0x18(%rax)   # imm = 0x1B8000
jl 0x28007
movl $0x6e0, %edx            # imm = 0x6E0
jmp 0x2800c
movl $0x370, %edx            # imm = 0x370
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28f12
testl %eax, %eax
jne 0x2802d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x28f12
testl %eax, %eax
je 0x28086
pushq $0x1a
popq %rsi
movq %rbx, %rdi
callq 0xeb95d
movq %rax, 0x30(%r14)
movq 0x10(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28168
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0x30(%r14), %rdi
callq 0xeb97e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
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 0x280bc
movl $0xdc000, %ecx          # imm = 0xDC000
addq 0x8(%r14), %rcx
cmpq %rcx, %rax
jl 0x280bc
movl $0x6e0, %edx            # imm = 0x6E0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28f12
testl %eax, %eax
jne 0x2802d
leaq 0xdfc91(%rip), %rsi     # 0x107d54
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x2804b