// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/jpeg.c
// Function: detect_jpeg_len
static int detect_jpeg_len(deark *c, scanctx *d, i64 pos1, i64 len)
{
	u8 b0, b1;
	i64 pos;
	i64 seg_size;
	int in_scan = 0;
	int found_sof = 0;
	int found_scan = 0;

	d->len = 0;
	d->is_jpegls = 0;
	pos = pos1;

	while(1) {
		if(pos>=pos1+len)
			break;
		b0 = de_getbyte(pos);

		if(b0!=0xff) {
			pos++;
			continue;
		}

		// Peek at the next byte (after this 0xff byte).
		b1 = de_getbyte(pos+1);

		if(b1==0xff) {
			// A "fill byte", not a marker.
			pos++;
			continue;
		}
		else if(b1==0x00 || (d->is_jpegls && b1<0x80 && in_scan)) {
			// An escape sequence, not a marker.
			pos+=2;
			continue;
		}
		else if(b1==0xd9) { // EOI. That's what we're looking for.
			if(!found_sof || !found_scan) return 0;
			pos+=2;
			d->len = pos-pos1;
			return 1;
		}
		else if(b1==0xf7) {
			de_dbg(c, "Looks like a JPEG-LS file.");
			found_sof = 1;
			d->is_jpegls = 1;
		}
		else if(b1>=0xc0 && b1<=0xcf && b1!=0xc4 && b1!=0xc8 && b1!=0xcc) {
			found_sof = 1;
		}

		if(b1==0xda) { // SOS - Start of scan
			if(!found_sof) return 0;
			found_scan = 1;
			in_scan = 1;
		}
		else if(b1>=0xd0 && b1<=0xd7) {
			// RSTn markers don't change the in_scan state.
			;
		}
		else {
			in_scan = 0;
		}

		if((b1>=0xd0 && b1<=0xda) || b1==0x01) {
			// Markers that have no content.
			pos+=2;
			continue;
		}

		// Everything else should be a marker segment, with a length field.
		seg_size = de_getu16be(pos+2);
		if(seg_size<2) break; // bogus size

		pos += seg_size+2;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movb $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
addq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jl 0xc0f7e
jmp 0xc1187
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165060
movb %al, -0x29(%rbp)
movzbl -0x29(%rbp), %eax
cmpl $0xff, %eax
je 0xc0fab
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xc0f68
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movb %al, -0x2a(%rbp)
movzbl -0x2a(%rbp), %eax
cmpl $0xff, %eax
jne 0xc0fdc
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xc0f68
movzbl -0x2a(%rbp), %eax
cmpl $0x0, %eax
je 0xc1003
movq -0x18(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
je 0xc1014
movzbl -0x2a(%rbp), %eax
cmpl $0x80, %eax
jge 0xc1014
cmpl $0x0, -0x44(%rbp)
je 0xc1014
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
jmp 0xc0f68
movzbl -0x2a(%rbp), %eax
cmpl $0xd9, %eax
jne 0xc105e
cmpl $0x0, -0x48(%rbp)
je 0xc102b
cmpl $0x0, -0x4c(%rbp)
jne 0xc1037
movl $0x0, -0x4(%rbp)
jmp 0xc118e
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rcx
subq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0xc118e
movzbl -0x2a(%rbp), %eax
cmpl $0xf7, %eax
jne 0xc108c
movq -0x10(%rbp), %rdi
leaq 0xf6462(%rip), %rsi     # 0x1b74d6
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x48(%rbp)
movq -0x18(%rbp), %rax
movb $0x1, 0x8(%rax)
jmp 0xc10cc
movzbl -0x2a(%rbp), %eax
cmpl $0xc0, %eax
jl 0xc10ca
movzbl -0x2a(%rbp), %eax
cmpl $0xcf, %eax
jg 0xc10ca
movzbl -0x2a(%rbp), %eax
cmpl $0xc4, %eax
je 0xc10ca
movzbl -0x2a(%rbp), %eax
cmpl $0xc8, %eax
je 0xc10ca
movzbl -0x2a(%rbp), %eax
cmpl $0xcc, %eax
je 0xc10ca
movl $0x1, -0x48(%rbp)
jmp 0xc10cc
jmp 0xc10ce
jmp 0xc10d0
jmp 0xc10d2
movzbl -0x2a(%rbp), %eax
cmpl $0xda, %eax
jne 0xc10ff
cmpl $0x0, -0x48(%rbp)
jne 0xc10ef
movl $0x0, -0x4(%rbp)
jmp 0xc118e
movl $0x1, -0x4c(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0xc1120
movzbl -0x2a(%rbp), %eax
cmpl $0xd0, %eax
jl 0xc1117
movzbl -0x2a(%rbp), %eax
cmpl $0xd7, %eax
jg 0xc1117
jmp 0xc111e
movl $0x0, -0x44(%rbp)
jmp 0xc1120
movzbl -0x2a(%rbp), %eax
cmpl $0xd0, %eax
jl 0xc1136
movzbl -0x2a(%rbp), %eax
cmpl $0xda, %eax
jle 0xc113f
movzbl -0x2a(%rbp), %eax
cmpl $0x1, %eax
jne 0xc1150
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
jmp 0xc0f68
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq $0x2, %rsi
callq 0x1651d0
movq %rax, -0x40(%rbp)
cmpq $0x2, -0x40(%rbp)
jge 0xc1172
jmp 0xc1187
movq -0x40(%rbp), %rax
addq $0x2, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xc0f68
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)