// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movq %r13, 0x48(%rax)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rdx
subq %r13, %rdx
movq %r13, %rsi
callq 0xf1988
movq %rax, 0x50(%r14)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
movl $0x4, %ecx
xorl %r8d, %r8d
callq 0xf1d74
leaq 0x10(%r14), %rdx
leaq 0xf75b6(%rip), %rsi     # 0x1110b2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl 0x4(%r14), %eax
cmpl $0x416d426b, %eax       # imm = 0x416D426B
je 0x19b31
cmpl $0x416d4963, %eax       # imm = 0x416D4963
je 0x19b28
cmpl $0x416d5370, %eax       # imm = 0x416D5370
jne 0x19b3a
leaq 0xf75a4(%rip), %rcx     # 0x1110ca
jmp 0x19b41
leaq 0xf7592(%rip), %rcx     # 0x1110c1
jmp 0x19b41
leaq 0xf759d(%rip), %rcx     # 0x1110d5
jmp 0x19b41
leaq 0xf7655(%rip), %rcx     # 0x111196
movq %rcx, 0x58(%r14)
cmpl $0x416d426b, %eax       # imm = 0x416D426B
je 0x19be8
cmpl $0x416d5370, %eax       # imm = 0x416D5370
je 0x19b62
cmpl $0x416d4963, %eax       # imm = 0x416D4963
jne 0x19bfd
movq 0x50(%r14), %rdi
movl $0x4, %esi
callq 0xef169
movq %rax, 0x60(%r14)
leaq 0xf757f(%rip), %rsi     # 0x1110fa
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x1a806
cmpl $0x416d4273, (%r12)     # imm = 0x416D4273
jne 0x19bbc
movq 0x38(%r14), %rdx
movq 0x50(%r14), %rdi
movq 0x58(%r14), %rcx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
jmp 0x19bd7
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a6a1
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0x1a806
movq 0x38(%r14), %rax
movq %rax, (%r15)
movl $0x1, %ebp
jmp 0x19d4c
movq 0x50(%r14), %rdi
cmpq $0x14, 0x18(%rdi)
jge 0x19c1c
xorl %r13d, %r13d
xorl %ebp, %ebp
jmp 0x19d3a
movq %r14, %rdx
addq $0x8, %rdx
leaq 0xf74d3(%rip), %rsi     # 0x1110de
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x19d4c
movl $0x4, %esi
callq 0xef169
leaq 0xf7517(%rip), %rsi     # 0x111144
xorl %ebp, %ebp
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x50(%r14), %rdi
movl $0x8, %esi
callq 0xef32a
andl $0xfffffff, %eax        # imm = 0xFFFFFFF
leaq 0xc(%rax), %r8
movq %r8, 0x38(%r14)
leaq -0x8(%rax), %rcx
movq %rcx, 0x40(%r14)
leaq 0xf74f6(%rip), %rsi     # 0x11115b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x50(%r14), %rdi
movl $0xc, %esi
movl $0x8, %edx
movl $0x8, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xefe67
movq %rax, %r13
movq 0x18(%rax), %rdi
callq 0xf74bd
leaq 0xf74db(%rip), %rsi     # 0x111181
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x40(%r14), %rdx
testq %rdx, %rdx
js 0x19d3a
movq 0x8(%r13), %rsi
leaq 0x163aa9(%rip), %rax    # 0x17d770
movl $0x6, %ecx
movq (%rsi), %rsi
cmpq 0x4(%rax), %rsi
je 0x19ce0
addq $0x18, %rax
decq %rcx
jne 0x19ccf
xorl %eax, %eax
testq %rax, %rax
je 0x19cf7
movl (%rax), %ecx
movq 0x10(%rax), %rax
movq %rax, 0x58(%r14)
cmpl $0x3, %ecx
sete %al
jmp 0x19cf9
xorl %eax, %eax
cmpl $0x416d4273, (%r12)     # imm = 0x416D4273
jne 0x19d1e
movq 0x38(%r14), %rdx
movq 0x50(%r14), %rdi
movq 0x58(%r14), %rcx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
jmp 0x19d35
testb %al, %al
je 0x19d68
movl $0x14, %ecx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x19d93
movl $0x1, %ebp
movq %rbx, %rdi
movq %r13, %rsi
callq 0xf00aa
movq 0x38(%r14), %rax
movq %rax, (%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a676
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %ebp
cmpl $0x2, 0x90(%rbx)
jl 0x19d3a
movq 0x50(%r14), %rdi
leaq 0x11b6b0(%rip), %rcx    # 0x135431
movl $0x14, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
jmp 0x19d3a