// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/ilbm.c
// Function: do_delta74_blocks
static void do_delta74_blocks(deark *c, struct d74state *d74s)
{
	i64 blkidx, rowidx, planeidx, byteidx;
	i64 d74_bytes_per_row_per_plane;

	// Reportedly, the 'offset' field assumes a potentially-different measuring
	// system than one would expect.
	d74_bytes_per_row_per_plane = (d74s->ibi->width + 7)/8;

	for(blkidx=0; blkidx<d74s->nblocks; blkidx++) {
		i64 offset;
		i64 block_srcpos = d74s->pos;
		i64 block_dstpos;

		if(d74s->pos+2 >= d74s->endpos) goto done;
		offset = de_getu16be_p(&d74s->pos);

		block_dstpos = (offset / d74_bytes_per_row_per_plane) * d74s->ibi->frame_buffer_rowspan +
			(offset % d74_bytes_per_row_per_plane);

		for(rowidx=0; rowidx<d74s->nrows; rowidx++) {
			for(planeidx=0; planeidx<d74s->ibi->planes_total; planeidx++) {
				i64 dstpos;

				// Calculate the offset in our frame buffer.
				dstpos = block_dstpos + (rowidx * d74s->ibi->frame_buffer_rowspan) +
					planeidx * d74s->ibi->bytes_per_row_per_plane;

				for(byteidx=0; byteidx<d74s->nbytes; byteidx++) {
					u8 val;

					val = de_getbyte_p(&d74s->pos);
					if(d74s->op) val ^= dbuf_getbyte(d74s->frctx->frame_buffer, dstpos);
					dbuf_writebyte_at(d74s->frctx->frame_buffer, dstpos, val);
					dstpos++;
				}
			}
		}

		if((d74s->pos - block_srcpos) & 0x1) {
			d74s->pos++; // padding byte
		}
	}

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x20(%rsp)
movq (%rsi), %rax
movq (%rax), %rcx
leaq 0x7(%rcx), %rax
addq $0xe, %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
movq %rcx, 0x8(%rsp)
cmpq $0x0, 0x28(%rsi)
jle 0x7cedf
movq %rsi, %rbx
sarq $0x3, 0x8(%rsp)
leaq 0x10(%rsi), %r15
movq 0x10(%rsi), %rdx
xorl %ecx, %ecx
leaq 0x2(%rdx), %rax
cmpq 0x18(%rbx), %rax
jge 0x7cedf
movq %rdx, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r15, %rsi
callq 0xeeb91
movq 0x30(%rbx), %rsi
cqto
idivq 0x8(%rsp)
movq %rdx, 0x18(%rsp)
testq %rsi, %rsi
jle 0x7ceb6
movq (%rbx), %rcx
imulq 0x48(%rcx), %rax
addq %rax, 0x18(%rsp)
movq $0x0, (%rsp)
cmpq $0x0, 0x18(%rcx)
jle 0x7cea2
movq 0x38(%rbx), %rax
xorl %edx, %edx
testq %rax, %rax
jle 0x7ce91
movq 0x48(%rcx), %rax
imulq (%rsp), %rax
addq 0x18(%rsp), %rax
movq 0x40(%rcx), %r12
movq %rdx, 0x30(%rsp)
imulq %rdx, %r12
addq %rax, %r12
xorl %r14d, %r14d
leaq (%r12,%r14), %r13
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r15, %rsi
callq 0xeeb4b
movl %eax, %ebp
cmpl $0x0, 0x20(%rbx)
je 0x7ce69
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
movq %r13, %rsi
callq 0xeead3
xorb %al, %bpl
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rdi
movzbl %bpl, %edx
movq %r13, %rsi
callq 0xf0e8b
incq %r14
movq 0x38(%rbx), %rax
cmpq %rax, %r14
jl 0x7ce39
movq (%rbx), %rcx
movq 0x30(%rsp), %rdx
incq %rdx
cmpq 0x18(%rcx), %rdx
jl 0x7ce13
movq 0x30(%rbx), %rsi
movq (%rsp), %rdx
incq %rdx
movq %rdx, (%rsp)
cmpq %rsi, %rdx
jl 0x7ce02
movq (%r15), %rax
movl %eax, %ecx
subl 0x28(%rsp), %ecx
testb $0x1, %cl
je 0x7ceca
incq %rax
movq %rax, (%r15)
movq 0x10(%rsp), %rcx
incq %rcx
movq %rax, %rdx
cmpq 0x28(%rbx), %rcx
jl 0x7cdab
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq