// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/abk.c
// Function: do_read_AmBk
static int do_read_AmBk(deark *c, lctx *d, struct amosbank *bk)
{
	i64 banknum;
	i64 bank_len_code;
	i64 bank_len_raw;
	int membanktype = 0;
	const struct membankinfo *mbi = NULL;
	struct de_stringreaderdata *srd = NULL;
	i64 i;
	int retval = 0;

	if(bk->f->len < 20) goto done;

	banknum = dbuf_getu16be(bk->f, 4);
	de_dbg(c, "bank number (1-15): %d", (int)banknum);

	bank_len_code = dbuf_getu32be(bk->f, 8);
	bank_len_raw = bank_len_code & 0x0fffffff;
	bk->bank_len = bank_len_raw+12;
	bk->bank_data_len = bank_len_raw-8;
	de_dbg(c, "bank length: %"I64_FMT" (dlen=%"I64_FMT", tlen=%"I64_FMT")",
		bank_len_raw, bk->bank_data_len, bk->bank_len);

	srd = dbuf_read_string(bk->f, 12, 8, 8, 0, DE_ENCODING_ASCII);
	de_dbg(c, "bank name: \"%s\"", ucstring_getpsz(srd->str));

	if(bk->bank_data_len<0) goto done;

	for(i=0; membankinfo_arr[i].type!=0; i++) {
		if(!de_memcmp(srd->sz, membankinfo_arr[i].name, 8)) {
			mbi = &membankinfo_arr[i];
			break;
		}
	}

	if(mbi) {
		membanktype = mbi->type;
		bk->file_ext = mbi->file_ext;
	}

	if(d->sig1==CODE_AmBs) {
		// If original file is in AmBs format, just extract the AmBk file.
		dbuf_create_file_from_slice(bk->f, 0, bk->bank_len, bk->file_ext, NULL, 0);
		retval = 1;
		goto done;
	}

	switch(membanktype) {
	case MEMBANKTYPE_PICTURE:
		do_picture_bank(c, d, bk, 20); // 20 to advance past AmBk header
		retval = 1;
		goto done;
	}

	if(c->extract_level>=2) {
		// Extracting the raw memory-bank data can be useful sometimes.
		dbuf_create_file_from_slice(bk->f, 20, bk->bank_data_len, "bin", NULL, 0);
	}

	retval = 1;
done:
	de_destroy_stringreaderdata(c, srd);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x14, 0x18(%rax)
jge 0x1cae6
jmp 0x1cd12
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movl $0x4, %esi
callq 0x1651d0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0x18061f(%rip), %rsi    # 0x19d12c
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movl $0x8, %esi
callq 0x165470
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
andq $0xfffffff, %rax        # imm = 0xFFFFFFF
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
addq $0xc, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x30(%rbp), %rcx
subq $0x8, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x38(%rax), %r8
leaq 0x1805cc(%rip), %rsi    # 0x19d143
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movl $0xc, %esi
movl $0x8, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
movq %rcx, %rdx
callq 0x1664c0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x172500
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x1805a1(%rip), %rsi    # 0x19d169
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jge 0x1cbdf
jmp 0x1cd12
movq $0x0, -0x50(%rbp)
leaq 0x1f6f22(%rip), %rax    # 0x213b10
imulq $0x18, -0x50(%rbp), %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
je 0x1cc4f
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
leaq 0x1f6f06(%rip), %rcx    # 0x213b10
imulq $0x18, -0x50(%rbp), %rdx
addq %rdx, %rcx
addq $0x4, %rcx
movq (%rcx), %rcx
movq (%rax), %rax
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1cc3f
leaq 0x1f6edf(%rip), %rax    # 0x213b10
imulq $0x18, -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
jmp 0x1cc4f
jmp 0x1cc41
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x1cbe7
cmpq $0x0, -0x40(%rbp)
je 0x1cc6f
movq -0x40(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
cmpl $0x416d4273, (%rax)     # imm = 0x416D4273
jne 0x1ccac
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rcx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
movq %r8, %rsi
callq 0x166be0
movl $0x1, -0x54(%rbp)
jmp 0x1cd12
movl -0x34(%rbp), %eax
subl $0x3, %eax
jne 0x1ccd5
jmp 0x1ccb6
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl $0x14, %ecx
callq 0x1b9d0
movl $0x1, -0x54(%rbp)
jmp 0x1cd12
movq -0x8(%rbp), %rax
cmpl $0x2, 0x90(%rax)
jl 0x1cd0b
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdx
movl $0x14, %esi
leaq 0x1a4509(%rip), %rcx    # 0x1c1207
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x166be0
movl $0x1, -0x54(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x166830
movl -0x54(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)