// File: /workspace/github_c_makefile_star2_O3/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, 0x10(%rsp)
movq %rdi, %r14
leaq 0x4(%rdx), %r13
movq %rdx, 0x18(%rsp)
leaq 0x10(%rdx), %r15
xorl %ebx, %ebx
movq %r13, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xeed0d
movq %rax, %rcx
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq %rax, 0x70(%rsp,%rbx)
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xeeb67
movq %rax, %r12
movq %rax, 0x50(%rsp,%rbx)
leaq 0xef7e9(%rip), %rax     # 0x185360
movq (%rbx,%rax), %rdx
movq %r14, %rdi
leaq 0x9a881(%rip), %rsi     # 0x130406
movl %r12d, %ecx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0xe8617
addq %r12, %rbp
addq $0x2, %r15
addq $0x4, %r13
addq $0x8, %rbx
cmpq $0x18, %rbx
jne 0x95b36
movq 0x18(%rsp), %rax
leaq (%rax,%rbp,8), %rbx
addq $0x16, %rbx
movq 0x18(%r14), %rdi
cmpq %rbx, 0x18(%rdi)
jle 0x95c56
movq %rbx, %rsi
callq 0xeed0d
cmpq $0x50414c54, %rax       # imm = 0x50414C54
jne 0x95c37
leaq 0x9a882(%rip), %rsi     # 0x130462
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r14), %rsi
addq $0x4, %rbx
movq 0x10(%rsp), %rax
leaq 0x2c(%rax), %rcx
movl $0x0, (%rsp)
movl $0x10, %r8d
movl $0x10, %r9d
movq %r14, %rdi
movq %rbx, %rdx
callq 0xf876e
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
jmp 0x95c56
leaq 0x9a7fa(%rip), %rsi     # 0x130438
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe8faa
movq 0x10(%rsp), %rax
movl $0xffffffff, 0x2c(%rax) # imm = 0xFFFFFFFF
addq $0x2c, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
leaq 0xef6f4(%rip), %rcx     # 0x185360
movq 0x8(%rsp), %rax
movq 0x50(%rsp,%rax,8), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
jle 0x95f74
movq 0x8(%rsp), %rax
movq 0x70(%rsp,%rax,8), %r15
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
addq $-0x8, %rax
cmpq %rax, %r15
jg 0x95f74
movq 0x8(%rsp), %rax
movq (%rcx,%rax,8), %rdx
movq %r14, %rdi
leaq 0x9a7c1(%rip), %rsi     # 0x130477
movq %rdx, 0x40(%rsp)
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %r15, 0x38(%rsp)
xorl %ecx, %ecx
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, 0x48(%rsp)
movq %r14, %rdi
leaq 0x9a7a6(%rip), %rsi     # 0x130498
movq 0x40(%rsp), %rdx
movq %rcx, 0x20(%rsp)
movl %r15d, %r8d
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
movl $0x40, %esi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, %rbx
movl $0x40, %esi
movq %r14, %rdi
callq 0xe8b2b
movq %rax, %r12
leaq 0x9a462(%rip), %rax     # 0x13019c
movq 0x8(%rsp), %rcx
movzbl (%rcx,%rax), %ecx
movq %rcx, (%rbx)
movl $0x1, %eax
shlq %cl, %rax
movq %rax, 0x8(%rbx)
movq $0x1, (%r12)
movq $0x2, 0x8(%r12)
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xeed0d
movq %rax, %r13
movq 0x18(%r14), %rdi
leaq 0x4(%r15), %rsi
callq 0xeead3
movzbl %al, %ebp
movl %ebp, %eax
shll $0x4, %eax
movq %rax, 0x10(%rbx)
movq 0x18(%r14), %rdi
movq %r15, 0x28(%rsp)
leaq 0x5(%r15), %rsi
callq 0xeead3
movzbl %al, %edx
movq %rdx, 0x18(%rbx)
movq 0x10(%rbx), %rsi
movq %r14, %rdi
callq 0xe8f88
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r14, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x95eed
movq 0x10(%rbx), %rax
movq %rax, 0x10(%r12)
movq 0x18(%rbx), %r15
movq %r15, 0x18(%r12)
addq 0x38(%rsp), %r13
addl %ebp, %ebp
imulq %rbp, %r15
movq %r14, %rdi
leaq 0x9a6cd(%rip), %rsi     # 0x1304bc
movl %r13d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r14), %rax
cmpq 0x18(%rax), %r13
jge 0x95eed
leaq (%r15,%r13), %rdx
movq %rdx, 0x30(%rsp)
imulq (%rbx), %rbp
imulq 0x18(%rbx), %rbp
movq %r14, %rdi
leaq 0x9a6af(%rip), %rsi     # 0x1304d5
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r14), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xf1388
movq %rax, 0x20(%r12)
movq 0x18(%r14), %rdi
movq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0xf1388
movq %rax, 0x20(%rbx)
leaq 0x48(%rsp), %rax
movq %rax, 0x30(%r12)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%rbx)
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r14, %rdi
movl $0x1, %ecx
callq 0xf266a
movq %rax, 0x38(%r12)
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %r14, %rdi
movl $0x4, %ecx
callq 0xf266a
movq %rax, 0x38(%rbx)
movq %r14, %rdi
movq %r12, %rsi
callq 0xf8b10
movq %r14, %rdi
movq %rbx, %rsi
callq 0xf8b10
movq 0x38(%rbx), %rdi
movq 0x38(%r12), %rsi
xorl %edx, %edx
callq 0xf37bd
movq %r14, %rdi
callq 0xea504
movq %rax, %r13
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0xf8e1c
movq 0x38(%rbx), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xf22d0
jmp 0x95ef0
xorl %r13d, %r13d
movq 0x20(%rbx), %rdi
callq 0xf052b
movq 0x38(%rbx), %rdi
callq 0xf228d
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe8be2
movq 0x20(%r12), %rdi
callq 0xf052b
movq 0x38(%r12), %rdi
callq 0xf228d
movq %r14, %rdi
movq %r12, %rsi
callq 0xe8be2
movq %r14, %rdi
movq %r13, %rsi
callq 0xea50e
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x28(%rsp), %r15
addq $0x8, %r15
cmpq %rcx, 0x18(%rsp)
jne 0x95cd9
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
leaq 0xef3ec(%rip), %rcx     # 0x185360
movq 0x8(%rsp), %rdx
incq %rdx
movq %rdx, 0x8(%rsp)
cmpq $0x3, %rdx
jne 0x95c6c
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq