// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pict.c
// Function: do_detect_version
static void do_detect_version(deark *c, struct detection_info *dti, int mode)
{
	static const u8 v1pattern[2] = { 0x11, 0x01 };
	static const u8 v2pattern[6] = { 0x00, 0x11, 0x02, 0xff, 0x0c, 0x00 };
	u8 buf[6];
	int v1_nohdr = 0;
	int v2_nohdr = 0;
	int v1_hdr = 0;
	int v2_hdr = 0;

	dti->file_version = 0;
	dti->has_fileheader = 0;

	de_read(buf, 522, sizeof(buf));
	if(!de_memcmp(buf, v2pattern, 6)) {
		v2_hdr = 1;
	}
	else if(!de_memcmp(buf, v1pattern, 2)) {
		v1_hdr = 1;
	}
	else {
		de_read(buf, 10, sizeof(buf));
		if(!de_memcmp(buf, v2pattern, 6)) {
			v2_nohdr = 1;
		}
		else if(!de_memcmp(buf, v1pattern, 2)) {
			v1_nohdr = 1;
		}
	}

	if(!v1_hdr && !v2_hdr && !v1_nohdr && !v2_nohdr) {
		return;
	}

	if(v2_hdr) {
		dti->file_version = 2;
		dti->has_fileheader = 1;
		return;
	}
	else if(v2_nohdr) {
		dti->file_version = 2;
		return;
	}

	if(mode==0) {
		// For v1, check that the file ends as expected
		de_read(buf, c->infile->len-2, 2);
		if(buf[1]==0xff) {
			; // v1 files should end with 0xff
		}
		else if(buf[0]==0xff && buf[1]==0x00) {
			; // But a few have an extra NUL byte at the end
		}
	}

	if(v1_hdr) {
		dti->file_version = 1;
		dti->has_fileheader = 1;
		return;
	}
	else if(v1_nohdr) {
		dti->file_version = 1;
		return;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x1a(%rbp), %rsi
movl $0x20a, %edx            # imm = 0x20A
movl $0x6, %ecx
callq 0x164d10
leaq -0x1a(%rbp), %rdi
leaq 0xbc260(%rip), %rsi     # 0x1c2b1a
movl $0x6, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x1068d5
movl $0x1, -0x2c(%rbp)
jmp 0x106955
leaq -0x1a(%rbp), %rax
movw (%rax), %ax
subw $0x111, %ax             # imm = 0x111
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1068f4
movl $0x1, -0x28(%rbp)
jmp 0x106953
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x1a(%rbp), %rsi
movl $0xa, %edx
movl $0x6, %ecx
callq 0x164d10
leaq -0x1a(%rbp), %rdi
leaq 0xbc200(%rip), %rsi     # 0x1c2b1a
movl $0x6, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x106932
movl $0x1, -0x24(%rbp)
jmp 0x106951
leaq -0x1a(%rbp), %rax
movw (%rax), %ax
subw $0x111, %ax             # imm = 0x111
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x10694f
movl $0x1, -0x20(%rbp)
jmp 0x106951
jmp 0x106953
jmp 0x106955
cmpl $0x0, -0x28(%rbp)
jne 0x106972
cmpl $0x0, -0x2c(%rbp)
jne 0x106972
cmpl $0x0, -0x20(%rbp)
jne 0x106972
cmpl $0x0, -0x24(%rbp)
jne 0x106972
jmp 0x106a2d
cmpl $0x0, -0x2c(%rbp)
je 0x106992
movq -0x10(%rbp), %rax
movl $0x2, (%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x106a2d
cmpl $0x0, -0x24(%rbp)
je 0x1069a7
movq -0x10(%rbp), %rax
movl $0x2, (%rax)
jmp 0x106a2d
jmp 0x1069a9
cmpl $0x0, -0x14(%rbp)
jne 0x1069fc
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x1a(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
subq $0x2, %rdx
movl $0x2, %ecx
callq 0x164d10
movzbl -0x19(%rbp), %eax
cmpl $0xff, %eax
jne 0x1069e2
jmp 0x1069fa
movzbl -0x1a(%rbp), %eax
cmpl $0xff, %eax
jne 0x1069f8
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x1069f8
jmp 0x1069f8
jmp 0x1069fa
jmp 0x1069fc
cmpl $0x0, -0x28(%rbp)
je 0x106a19
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x106a2d
cmpl $0x0, -0x20(%rbp)
je 0x106a2b
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
jmp 0x106a2d
jmp 0x106a2d
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)