// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ilbm.c
// Function: decompress_rgbn8
static int decompress_rgbn8(deark *c, lctx *d, struct imgbody_info *ibi,
	i64 pos1, i64 len, dbuf *unc_pixels)
{
	int retval = 0;
	i64 pos = pos1;
	i64 endpos = pos1+len;
	i64 npixels = 0;
	i64 npixels_expected;
	u8 is_rgb8 = (d->formtype==CODE_RGB8);

	npixels_expected = ibi->width * ibi->height;

	while(1) {
		i64 i;
		u32 n;
		i64 count;
		u8 cbuf[3];

		if(npixels >= npixels_expected) {
			retval = 1;
			goto done;
		}
		if(pos >= endpos) {
			goto done;
		}


		if(is_rgb8) {
			n = (u32)de_getu32be_p(&pos);
			cbuf[0] = (u8)(n>>24);
			cbuf[1] = (u8)((n&0x00ff0000U)>>16);
			cbuf[2] = (u8)((n&0x0000ff00U)>>8);
			// TODO?: There's also a "genlock" bit that's sort of a transparency mask.
			count = (i64)(n & 0x7f);
		}
		else {
			n = (u32)de_getu16be_p(&pos);
			cbuf[0] = (u8)(n>>12);
			cbuf[1] = (u8)((n&0x0f00)>>8);
			cbuf[2] = (u8)((n&0x00f0)>>4);
			for(i=0; i<3; i++) {
				cbuf[i] *= 17;
			}
			count = (i64)(n & 0x07);
		}

		if(count==0) {
			u8 c2raw;

			c2raw = de_getbyte_p(&pos);
			if(c2raw!=0) {
				count = (i64)c2raw;
			}
			else {
				count = de_getu16be_p(&pos);
			}
		}

		for(i=0; i<count; i++) {
			dbuf_write(unc_pixels, cbuf, 3);
		}
		npixels += count;
	}

done:
	if(!retval) {
		de_err(c, "RGBN/RGB8 decompression failed");
	}
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x52474238, 0x4(%rax)  # imm = 0x52474238
sete %al
andb $0x1, %al
movzbl %al, %eax
movb %al, -0x59(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
imulq 0x8(%rcx), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jl 0xb28e6
movl $0x1, -0x34(%rbp)
jmp 0xb2a3c
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jl 0xb28f5
jmp 0xb2a3c
cmpb $0x0, -0x59(%rbp)
je 0xb2942
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1654b0
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
shrl $0x18, %eax
movb %al, -0x7b(%rbp)
movl -0x6c(%rbp), %eax
andl $0xff0000, %eax         # imm = 0xFF0000
shrl $0x10, %eax
movb %al, -0x7a(%rbp)
movl -0x6c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
shrl $0x8, %eax
movb %al, -0x79(%rbp)
movl -0x6c(%rbp), %eax
andl $0x7f, %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
jmp 0xb29b4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165210
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
shrl $0xc, %eax
movb %al, -0x7b(%rbp)
movl -0x6c(%rbp), %eax
andl $0xf00, %eax            # imm = 0xF00
shrl $0x8, %eax
movb %al, -0x7a(%rbp)
movl -0x6c(%rbp), %eax
andl $0xf0, %eax
shrl $0x4, %eax
movb %al, -0x79(%rbp)
movq $0x0, -0x68(%rbp)
cmpq $0x3, -0x68(%rbp)
jge 0xb29a8
movq -0x68(%rbp), %rax
movzbl -0x7b(%rbp,%rax), %ecx
imull $0x11, %ecx, %ecx
movb %cl, -0x7b(%rbp,%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xb2983
movl -0x6c(%rbp), %eax
andl $0x7, %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0xb29f9
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x7c(%rbp)
movzbl -0x7c(%rbp), %eax
cmpl $0x0, %eax
je 0xb29e2
movzbl -0x7c(%rbp), %eax
movq %rax, -0x78(%rbp)
jmp 0xb29f7
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165210
movq %rax, -0x78(%rbp)
jmp 0xb29f9
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0xb2a2b
movq -0x30(%rbp), %rdi
leaq -0x7b(%rbp), %rsi
movl $0x3, %edx
callq 0x166170
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xb2a01
movq -0x78(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0xb28d0
cmpl $0x0, -0x34(%rbp)
jne 0xb2a54
movq -0x8(%rbp), %rdi
leaq 0x101e3f(%rip), %rsi    # 0x1b488c
movb $0x0, %al
callq 0x15bfb0
movl -0x34(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq