// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rsi, %r12
movq %rdi, %rbx
movl $0x4b8, %esi            # imm = 0x4B8
callq 0xe8b2b
movq %rax, %r14
movq %r13, 0x48(%rax)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r13, %rdx
movq %r13, %rsi
callq 0xf1388
movq %rax, 0x50(%r14)
xorl %r13d, %r13d
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1762
leaq 0x10(%r14), %rdx
leaq 0xf7511(%rip), %rsi     # 0x1110c1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x4(%r14), %eax
cmpl $0x416d426b, %eax       # imm = 0x416D426B
je 0x19c76
cmpl $0x416d4963, %eax       # imm = 0x416D4963
je 0x19be4
cmpl $0x416d5370, %eax       # imm = 0x416D5370
jne 0x19c93
leaq 0xf74f7(%rip), %rax     # 0x1110d9
jmp 0x19beb
leaq 0xf74e5(%rip), %rax     # 0x1110d0
movq %rax, 0x58(%r14)
movq 0x50(%r14), %rdi
movl $0x4, %esi
callq 0xeeb67
movq %rax, 0x60(%r14)
leaq 0xf7501(%rip), %rsi     # 0x111109
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x1a8aa
cmpl $0x416d4273, (%r12)     # imm = 0x416D4273
jne 0x19c49
movq 0x38(%r14), %rdx
movq 0x50(%r14), %rdi
movq 0x58(%r14), %rcx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xefd48
jmp 0x19c64
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a745
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0x1a8aa
movq 0x38(%r14), %rax
movq %rax, (%r15)
movl $0x1, %r13d
jmp 0x19e1f
leaq 0xf7467(%rip), %rax     # 0x1110e4
movq %rax, 0x58(%r14)
movq 0x50(%r14), %rdi
cmpq $0x14, 0x18(%rdi)
jge 0x19cbe
xorl %ebp, %ebp
jmp 0x19e0d
leaq 0xf750b(%rip), %rax     # 0x1111a5
movq %rax, 0x58(%r14)
movq %r14, %rdx
addq $0x8, %rdx
leaq 0xf7441(%rip), %rsi     # 0x1110ed
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x19e1f
movl $0x4, %esi
callq 0xeeb67
leaq 0xf7484(%rip), %rsi     # 0x111153
xorl %r13d, %r13d
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x50(%r14), %rdi
movl $0x8, %esi
callq 0xeed0d
andl $0xfffffff, %eax        # imm = 0xFFFFFFF
leaq 0xc(%rax), %r8
movq %r8, 0x38(%r14)
leaq -0x8(%rax), %rcx
movq %rcx, 0x40(%r14)
leaq 0xf7462(%rip), %rsi     # 0x11116a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x50(%r14), %rdi
movl $0xc, %esi
movl $0x8, %edx
movl $0x8, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xef87c
movq %rax, %rbp
movq 0x18(%rax), %rdi
callq 0xf705c
leaq 0xf7447(%rip), %rsi     # 0x111190
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x40(%r14), %rdx
testq %rdx, %rdx
js 0x19e0d
movq 0x8(%rbp), %rsi
xorl %eax, %eax
leaq 0x163aa0(%rip), %rcx    # 0x17d810
movq (%rsi), %rsi
cmpq 0x4(%rax,%rcx), %rsi
je 0x19d96
addq $0x18, %rax
cmpq $0x90, %rax
jne 0x19d73
cmpl $0x416d4273, (%r12)     # imm = 0x416D4273
jne 0x19de3
movq 0x58(%r14), %rcx
jmp 0x19dac
movl (%rax,%rcx), %esi
movq 0x10(%rax,%rcx), %rcx
movq %rcx, 0x58(%r14)
cmpl $0x416d4273, (%r12)     # imm = 0x416D4273
jne 0x19dc3
movq 0x38(%r14), %rdx
movq 0x50(%r14), %rdi
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xefd48
jmp 0x19ddb
cmpl $0x3, %esi
jne 0x19de3
movl $0x14, %ecx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x19e3c
movl $0x1, %r13d
jmp 0x19e0d
movl $0x1, %r13d
cmpl $0x2, 0x90(%rbx)
jl 0x19e0d
movq 0x50(%r14), %rdi
leaq 0x11b9c0(%rip), %rcx    # 0x1357bd
movl $0x14, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xefd48
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xefaca
movq 0x38(%r14), %rax
movq %rax, (%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a71a
movl %r13d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq