// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/abk.c
// Function: do_read_bank
static int do_read_bank(deark *c, lctx *d, i64 pos, i64 *bytesused)
{
	struct amosbank *bk = NULL;
	int ret;
	int retval = 0;

	bk = de_malloc(c, sizeof(struct amosbank));
	bk->f_offs = pos;
	bk->f = dbuf_open_input_subfile(c->infile, bk->f_offs, c->infile->len - bk->f_offs);

	dbuf_read_fourcc(bk->f, 0, &bk->banktype4cc, 4, 0x0);
	de_dbg(c, "bank type '%s'", bk->banktype4cc.id_dbgstr);

	switch(bk->banktype4cc.id) {
	case CODE_AmIc: bk->file_ext = "icon.abk"; break;
	case CODE_AmSp: bk->file_ext = "sprite.abk"; break;
	case CODE_AmBk: bk->file_ext = "AmBk.abk"; break;
	default: bk->file_ext = "abk";
	}

	if(bk->banktype4cc.id==CODE_AmIc || bk->banktype4cc.id==CODE_AmSp) {
		ret = do_read_sprite(c, d, bk);
		retval = ret;
		*bytesused = bk->bank_len;
	}
	else if(bk->banktype4cc.id==CODE_AmBk) {
		ret = do_read_AmBk(c, d, bk);
		retval = ret;
		*bytesused = bk->bank_len;
	}
	else {
		de_err(c, "Unsupported bank type: '%s'", bk->banktype4cc.id_sanitized_sz);
	}

	destroy_amosbank(c, bk);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movl $0x4b8, %esi            # imm = 0x4B8
callq 0xdecaf
movq %rax, %r14
movq %r12, 0x48(%rax)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r12, %rdx
movq %r12, %rsi
callq 0xe6248
movq %rax, 0x50(%r14)
xorl %r12d, %r12d
pushq $0x4
popq %rcx
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0xe657c
leaq 0x10(%r14), %rdx
leaq 0xea69a(%rip), %rsi     # 0x10409e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x4(%r14), %eax
cmpl $0x416d426b, %eax       # imm = 0x416D426B
je 0x19abe
cmpl $0x416d4963, %eax       # imm = 0x416D4963
je 0x19a38
cmpl $0x416d5370, %eax       # imm = 0x416D5370
jne 0x19adb
leaq 0xea680(%rip), %rax     # 0x1040b6
jmp 0x19a3f
leaq 0xea66e(%rip), %rax     # 0x1040ad
movq %rax, 0x58(%r14)
movq 0x50(%r14), %rdi
pushq $0x4
popq %rsi
callq 0xe3f46
movq %rax, 0x60(%r14)
leaq 0xea68c(%rip), %rsi     # 0x1040e6
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %r12
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x1a664
cmpl $0x416d4273, (%r13)     # imm = 0x416D4273
jne 0x19aa0
movq 0x38(%r14), %rdx
movq 0x50(%r14), %rdi
movq 0x58(%r14), %rcx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe4e0b
jmp 0x19c39
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a504
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a664
jmp 0x19c39
leaq 0xea5fc(%rip), %rax     # 0x1040c1
movq %rax, 0x58(%r14)
movq 0x50(%r14), %rdi
cmpq $0x14, 0x18(%rdi)
jge 0x19b06
xorl %ebp, %ebp
jmp 0x19c2e
leaq 0xea6a0(%rip), %rax     # 0x104182
movq %rax, 0x58(%r14)
movq %r14, %rdx
addq $0x8, %rdx
leaq 0xea5d6(%rip), %rsi     # 0x1040ca
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x19c40
pushq $0x4
popq %rsi
callq 0xe3f46
leaq 0xea61b(%rip), %rsi     # 0x104130
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x50(%r14), %rdi
pushq $0x8
popq %rbp
movq %rbp, %rsi
callq 0xe404f
andl $0xfffffff, %eax        # imm = 0xFFFFFFF
leaq 0xc(%rax), %r8
movq %r8, 0x38(%r14)
leaq -0x8(%rax), %rcx
movq %rcx, 0x40(%r14)
leaq 0xea5fb(%rip), %rsi     # 0x104147
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x50(%r14), %rdi
pushq $0xc
popq %rsi
pushq $0x1
popq %r12
movq %rbp, %rdx
movq %rbp, %rcx
xorl %r8d, %r8d
movl %r12d, %r9d
callq 0xe495c
movq %rax, %rbp
movq 0x18(%rax), %rdi
callq 0xeb7e4
leaq 0xea5e5(%rip), %rsi     # 0x10416d
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x40(%r14), %rdx
testq %rdx, %rdx
js 0x19bed
movq 0x8(%rbp), %rax
leaq 0x166cab(%rip), %rsi    # 0x180854
pushq $0x7
popq %rcx
decq %rcx
je 0x19bf2
leaq 0x18(%rsi), %rdi
movq (%rax), %r8
cmpq (%rsi), %r8
movq %rdi, %rsi
jne 0x19bac
movl -0x1c(%rdi), %eax
movq -0xc(%rdi), %rcx
movq %rcx, 0x58(%r14)
cmpl $0x416d4273, (%r13)     # imm = 0x416D4273
je 0x19c00
cmpl $0x3, %eax
jne 0x19c0c
pushq $0x14
popq %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x19c5d
jmp 0x19c2e
xorl %r12d, %r12d
jmp 0x19c2e
cmpl $0x416d4273, (%r13)     # imm = 0x416D4273
jne 0x19c0c
movq 0x58(%r14), %rcx
movq 0x38(%r14), %rdx
movq 0x50(%r14), %rdi
xorl %esi, %esi
jmp 0x19c23
cmpl $0x2, 0x90(%rbx)
jl 0x19c2e
movq 0x50(%r14), %rdi
leaq 0x10e8bd(%rip), %rcx    # 0x1284dd
pushq $0x14
popq %rsi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe4e0b
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe4b94
movq 0x38(%r14), %rax
movq %rax, (%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a4d9
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq