// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/mbk.c
// Function: do_sprite_bank
static void do_sprite_bank(deark *c, lctx *d, i64 pos)
{
	i64 res;
	i64 paramoffs_raw[3]; // One for each resolution: low, med, hi
	i64 paramoffs[3];
	i64 nsprites[3];
	i64 nsprites_total = 0;
	i64 pal_pos;

	for(res=0; res<3; res++) {
		paramoffs_raw[res] = de_getu32be(pos+4+4*res);
		// paramoffs is relative to the first position after the ID.
		paramoffs[res] = pos + 4 + paramoffs_raw[res];
		nsprites[res] = de_getu16be(pos+16+2*res);
		de_dbg(c, "%s-res sprites: %d, param blk offset: %d ("DE_CHAR_RIGHTARROW" %d)", sprite_res_name[res],
			(int)nsprites[res], (int)paramoffs_raw[res], (int)paramoffs[res]);
		nsprites_total += nsprites[res];
	}

	// TODO: What's the right way to calculate the position of the palette?
	pal_pos = pos + 22 + (nsprites_total)*8;
	read_sprite_palette(c, d, pal_pos);

	for(res=0; res<3; res++) {
		if(nsprites[res]<1) continue;
		if(paramoffs[res]>(c->infile->len-8)) continue;
		do_sprite_param_blocks(c, d, res, nsprites[res], paramoffs[res]);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
leaq 0x4(%rdx), %r13
movq %rdx, 0x10(%rsp)
leaq 0x10(%rdx), %r12
movq %r13, 0x18(%rsp)
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpq $0x18, %r15
je 0x90662
movq 0x18(%r14), %rdi
movq %r13, %rsi
movq %rcx, 0x20(%rsp)
callq 0xe404f
movq %rax, %rbp
movq 0x18(%rsp), %rax
leaq (%rax,%rbp), %rbx
movq %rbx, 0x70(%rsp,%r15)
movq 0x18(%r14), %rdi
movq %r12, %rsi
callq 0xe3f46
movq %r12, 0x28(%rsp)
movq %rax, %r12
movq %rax, 0x50(%rsp,%r15)
leaq 0xf7d4a(%rip), %rax     # 0x188370
movq (%r15,%rax), %rdx
movq %r14, %rdi
leaq 0x92b31(%rip), %rsi     # 0x123165
movl %r12d, %ecx
movl %ebp, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0xde896
movq 0x20(%rsp), %rcx
addq %r12, %rcx
movq 0x28(%rsp), %r12
addq $0x8, %r15
addq $0x2, %r12
addq $0x4, %r13
jmp 0x905de
movq 0x10(%rsp), %rax
leaq (%rax,%rcx,8), %rbx
addq $0x16, %rbx
movq 0x18(%r14), %rdi
cmpq %rbx, 0x18(%rdi)
jle 0x906f7
movq %rbx, %rsi
callq 0xe404f
cmpq $0x50414c54, %rax       # imm = 0x50414C54
jne 0x906db
leaq 0x92b31(%rip), %rsi     # 0x1231c1
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xdeba7
movq 0x18(%r14), %rsi
addq $0x4, %rbx
movq 0x8(%rsp), %rax
leaq 0x2c(%rax), %rcx
andl $0x0, (%rsp)
pushq $0x10
popq %r8
movq %r14, %rdi
movq %rbx, %rdx
movq %r8, %r9
callq 0xecea5
pushq $-0x1
popq %rsi
movq %r14, %rdi
callq 0xdeba7
jmp 0x906f7
leaq 0x92ab5(%rip), %rsi     # 0x123197
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xdf05a
movq 0x8(%rsp), %rax
orl $-0x1, 0x2c(%rax)
addq $0x2c, 0x8(%rsp)
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0x90a0e
movq 0x50(%rsp,%r13,8), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
jle 0x90a06
movq 0x70(%rsp,%r13,8), %r15
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
addq $-0x8, %rax
cmpq %rax, %r15
jg 0x90a06
leaq 0xf7c32(%rip), %rax     # 0x188370
movq (%rax,%r13,8), %rdx
movq %r14, %rdi
leaq 0x92a8a(%rip), %rsi     # 0x1231d6
movq %rdx, 0x40(%rsp)
movl %r15d, %ecx
xorl %eax, %eax
callq 0xde896
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq %r15, %rbx
xorl %ecx, %ecx
movq %r13, 0x10(%rsp)
movq %r15, 0x38(%rsp)
cmpq %rcx, 0x18(%rsp)
je 0x909fb
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, 0x48(%rsp)
movq %r14, %rdi
leaq 0x92a5e(%rip), %rsi     # 0x1231f7
movq 0x40(%rsp), %rdx
movq %rcx, 0x20(%rsp)
movl %ebx, %r8d
xorl %eax, %eax
callq 0xde896
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq %r14, %rdi
pushq $0x40
popq %r12
movq %r12, %rsi
callq 0xdecaf
movq %rax, %r15
movq %r14, %rdi
movq %r12, %rsi
callq 0xdecaf
movq %rax, %r12
leaq 0x9272d(%rip), %rax     # 0x122f0c
movzbl (%r13,%rax), %ecx
movq %rcx, (%r15)
pushq $0x1
popq %rax
shlq %cl, %rax
movq %rax, 0x8(%r15)
movq $0x1, (%r12)
movq $0x2, 0x8(%r12)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xe404f
movq %rax, %rbp
movq 0x18(%r14), %rdi
leaq 0x4(%rbx), %rsi
callq 0xe3eb4
movzbl %al, %r13d
movl %r13d, %eax
shll $0x4, %eax
movq %rax, 0x10(%r15)
movq 0x18(%r14), %rdi
movq %rbx, 0x28(%rsp)
leaq 0x5(%rbx), %rsi
callq 0xe3eb4
movzbl %al, %edx
movq %rdx, 0x18(%r15)
movq 0x10(%r15), %rsi
movq %r14, %rdi
callq 0xdf038
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %r14, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x9098b
movq 0x10(%r15), %rax
movq %rax, 0x10(%r12)
movq 0x18(%r15), %rbx
movq %rbx, 0x18(%r12)
addq 0x38(%rsp), %rbp
addl %r13d, %r13d
imulq %r13, %rbx
movq %r14, %rdi
leaq 0x92989(%rip), %rsi     # 0x12321b
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rax
cmpq 0x18(%rax), %rbp
jge 0x9098b
leaq (%rbx,%rbp), %rdx
movq %rdx, 0x30(%rsp)
imulq (%r15), %r13
imulq 0x18(%r15), %r13
movq %r14, %rdi
leaq 0x9296d(%rip), %rsi     # 0x123234
movl %r13d, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0xe6248
movq %rax, 0x20(%r12)
movq 0x18(%r14), %rdi
movq 0x30(%rsp), %rsi
movq %r13, %rdx
callq 0xe6248
movq %rax, 0x20(%r15)
leaq 0x48(%rsp), %rax
movq %rax, 0x30(%r12)
movq 0x8(%rsp), %rax
movq %rax, 0x30(%r15)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %r14, %rdi
pushq $0x1
popq %rcx
callq 0xe7437
movq %rax, 0x38(%r12)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %r14, %rdi
pushq $0x4
popq %rcx
callq 0xe7437
movq %rax, 0x38(%r15)
movq %r14, %rdi
movq %r12, %rsi
callq 0xed22a
movq %r14, %rdi
movq %r15, %rsi
callq 0xed22a
movq 0x38(%r15), %rdi
movq 0x38(%r12), %rsi
xorl %edx, %edx
callq 0xe83f8
movq %r14, %rdi
callq 0xdfd1a
movq %rax, %r13
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xed4e6
movq 0x38(%r15), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xe70ab
jmp 0x9098e
xorl %r13d, %r13d
movq 0x20(%r15), %rdi
callq 0xe5591
movq 0x38(%r15), %rdi
callq 0xe7084
movq %r14, %rdi
movq %r15, %rsi
callq 0xded32
movq 0x20(%r12), %rdi
callq 0xe5591
movq 0x38(%r12), %rdi
callq 0xe7084
movq %r14, %rdi
movq %r12, %rsi
callq 0xded32
movq %r14, %rdi
movq %r13, %rsi
callq 0xdfd24
movq %r14, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %rbx
addq $0x8, %rbx
movq 0x10(%rsp), %r13
jmp 0x90775
movq %r14, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
incq %r13
jmp 0x90700
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq