// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/j2c.c
// Function: do_j2c_page
static int do_j2c_page(deark *c, lctx *d, i64 pos1, i64 *bytes_consumed)
{
	u8 b;
	i64 pos = pos1;
	i64 seg_size;
	u8 seg_type;
	int found_marker;
	struct marker_info mi;
	i64 scan_byte_count;
	int retval = 0;
	struct page_ctx *pg = NULL;

	pg = de_malloc(c, sizeof(struct page_ctx));

	found_marker = 0;
	while(1) {
		if(pos>=c->infile->len)
			break;
		b = de_getbyte_p(&pos);
		if(b==0xff) {
			found_marker = 1;
			continue;
		}

		if(!found_marker) {
			// Not an 0xff byte, and not preceded by an 0xff byte. Just ignore it.
			continue;
		}

		found_marker = 0; // Reset this flag.

		if(b==0x00) {
			continue; // Escaped 0xff
		}

		seg_type = b;

		get_marker_info(c, d, pg, seg_type, &mi);

		if(mi.flags & FLAG_NO_DATA) {
			de_dbg(c, "marker 0x%02x (%s) at %d", (unsigned int)seg_type,
				mi.longname, (int)(pos-2));

			if(seg_type==0xd9) { // EOC
				retval = 1;
				goto done;
			}

			if(seg_type==0x93) {
				// SOD (JPEG 2000 marker sort of like SOS)
				if(!do_read_scan_data(c, d, pg, pos, &scan_byte_count)) {
					break;
				}
				pos += scan_byte_count;
			}

			continue;
		}

		// If we get here, we're reading a segment that has a size field.
		seg_size = de_getu16be(pos);
		if(pos<2) break; // bogus size

		do_segment(c, d, pg, &mi, pos+2, seg_size-2);

		pos += seg_size;
	}

done:
	if(pg) {
		de_free(c, pg);
	}

	*bytes_consumed = pos - pos1;
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0xbc(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
movl $0x18, %esi
callq 0x15b780
movq %rax, -0xc8(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xc76ab
jmp 0xc780b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x21(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0xff, %eax
jne 0xc76d3
movl $0x1, -0x40(%rbp)
jmp 0xc7694
cmpl $0x0, -0x40(%rbp)
jne 0xc76db
jmp 0xc7694
movl $0x0, -0x40(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x0, %eax
jne 0xc76ed
jmp 0xc7694
movb -0x21(%rbp), %al
movb %al, -0x39(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0xc8(%rbp), %rdx
leaq -0xb0(%rbp), %r8
movzbl -0x39(%rbp), %ecx
callq 0xc7850
movl -0xac(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0xc77b1
movq -0x8(%rbp), %rdi
movzbl -0x39(%rbp), %edx
leaq -0xb0(%rbp), %rcx
addq $0x14, %rcx
movq -0x30(%rbp), %rax
subq $0x2, %rax
movl %eax, %r8d
leaq 0xeedc1(%rip), %rsi     # 0x1b650c
movb $0x0, %al
callq 0x15b0d0
movzbl -0x39(%rbp), %eax
cmpl $0xd9, %eax
jne 0xc776c
movl $0x1, -0xbc(%rbp)
jmp 0xc780d
movzbl -0x39(%rbp), %eax
cmpl $0x93, %eax
jne 0xc77ac
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0xb8(%rbp), %r8
callq 0xc79e0
cmpl $0x0, %eax
jne 0xc779d
jmp 0xc780b
movq -0xb8(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xc7694
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x38(%rbp)
cmpq $0x2, -0x30(%rbp)
jge 0xc77cf
jmp 0xc780b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movq -0x30(%rbp), %r8
addq $0x2, %r8
movq -0x38(%rbp), %r9
subq $0x2, %r9
leaq -0xb0(%rbp), %rcx
callq 0xc7ba0
movq -0x38(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xc7694
jmp 0xc780d
cmpq $0x0, -0xc8(%rbp)
je 0xc7827
movq -0x8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x15b820
movq -0x30(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl -0xbc(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)