// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/mbk.c
// Function: run_mbk_mbs_internal
static void run_mbk_mbs_internal(deark *c, de_module_params *mparams, UI mode)
{
	lctx *d = NULL;
	u32 id;
	const char *s;
	char opt_res0_c = '\0';
	char opt_res1_c = '\0';
	u8 buf[10];

	d = de_malloc(c, sizeof(lctx));

	d->imgtype_of_res1 = PKPIC_IMGTYPE_UNKNOWN;
	d->imgtype_of_res0 = PKPIC_IMGTYPE_UNKNOWN;

	if(mode==1) { // .pp1 extension
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP1;
	}
	else if(mode==2) {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_MED;
	}
	else if(mode==3) {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP3;
	}
	else if(mode==4) { // .daj extension
		d->imgtype_of_res0 = PKPIC_IMGTYPE_M4P;
		d->imgtype_of_res1 = PKPIC_IMGTYPE_MED;
	}

	s = de_get_ext_option(c, "stos:res0");
	if(s) opt_res0_c = s[0];
	s = de_get_ext_option(c, "stos:res1");
	if(s) opt_res1_c = s[0];

	if(opt_res1_c=='l') {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP1;
	}
	else if(opt_res1_c=='m') {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_MED;
	}
	else if(opt_res1_c=='h') {
		d->imgtype_of_res1 = PKPIC_IMGTYPE_PP3;
	}

	if(opt_res0_c=='l') {
		d->imgtype_of_res0 = PKPIC_IMGTYPE_LOW;
	}
	else if(opt_res0_c=='m') {
		d->imgtype_of_res0 = PKPIC_IMGTYPE_M4P;
	}

	de_read(buf, 0, sizeof(buf));
	if(!de_memcmp(buf, g_lion_sig, 10)) {
		d->is_container_fmt = 1;
		d->banknum = de_getu32be(10);
		if(d->banknum==0) {
			de_declare_fmt(c, "STOS MBS");
			do_mbs(c, d);
		}
		else {
			de_declare_fmt(c, "STOS MBK");
			do_mbk(c, d);
		}
	}
	else {
		id = (u32)de_getu32be_direct(buf);

		if(id==BANKID_SPRITE) {
			de_declare_fmt(c, "STOS Sprite Bank");
			do_sprite_bank(c, d, 0);
		}
		else if(id==BANKID_PKSCREEN) {
			de_declare_fmt(c, "STOS Packed Screen / Picture Packer");
			do_pkscreen_bank(c, d, 0);
		}
		else {
			de_err(c, "Not a (supported) STOS/MBK format");
		}
	}

	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movl $0x430, %esi            # imm = 0x430
callq 0xe8b2b
movq %rax, %r15
movabsq $0x6300000063, %rax     # imm = 0x6300000063
movq %rax, 0x4(%r15)
decl %r14d
cmpl $0x3, %r14d
ja 0x955f7
leaq 0x9aba9(%rip), %rax     # 0x130178
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x6e, %eax
jmp 0x955f3
movl $0x70, %eax
jmp 0x955f3
movl $0x78, 0x4(%r15)
movl $0x65, %eax
movl %eax, 0x8(%r15)
leaq 0x9ac50(%rip), %rsi     # 0x13024e
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x95610
movsbl (%rax), %ebp
jmp 0x95612
xorl %ebp, %ebp
leaq 0x9ac3f(%rip), %rsi     # 0x130258
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x9564f
movzbl (%rax), %eax
cmpl $0x68, %eax
je 0x95646
cmpl $0x6c, %eax
je 0x9563f
cmpl $0x6d, %eax
jne 0x9564f
movl $0x65, %eax
jmp 0x9564b
movl $0x6e, %eax
jmp 0x9564b
movl $0x70, %eax
movl %eax, 0x8(%r15)
cmpl $0x6c, %ebp
je 0x95660
cmpl $0x6d, %ebp
jne 0x95669
movl $0x78, %eax
jmp 0x95665
movl $0x64, %eax
movl %eax, 0x4(%r15)
movq 0x18(%rbx), %rdi
leaq 0x1e(%rsp), %r14
movl $0xa, %ecx
movq %r14, %rsi
xorl %edx, %edx
callq 0xee8d4
movabsq $0x62756f706e6f694c, %rax # imm = 0x62756F706E6F694C
xorq (%r14), %rax
movzwl 0x8(%r14), %ecx
xorq $0x6b6e, %rcx           # imm = 0x6B6E
orq %rax, %rcx
je 0x956e0
leaq 0x1e(%rsp), %rdi
callq 0xeed08
cmpl $0x6071963, %eax        # imm = 0x6071963
je 0x95796
cmpl $0x19861987, %eax       # imm = 0x19861987
jne 0x957b7
leaq 0x9abae(%rip), %rsi     # 0x130274
movq %rbx, %rdi
callq 0xeb125
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x95b07
jmp 0x95aed
movb $0x1, (%r15)
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeed0d
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x957cd
leaq 0x9ab65(%rip), %rsi     # 0x13026b
movq %rbx, %rdi
callq 0xeb125
leaq 0x9abd2(%rip), %rsi     # 0x1302e7
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl 0x10(%r15), %edx
leaq 0x9abbf(%rip), %rsi     # 0x1302f8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xeed0d
movq %rax, %rdx
shrq $0x18, %rdx
movb %dl, 0x18(%r15)
andl $0xffffff, %eax         # imm = 0xFFFFFF
movq %rax, 0x20(%r15)
movzbl %dl, %eax
leaq 0x969e2(%rip), %rcx     # 0x12c151
leal -0x81(%rax), %esi
cmpl $0x4, %esi
ja 0x957f4
leaq 0x9aa07(%rip), %rax     # 0x130188
movslq (%rax,%rsi,4), %rsi
addq %rax, %rsi
jmpq *%rsi
leaq 0xa4b94(%rip), %rcx     # 0x13a325
jmp 0x95829
leaq 0x9aae8(%rip), %rsi     # 0x130285
movq %rbx, %rdi
callq 0xeb125
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x95f9d
jmp 0x95aed
leaq 0x9aaeb(%rip), %rsi     # 0x1302a9
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x95aed
leaq 0x9aa8e(%rip), %rsi     # 0x130262
movq %rbx, %rdi
callq 0xeb125
leaq 0x9aaf3(%rip), %rsi     # 0x1302d6
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
jmp 0x95aed
cmpl $0x2, %eax
je 0x95822
cmpl $0x1, %eax
jne 0x95829
leaq 0x9ab03(%rip), %rcx     # 0x130308
jmp 0x95829
leaq 0x9ab0a(%rip), %rcx     # 0x130318
jmp 0x95829
leaq 0x9aaf6(%rip), %rcx     # 0x13030d
jmp 0x95829
leaq 0x893c1(%rip), %rcx     # 0x11ebe1
jmp 0x95829
leaq 0x9ab3d(%rip), %rcx     # 0x130366
movzbl %dl, %edx
leaq 0x9aaf2(%rip), %rsi     # 0x130325
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x20(%r15), %edx
leaq 0x9aaf4(%rip), %rsi     # 0x13033c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $-0x7f, 0x18(%r15)
jne 0x95aed
leaq 0x9aad9(%rip), %rsi     # 0x13034a
movq %rbx, %rdi
movl $0x12, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl $0x12, %esi
callq 0xeed0d
movl %eax, 0x28(%r15)
leaq 0x968ab(%rip), %rcx     # 0x12c151
cmpl $0x19861986, %eax       # imm = 0x19861986
jle 0x958cb
cmpl $0x19861987, %eax       # imm = 0x19861987
je 0x958e2
cmpl $0x28091960, %eax       # imm = 0x28091960
je 0x958eb
cmpl $0x4d414553, %eax       # imm = 0x4D414553
jne 0x958fb
leaq 0x9aac5(%rip), %rcx     # 0x13038e
jmp 0x958fb
cmpl $0x6071963, %eax        # imm = 0x6071963
je 0x958f4
cmpl $0x13490157, %eax       # imm = 0x13490157
jne 0x958fb
leaq 0x9aa8d(%rip), %rcx     # 0x13036d
jmp 0x958fb
leaq 0x9aa99(%rip), %rcx     # 0x130382
jmp 0x958fb
leaq 0x9aa86(%rip), %rcx     # 0x130378
jmp 0x958fb
leaq 0x9aa64(%rip), %rcx     # 0x13035f
leaq 0x9aa95(%rip), %rsi     # 0x130397
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl 0x28(%r15), %eax
cmpl $0x6071963, %eax        # imm = 0x6071963
movq %r15, 0x8(%rsp)
je 0x95acb
cmpl $0x28091960, %eax       # imm = 0x28091960
je 0x9594b
cmpl $0x19861987, %eax       # imm = 0x19861987
jne 0x95adb
movl $0x12, %edx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x95b07
jmp 0x95adb
movq 0x18(%rbx), %rdi
movl $0x16, %esi
callq 0xeeb67
movq %rax, %r14
leaq 0x9aa4e(%rip), %rsi     # 0x1303b1
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
testq %r14, %r14
jle 0x95adb
movl $0x24, %r12d
xorl %ebp, %ebp
movq %r14, 0x10(%rsp)
leal -0xc(%r12), %ecx
movq %rbx, %rdi
leaq 0x9aa30(%rip), %rsi     # 0x1303c5
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
leaq -0x8(%r12), %rsi
callq 0xeeb67
movq %rbx, %rdi
leaq 0x9aa12(%rip), %rsi     # 0x1303d5
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq -0x6(%r12), %rsi
callq 0xeeb67
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq -0x4(%r12), %rsi
callq 0xeeb67
movq %rax, %r15
movq %rbx, %rdi
leaq 0x9a9f1(%rip), %rsi     # 0x1303e9
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe8617
testq %r15, %r15
setne %al
testq %r14, %r14
sete %cl
orb %al, %cl
movzbl %cl, %r14d
movl $0x10, %esi
movl $0x10, %edx
movq %rbx, %rdi
movl $0x2, %ecx
callq 0xf266a
movq %rax, %r13
movl $0x10, %esi
movl $0x10, %edx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0xf266a
movq %rax, %r15
leaq -0x2(%r12), %rsi
movq 0x18(%rbx), %rdi
movl $0x4, %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0xf2a40
movq 0x18(%rbx), %rdi
movl $0x4, %edx
movq %r12, %rsi
movq %r13, %rcx
movl %r14d, %r8d
movq 0x10(%rsp), %r14
callq 0xf2a40
movq %r13, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xf37bd
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf22dc
movq %r13, %rdi
callq 0xf228d
movq %r15, %rdi
callq 0xf228d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
incq %rbp
addq $0x54, %r12
cmpq %rbp, %r14
jne 0x95986
jmp 0x95adb
movl $0x12, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x95f9d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq