// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/jpeg.c
// Function: do_jpeg_stream
static int do_jpeg_stream(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 sof_count = 0;
	int retval = 0;

	d->sampling_code = ucstring_create(c);

	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;

		if(seg_type==0xf7 && !d->found_sof) {
			d->is_jpegls = 1;
		}

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

		if(mi.flags & FLAG_IS_SOF) {
			d->found_sof = 1;
		}

		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) { // EOI
				retval = 1;
				goto done;
			}

			if(seg_type==0xd8 && !d->found_soi) {
				d->found_soi = 1;
			}

			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, &mi, pos+2, seg_size-2);

		pos += seg_size;

		if(mi.flags & FLAG_IS_SOF) {
			if(sof_count==0) {
				do_post_sof_stuff(c, d);
			}
			sof_count++;
		}

		if(seg_type==0xda) {
			// If we read an SOS segment, now read the untagged image data that
			// should follow it.
			if(!do_read_scan_data(c, d, pos, &scan_byte_count)) {
				break;
			}
			pos += scan_byte_count;
		}
	}

done:
	do_at_end_of_jpeg(c, d);
	*bytes_consumed = pos - pos1;
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %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)
movl $0x0, -0xc0(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movl $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xbbd6c
jmp 0xbbf3c
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 0xbbd94
movl $0x1, -0x40(%rbp)
jmp 0xbbd55
cmpl $0x0, -0x40(%rbp)
jne 0xbbd9c
jmp 0xbbd55
movl $0x0, -0x40(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x0, %eax
jne 0xbbdae
jmp 0xbbd55
movb -0x21(%rbp), %al
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
cmpl $0xf7, %eax
jne 0xbbdd0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x29(%rax)
jne 0xbbdd0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0xb0(%rbp), %rcx
movzbl -0x39(%rbp), %edx
callq 0xbc4f0
movl -0xac(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xbbe00
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
movl -0xac(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0xbbe78
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 0xfa6d7(%rip), %rsi     # 0x1b650c
movb $0x0, %al
callq 0x15b0d0
movzbl -0x39(%rbp), %eax
cmpl $0xd9, %eax
jne 0xbbe56
movl $0x1, -0xc0(%rbp)
jmp 0xbbf3e
movzbl -0x39(%rbp), %eax
cmpl $0xd8, %eax
jne 0xbbe73
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
jne 0xbbe73
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
jmp 0xbbd55
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x38(%rbp)
cmpq $0x2, -0x30(%rbp)
jge 0xbbe99
jmp 0xbbf3c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rcx
addq $0x2, %rcx
movq -0x38(%rbp), %r8
subq $0x2, %r8
leaq -0xb0(%rbp), %rdx
callq 0xbc840
movq -0x38(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0xac(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xbbefe
cmpl $0x0, -0xbc(%rbp)
jne 0xbbeef
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xbc8f0
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
movzbl -0x39(%rbp), %eax
cmpl $0xda, %eax
jne 0xbbf37
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
callq 0xbca00
cmpl $0x0, %eax
jne 0xbbf28
jmp 0xbbf3c
movq -0xb8(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xbbd55
jmp 0xbbf3e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xbcbc0
movq -0x30(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl -0xc0(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)