// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/jbf.c
// Function: read_bitmap_v1
static int read_bitmap_v1(deark *c, lctx *d, struct page_ctx *pg, i64 pos1, i64 *bytes_consumed)
{
	struct de_bmpinfo bi;
	int retval = 0;
	dbuf *outf = NULL;
	i64 pos = pos1;
	i64 k;
	i64 count;
	i64 dec_bytes = 0;

	de_dbg(c, "bitmap at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	if(!fmtutil_get_bmpinfo(c, c->infile, &bi, pos, c->infile->len-pos, 0)) {
		de_err(c, "Invalid bitmap");
		goto done;
	}

	if(bi.infohdrsize != 40) {
		de_err(c, "Unexpected BMP format");
		goto done;
	}

	outf = dbuf_create_output_file(c, pg->thumbnail_ext, pg->fi, 0);
	dbuf_enable_wbuffer(outf);

	// Manufacture a BMP fileheader
	fmtutil_generate_bmpfileheader(c, outf, &bi, 0);

	// Copy the BITMAPINFOHEADER
	dbuf_copy(c->infile, pos, bi.infohdrsize, outf);

	// Write the standard palette
	for(k=0; k<256; k++) {
		dbuf_writebyte(outf, v1pal[k*3+2]);
		dbuf_writebyte(outf, v1pal[k*3+1]);
		dbuf_writebyte(outf, v1pal[k*3]);
		dbuf_writebyte(outf, 0);
	}

	pos += bi.infohdrsize;

	// Decompress the image
	while(1) {
		u8 b0, b1;

		// Stop if we reach the end of the input file.
		if(pos >= c->infile->len) break;

		// Stop if we decompressed the expected number of bytes
		if(dec_bytes >= bi.foreground_size) break;

		b0 = de_getbyte_p(&pos);

		if(d->ver_minor>=3) {
			if(b0>0x80) { // a compressed run
				count = (i64)(b0-0x80);
				b1 = de_getbyte_p(&pos);
				dbuf_write_run(outf, b1, count);
				dec_bytes += count;
			}
			else { // uncompressed run
				count = (i64)b0;
				dbuf_copy(c->infile, pos, count, outf);
				pos += count;
				dec_bytes += count;
			}
		}
		else {
			if(b0>0xc0) { // a compressed run
				count = (i64)(b0-0xc0);
				b1 = de_getbyte_p(&pos);
				dbuf_write_run(outf, b1, count);
				dec_bytes += count;
			}
			else { // literal byte
				count = 1;
				dbuf_writebyte(outf, b0);
				dec_bytes += count;
			}
		}
	}

	retval = 1;
done:
	dbuf_close(outf);
	*bytes_consumed = pos - pos1;
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0xcc(%rbp)
movq $0x0, -0xd8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0x0, -0xf8(%rbp)
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rdx
leaq 0x10783b(%rip), %rsi    # 0x1c1f07
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0xe0(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %r8
subq -0xe0(%rbp), %r8
leaq -0xc8(%rbp), %rdx
xorl %r9d, %r9d
callq 0x172d70
cmpl $0x0, %eax
jne 0xba732
movq -0x8(%rbp), %rdi
leaq 0xed3d6(%rip), %rsi     # 0x1a7afc
movb $0x0, %al
callq 0x15bfb0
jmp 0xbaa49
cmpq $0x28, -0xb0(%rbp)
je 0xba753
movq -0x8(%rbp), %rdi
leaq 0xfba2b(%rip), %rsi     # 0x1b6172
movb $0x0, %al
callq 0x15bfb0
jmp 0xbaa49
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1734e0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0xe0(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0xd8(%rbp), %rcx
callq 0x166070
movq $0x0, -0xe8(%rbp)
cmpq $0x100, -0xe8(%rbp) # imm = 0x100
jge 0xba85d
movq -0xd8(%rbp), %rdi
imulq $0x3, -0xe8(%rbp), %rcx
leaq 0xfb4e1(%rip), %rax     # 0x1b5cd0
movzbl 0x2(%rax,%rcx), %esi
callq 0x168380
movq -0xd8(%rbp), %rdi
imulq $0x3, -0xe8(%rbp), %rcx
leaq 0xfb4c1(%rip), %rax     # 0x1b5cd0
movzbl 0x1(%rax,%rcx), %esi
callq 0x168380
movq -0xd8(%rbp), %rdi
imulq $0x3, -0xe8(%rbp), %rcx
leaq 0xfb4a1(%rip), %rax     # 0x1b5cd0
movzbl (%rax,%rcx), %esi
callq 0x168380
movq -0xd8(%rbp), %rdi
xorl %esi, %esi
callq 0x168380
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0xba7c8
movq -0xb0(%rbp), %rax
addq -0xe0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xba88c
jmp 0xbaa3f
movq -0xf8(%rbp), %rax
cmpq -0x60(%rbp), %rax
jl 0xba89e
jmp 0xbaa3f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x165170
movb %al, -0xf9(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, 0x8(%rax)
jb 0xba995
movzbl -0xf9(%rbp), %eax
cmpl $0x80, %eax
jle 0xba936
movzbl -0xf9(%rbp), %eax
subl $0x80, %eax
cltq
movq %rax, -0xf0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x165170
movb %al, -0xfa(%rbp)
movq -0xd8(%rbp), %rdi
movb -0xfa(%rbp), %al
movq -0xf0(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
movq -0xf0(%rbp), %rax
addq -0xf8(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0xba990
movzbl -0xf9(%rbp), %eax
movq %rax, -0xf0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0xe0(%rbp), %rsi
movq -0xf0(%rbp), %rdx
movq -0xd8(%rbp), %rcx
callq 0x166070
movq -0xf0(%rbp), %rax
addq -0xe0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xf0(%rbp), %rax
addq -0xf8(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0xbaa3a
movzbl -0xf9(%rbp), %eax
cmpl $0xc0, %eax
jle 0xbaa05
movzbl -0xf9(%rbp), %eax
subl $0xc0, %eax
cltq
movq %rax, -0xf0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x165170
movb %al, -0xfa(%rbp)
movq -0xd8(%rbp), %rdi
movb -0xfa(%rbp), %al
movq -0xf0(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
movq -0xf0(%rbp), %rax
addq -0xf8(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0xbaa38
movq $0x1, -0xf0(%rbp)
movq -0xd8(%rbp), %rdi
movzbl -0xf9(%rbp), %esi
callq 0x168380
movq -0xf0(%rbp), %rax
addq -0xf8(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0xbaa3a
jmp 0xba872
movl $0x1, -0xcc(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x1676a0
movq -0xe0(%rbp), %rcx
subq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0xcc(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop