// File: /workspace/github_c_makefile_star2_O3/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)
cmpq $0x6, %r8
jl 0x834a5
movq %r8, %r12
movq %rdi, %rbx
movq %rcx, %r15
movzbl (%rdx), %r14d
movq 0x18(%rsp), %r13
movq 0xa8(%r13), %rax
testq %rax, %rax
je 0x831d6
movq (%rax), %rcx
testq %rcx, %rcx
je 0x831c6
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x8(%rax), %rsi
cmpq $0x0, 0x10(%rsi,%r13)
je 0x831ba
addq %r13, %rsi
movq %rbx, %rdi
callq 0x84029
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rax
movq (%rax), %rcx
incq %rbp
addq $0x28, %r13
cmpq %rcx, %rbp
jb 0x83194
movq %rbx, %rdi
movq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0x83f7a
leal 0x3f(%r14), %eax
cmpb $0xe, %al
ja 0x831f9
movl %r14d, %eax
andl $0x3, %eax
je 0x831f9
cmpl $0x3, %eax
jne 0x8324c
movb $0x1, 0x14(%r13)
leaq 0xa8778(%rip), %rdx     # 0x12b96f
jmp 0x83253
cmpl $0xf7, %r14d
je 0x83239
cmpl $0xc0, %r14d
jne 0x832bd
movb $0x1, 0x12(%r13)
leaq 0xa8552(%rip), %rsi     # 0x12b76d
leaq 0xa8517(%rip), %rdx     # 0x12b739
leaq 0xbbef0(%rip), %rcx     # 0x13f119
leaq 0xa850d(%rip), %r8      # 0x12b73d
leaq 0xa8511(%rip), %r9      # 0x12b748
jmp 0x832b3
leaq 0xa8553(%rip), %rsi     # 0x12b793
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0x832bd
leaq 0xa84e6(%rip), %rdx     # 0x12b739
movl %r14d, %ecx
andl $0xf, %ecx
cmpl $0x9, %ecx
jb 0x8326c
movb $0x1, 0x15(%r13)
leaq 0xa871c(%rip), %rcx     # 0x12b986
jmp 0x83273
leaq 0xbbea6(%rip), %rcx     # 0x13f119
cmpl $0x2, %eax
jne 0x83286
movb $0x1, 0x13(%r13)
leaq 0xa86f5(%rip), %r8      # 0x12b979
jmp 0x8328d
leaq 0xa84b0(%rip), %r8      # 0x12b73d
andl $0x7, %r14d
cmpl $0x5, %r14d
jb 0x832a5
movb $0x1, 0x16(%r13)
leaq 0xa86ef(%rip), %r9      # 0x12b992
jmp 0x832ac
leaq 0xa849c(%rip), %r9      # 0x12b748
leaq 0xa849f(%rip), %rsi     # 0x12b752
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpb $0x0, (%r13)
je 0x832cd
leaq 0xa84d4(%rip), %rsi     # 0x12b79f
jmp 0x8333e
cmpb $0x0, 0x19(%r13)
je 0x832dd
leaq 0xa851b(%rip), %rsi     # 0x12b7f6
jmp 0x8333e
cmpb $0x0, 0x1a(%r13)
je 0x832ed
leaq 0xa8514(%rip), %rsi     # 0x12b7ff
jmp 0x8333e
cmpb $0x0, 0x17(%r13)
je 0x832fd
leaq 0xa850d(%rip), %rsi     # 0x12b808
jmp 0x8333e
cmpb $0x0, 0x14(%r13)
je 0x8330d
leaq 0xa8506(%rip), %rsi     # 0x12b811
jmp 0x8333e
cmpb $0x0, 0x2(%r13)
movb 0x5(%r13), %al
je 0x8332a
testb %al, %al
leaq 0xa850d(%rip), %rax     # 0x12b82e
leaq 0xa84f7(%rip), %rsi     # 0x12b81f
jmp 0x8333a
testb %al, %al
leaq 0xa84b6(%rip), %rax     # 0x12b7e9
leaq 0xa84fe(%rip), %rsi     # 0x12b838
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0xeb125
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeead3
movb %al, 0x1c(%r13)
movzbl %al, %edx
leaq 0xa8447(%rip), %rsi     # 0x12b7a7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x1(%r15), %rsi
callq 0xeeb67
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq 0x3(%r15), %rsi
callq 0xeeb67
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xe8f88
movq 0x18(%rbx), %rdi
leaq 0x5(%r15), %rsi
callq 0xeead3
movzbl %al, %edx
movq %rdx, 0x30(%r13)
leaq 0x92d1d(%rip), %rsi     # 0x1160cd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x30(%r13), %rax
leaq (%rax,%rax,2), %rcx
addq $0x6, %rcx
cmpq %r12, %rcx
setg %cl
testq %rax, %rax
setle %al
orb %cl, %al
jne 0x834a5
movq %r15, %r14
addq $0x8, %r14
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
leaq -0x2(%r14), %rsi
callq 0xeead3
movb %al, 0x17(%rsp)
movq %rbx, %r15
movq 0x18(%rbx), %rdi
movq %r14, 0x20(%rsp)
leaq -0x1(%r14), %rsi
callq 0xeead3
movzbl %al, %r12d
movl %r12d, %r13d
shrl $0x4, %r13d
andl $0xf, %r12d
movl %r13d, %eax
xorl $0x1, %eax
movl %r12d, %ecx
xorl $0x1, %ecx
orl %eax, %ecx
je 0x83437
movq 0x18(%rsp), %rax
movl $0x1, 0x98(%rax)
movq 0x18(%rsp), %rbx
movq 0xa0(%rbx), %rdi
movl $0x5, %esi
leaq 0xa8366(%rip), %rdx     # 0x12b7b5
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xf6653
movq 0x18(%r15), %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0xeead3
movzbl 0x17(%rsp), %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movq %r15, %rdi
leaq 0xa8338(%rip), %rsi     # 0x12b7ba
movl %ebp, %edx
movl %r13d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0xe8617
incq %rbp
addq $0x3, %r14
cmpq 0x30(%rbx), %rbp
movq %r15, %rbx
jl 0x833e3
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq