// File: /workspace/github_c_makefile_star2_O1/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 0x83b4d
movq %r8, %r12
movq %rdi, %rbx
movq %rcx, %r15
movzbl (%rdx), %r14d
movq 0x18(%rsp), %r13
movq 0xa8(%r13), %rax
testq %rax, %rax
je 0x83879
cmpq $0x0, (%rax)
je 0x83869
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x8(%rax), %rsi
cmpq $0x0, 0x10(%rsi,%r13)
je 0x83851
addq %r13, %rsi
movq %rbx, %rdi
callq 0x846e8
incq %rbp
movq 0x18(%rsp), %rax
movq 0xa8(%rax), %rax
addq $0x28, %r13
cmpq (%rax), %rbp
jb 0x8383a
movq %rbx, %rdi
movq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0x84628
leal 0x3f(%r14), %eax
cmpb $0xe, %al
ja 0x8389c
movl %r14d, %eax
andl $0x3, %eax
je 0x8389c
cmpl $0x3, %eax
jne 0x838ef
movb $0x1, 0x14(%r13)
leaq 0xa7e05(%rip), %rdx     # 0x12b69f
jmp 0x838f6
cmpl $0xf7, %r14d
je 0x838dc
cmpl $0xc0, %r14d
jne 0x83960
movb $0x1, 0x12(%r13)
leaq 0xa7bdf(%rip), %rsi     # 0x12b49d
leaq 0xa7ba4(%rip), %rdx     # 0x12b469
leaq 0xbb44d(%rip), %rcx     # 0x13ed19
leaq 0xa7b9a(%rip), %r8      # 0x12b46d
leaq 0xa7b9e(%rip), %r9      # 0x12b478
jmp 0x83956
leaq 0xa7be0(%rip), %rsi     # 0x12b4c3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
jmp 0x83960
leaq 0xa7b73(%rip), %rdx     # 0x12b469
movl %r14d, %ecx
andl $0xf, %ecx
cmpl $0x9, %ecx
jb 0x8390f
movb $0x1, 0x15(%r13)
leaq 0xa7da9(%rip), %rcx     # 0x12b6b6
jmp 0x83916
leaq 0xbb403(%rip), %rcx     # 0x13ed19
cmpl $0x2, %eax
jne 0x83929
movb $0x1, 0x13(%r13)
leaq 0xa7d82(%rip), %r8      # 0x12b6a9
jmp 0x83930
leaq 0xa7b3d(%rip), %r8      # 0x12b46d
andl $0x7, %r14d
cmpl $0x5, %r14d
jb 0x83948
movb $0x1, 0x16(%r13)
leaq 0xa7d7c(%rip), %r9      # 0x12b6c2
jmp 0x8394f
leaq 0xa7b29(%rip), %r9      # 0x12b478
leaq 0xa7b2c(%rip), %rsi     # 0x12b482
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpb $0x0, (%r13)
je 0x83970
leaq 0xa7b61(%rip), %rsi     # 0x12b4cf
jmp 0x839e1
cmpb $0x0, 0x19(%r13)
je 0x83980
leaq 0xa7ba8(%rip), %rsi     # 0x12b526
jmp 0x839e1
cmpb $0x0, 0x1a(%r13)
je 0x83990
leaq 0xa7ba1(%rip), %rsi     # 0x12b52f
jmp 0x839e1
cmpb $0x0, 0x17(%r13)
je 0x839a0
leaq 0xa7b9a(%rip), %rsi     # 0x12b538
jmp 0x839e1
cmpb $0x0, 0x14(%r13)
je 0x839b0
leaq 0xa7b93(%rip), %rsi     # 0x12b541
jmp 0x839e1
cmpb $0x0, 0x2(%r13)
movb 0x5(%r13), %al
je 0x839cd
testb %al, %al
leaq 0xa7b9a(%rip), %rax     # 0x12b55e
leaq 0xa7b84(%rip), %rsi     # 0x12b54f
jmp 0x839dd
testb %al, %al
leaq 0xa7b43(%rip), %rax     # 0x12b519
leaq 0xa7b8b(%rip), %rsi     # 0x12b568
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0xeb6d6
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef0d5
movb %al, 0x1c(%r13)
movzbl %al, %edx
leaq 0xa7ad4(%rip), %rsi     # 0x12b4d7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq 0x1(%r15), %rsi
callq 0xef169
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq 0x3(%r15), %rsi
callq 0xef169
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xe9b05
movq 0x18(%rbx), %rdi
leaq 0x5(%r15), %rsi
callq 0xef0d5
movzbl %al, %edx
movq %rdx, 0x30(%r13)
leaq 0x925ca(%rip), %rsi     # 0x11601d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x30(%r13), %rax
leaq (%rax,%rax,2), %rax
addq $0x6, %rax
cmpq %r12, %rax
jg 0x83b4d
movq 0x18(%rsp), %rax
cmpq $0x0, 0x30(%rax)
jle 0x83b4d
movq %r15, %r14
addq $0x8, %r14
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
leaq -0x2(%r14), %rsi
callq 0xef0d5
movb %al, 0x17(%rsp)
movq %rbx, %r15
movq 0x18(%rbx), %rdi
movq %r14, 0x20(%rsp)
leaq -0x1(%r14), %rsi
callq 0xef0d5
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 0x83adf
movq 0x18(%rsp), %rax
movl $0x1, 0x98(%rax)
movq 0x18(%rsp), %rbx
movq 0xa0(%rbx), %rdi
movl $0x5, %esi
leaq 0xa79ee(%rip), %rdx     # 0x12b4e5
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0xf6ab0
movq 0x18(%r15), %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0xef0d5
movzbl 0x17(%rsp), %ecx
movzbl %al, %eax
movl %eax, (%rsp)
movq %r15, %rdi
leaq 0xa79c0(%rip), %rsi     # 0x12b4ea
movl %ebp, %edx
movl %r13d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0xe9226
incq %rbp
addq $0x3, %r14
cmpq 0x30(%rbx), %rbp
movq %r15, %rbx
jl 0x83a8b
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq