// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/jpeg.c
// Function: handler_sof
static void handler_sof(deark *c, lctx *d,
	const struct marker_info *mi, i64 pos, i64 data_size)
{
	i64 w, h;
	u8 b;
	i64 i;
	const char *attr_lossy = "DCT";
	const char *attr_cmpr = "huffman";
	const char *attr_progr = "non-progr.";
	const char *attr_hier = "non-hier.";
	u8 seg_type = mi->seg_type;

	if(data_size<6) return;

	if(d->fpxr_data) {
		finalize_all_fpxr_streams(c, d);
	}

	if(seg_type>=0xc1 && seg_type<=0xcf && (seg_type%4)!=0) {
		if((seg_type%4)==3) { d->is_lossless=1; attr_lossy="lossless"; }
		if(seg_type%16>=9) { d->is_arithmetic=1; attr_cmpr="arithmetic"; }
		if((seg_type%4)==2) { d->is_progressive=1; attr_progr="progressive"; }
		if((seg_type%8)>=5) { d->is_hierarchical=1; attr_hier="hierarchical"; }
		de_dbg(c, "image type: %s, %s, %s, %s",
			attr_lossy, attr_cmpr, attr_progr, attr_hier);
	}
	else if(seg_type==0xc0) {
		d->is_baseline = 1;
		de_dbg(c, "image type: baseline (%s, %s, %s, %s)",
			attr_lossy, attr_cmpr, attr_progr, attr_hier);
	}
	else if(seg_type==0xf7) {
		de_dbg(c, "image type: JPEG-LS");
	}

	// By now we have hopefully collected the info we need to decide what JPEG
	// format we're dealing with.
	declare_jpeg_fmt(c, d, seg_type);

	d->precision = de_getbyte(pos);
	de_dbg(c, "precision: %d", (int)d->precision);
	h = de_getu16be(pos+1);
	w = de_getu16be(pos+3);
	de_dbg_dimensions(c, w, h);
	d->ncomp = (i64)de_getbyte(pos+5);
	de_dbg(c, "number of components: %d", (int)d->ncomp);

	// per-component data
	if(data_size<6+3*d->ncomp) goto done;
	for(i=0; i<d->ncomp; i++) {
		u8 comp_id;
		i64 sf1, sf2;
		u8 qtid;
		comp_id = de_getbyte(pos+6+3*i+0);
		b = de_getbyte(pos+6+3*i+1);
		sf1 = (i64)(b>>4);
		sf2 = (i64)(b&0x0f);
		if(sf1!=1 || sf2!=1) d->is_subsampled = 1;
		ucstring_printf(d->sampling_code, DE_ENCODING_LATIN1, "%d%d", (int)sf1, (int)sf2);
		qtid = de_getbyte(pos+6+3*i+2);
		de_dbg(c, "cmp #%d: id=%d sampling=%d"DE_CHAR_TIMES"%d quant_table=Q%d",
			(int)i, (int)comp_id, (int)sf1, (int)sf2, (int)qtid);
	}

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
cmpq $0x6, %r8
jl 0x7e6c5
movq %r8, %r12
movq %rcx, %r15
movzbl (%rdx), %r14d
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rax
testq %rax, %rax
je 0x7e4ae
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpq (%rax), %rbp
jae 0x7e49f
movq 0x8(%rax), %rsi
cmpq $0x0, 0x10(%rsi,%r13)
je 0x7e496
addq %r13, %rsi
movq 0x20(%rsp), %rdi
callq 0x7f2d8
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rax
incq %rbp
addq $0x28, %r13
jmp 0x7e46c
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x7f236
leal 0x3f(%r14), %eax
cmpb $0xe, %al
ja 0x7e4d5
movl %r14d, %eax
andl $0x3, %eax
je 0x7e4d5
cmpl $0x3, %eax
jne 0x7e531
movq 0x18(%rsp), %rcx
movb $0x1, 0x14(%rcx)
leaq 0xa022c(%rip), %rdx     # 0x11e6ff
jmp 0x7e538
cmpl $0xf7, %r14d
je 0x7e51c
cmpl $0xc0, %r14d
jne 0x7e5b0
movq 0x18(%rsp), %rax
movb $0x1, 0x12(%rax)
leaq 0xa0002(%rip), %rsi     # 0x11e4fd
leaq 0x9ffc7(%rip), %rdx     # 0x11e4c9
leaq 0xb38e0(%rip), %rcx     # 0x131de9
leaq 0x9ffbd(%rip), %r8      # 0x11e4cd
leaq 0x9ffc1(%rip), %r9      # 0x11e4d8
jmp 0x7e5a4
leaq 0xa0000(%rip), %rsi     # 0x11e523
movq 0x20(%rsp), %rdi
xorl %eax, %eax
callq 0xde896
jmp 0x7e5b0
leaq 0x9ff91(%rip), %rdx     # 0x11e4c9
movl %r14d, %ecx
andl $0xf, %ecx
cmpl $0x9, %ecx
jb 0x7e555
movq 0x18(%rsp), %rcx
movb $0x1, 0x15(%rcx)
leaq 0xa01c3(%rip), %rcx     # 0x11e716
jmp 0x7e55c
leaq 0xb388d(%rip), %rcx     # 0x131de9
cmpl $0x2, %eax
jne 0x7e573
movq 0x18(%rsp), %rax
movb $0x1, 0x13(%rax)
leaq 0xa0198(%rip), %r8      # 0x11e709
jmp 0x7e57f
leaq 0x9ff53(%rip), %r8      # 0x11e4cd
movq 0x18(%rsp), %rax
andl $0x7, %r14d
cmpl $0x5, %r14d
jb 0x7e596
movb $0x1, 0x16(%rax)
leaq 0xa018e(%rip), %r9      # 0x11e722
jmp 0x7e59d
leaq 0x9ff3b(%rip), %r9      # 0x11e4d8
leaq 0x9ff3e(%rip), %rsi     # 0x11e4e2
movq 0x20(%rsp), %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
je 0x7e5c3
leaq 0x9ff6e(%rip), %rsi     # 0x11e52f
jmp 0x7e62e
cmpb $0x0, 0x19(%rax)
je 0x7e5d2
leaq 0x9ffb6(%rip), %rsi     # 0x11e586
jmp 0x7e62e
cmpb $0x0, 0x1a(%rax)
je 0x7e5e1
leaq 0x9ffb0(%rip), %rsi     # 0x11e58f
jmp 0x7e62e
cmpb $0x0, 0x17(%rax)
je 0x7e5f0
leaq 0x9ffaa(%rip), %rsi     # 0x11e598
jmp 0x7e62e
cmpb $0x0, 0x14(%rax)
je 0x7e5ff
leaq 0x9ffa4(%rip), %rsi     # 0x11e5a1
jmp 0x7e62e
cmpb $0x0, 0x2(%rax)
movb 0x5(%rax), %al
je 0x7e61a
testb %al, %al
leaq 0x9ffad(%rip), %rax     # 0x11e5be
leaq 0x9ff97(%rip), %rsi     # 0x11e5af
jmp 0x7e62a
testb %al, %al
leaq 0x9ff56(%rip), %rax     # 0x11e579
leaq 0x9ff9e(%rip), %rsi     # 0x11e5c8
cmoveq %rax, %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0xe07f7
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3eb4
movq 0x18(%rsp), %r13
movb %al, 0x1c(%r13)
movzbl %al, %edx
leaq 0x9fedd(%rip), %rsi     # 0x11e537
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x1(%r15), %rsi
callq 0xe3f46
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq 0x3(%r15), %rsi
callq 0xe3f46
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xdf038
movq 0x18(%rbx), %rdi
leaq 0x5(%r15), %rsi
callq 0xe3eb4
movzbl %al, %edx
movq %rdx, 0x30(%r13)
leaq 0x8a993(%rip), %rsi     # 0x10903d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x30(%r13), %rax
leaq (%rax,%rax,2), %rcx
addq $0x6, %rcx
cmpq %r12, %rcx
jle 0x7e6d4
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x8, %r15
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
cmpq %rax, %r12
jge 0x7e6c5
movq 0x20(%rsp), %rbx
movq 0x18(%rbx), %rdi
leaq -0x2(%r15), %rsi
callq 0xe3eb4
movb %al, 0x17(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rbx
leaq -0x1(%r15), %rsi
callq 0xe3eb4
movzbl %al, %ebp
movl %ebp, %r13d
shrl $0x4, %r13d
andl $0xf, %ebp
movl %r13d, %eax
xorl $0x1, %eax
movl %ebp, %ecx
xorl $0x1, %ecx
orl %eax, %ecx
movq 0x18(%rsp), %r15
je 0x7e737
movl $0x1, 0x98(%r15)
movq 0xa0(%r15), %rdi
pushq $0x5
popq %rsi
leaq 0x9fdfd(%rip), %rdx     # 0x11e545
movl %r13d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0xeae8b
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xe3eb4
movzbl 0x17(%rsp), %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movq %r14, %rdi
leaq 0x9fdd4(%rip), %rsi     # 0x11e54a
movl %r12d, %edx
movl %r13d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0xde896
incq %r12
movq 0x30(%r15), %rax
movq %rbx, %r15
addq $0x3, %r15
jmp 0x7e6e0