// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/arc.c
// Function: de_identify_arc
static int de_identify_arc(deark *c)
{
	static const char *exts[] = {"arc", "ark", "pak", "spk", "sdn", "com"};
	u8 has_ext = 0;
	UI ext_idx = 0;
	UI strictness;
	int ends_with_trailer = 0;
	int ends_with_comments = 0;
	int starts_with_trailer = 0;
	i64 arc_start = 0;
	size_t k;
	u8 cmpr_meth;
	u8 buf[5];

	de_read(buf, 0, sizeof(buf));

	// Look for 0x1a in the first 4 bytes. Some .COM-style self-extracting
	// archives start with 1-3 bytes of code before the ARC marker.
	if(!find_arc_marker(c, buf, sizeof(buf)-1, &arc_start)) {
		return 0;
	}

	cmpr_meth = buf[arc_start+1];

	if(cmpr_meth==0) {
		// Don't tolerate empty archives that don't start at the beginning of file.
		if(arc_start!=0) return 0;

		starts_with_trailer = 1;
	}

	// Get info about file extension
	for(k=0; k<DE_ARRAYCOUNT(exts); k++) {
		if(de_input_file_has_ext(c, exts[k])) {
			has_ext = 1;
			ext_idx = (UI)k;
			break;
		}
	}

	// Only tolerate leading junk for a few file extensions
	if(arc_start>0) {
		if(ext_idx==0 || ext_idx==1 || ext_idx==5) { // .arc, .ark, .com
			;
		}
		else {
			return 0;
		}
	}

	// Look at some of the file, to see if it seems ok.
	if(has_ext && ext_idx<=1 && arc_start==0) {
		strictness = 0;
	}
	else if(has_ext && ext_idx<=3 && arc_start==0) {
		strictness = 1;
	}
	else {
		strictness = 2;
	}
	if(!is_valid_file_at(c->infile, arc_start, c->infile->len, strictness)) {
		return 0;
	}

	// Handle 2-byte files
	if(starts_with_trailer && c->infile->len==2) {
		if(!has_ext) return 0;
		if(ext_idx==0) return 100;
		if(ext_idx<=3) return 15;
		return 0;
	}

	if(de_getu16be(c->infile->len-2) == 0x1a00) {
		// Standard ARC trailer
		ends_with_trailer = 1;
	}

	if(de_getu32be(c->infile->len-8) == 0x504baa55) {
		// PKARC trailer, for files with comments
		ends_with_comments = 1;
	}

	if(!ends_with_trailer && !ends_with_comments) {
		// PAK-style extensions
		if(de_getu16be(c->infile->len-2) == 0xfe00) {
			ends_with_comments = 1;
		}
	}

	if(starts_with_trailer) {
		if(ends_with_comments) return 25;
		else return 0;
	}
	if(has_ext && (ends_with_trailer || ends_with_comments)) return 90;
	if(ends_with_trailer || ends_with_comments) return 35;
	if(has_ext) return 24;
	if(arc_start==0) return 19;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
leaq 0x23(%rsp), %rsi
movl $0x5, %ecx
xorl %edx, %edx
callq 0xee8d4
movl $0x1, %r12d
cmpb $0x1a, 0x22(%rsp,%r12)
je 0x262c2
incq %r12
cmpq $0x5, %r12
jne 0x262ac
jmp 0x263ab
movb 0x23(%rsp,%r12), %bpl
testb %bpl, %bpl
sete %al
cmpq $0x1, %r12
setne %cl
testb %al, %cl
jne 0x263ab
leaq 0xf927f(%rip), %rsi     # 0x11f562
movq %rbx, %rdi
callq 0xea491
testl %eax, %eax
je 0x262fb
xorl %r14d, %r14d
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x26340
leaq 0xee302(%rip), %r15     # 0x114604
xorl %r14d, %r14d
cmpq $0x5, %r14
je 0x26334
movslq 0x4(%r15,%r14,4), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0xea491
incq %r14
testl %eax, %eax
je 0x26305
leaq -0x1(%r14), %rax
cmpq $0x5, %rax
setb %al
movq %rax, 0x10(%rsp)
jmp 0x26340
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
cmpq $0x1, %r12
je 0x26359
xorl %eax, %eax
cmpl $0x5, %r14d
ja 0x263ad
movl $0x23, %ecx
btl %r14d, %ecx
jae 0x263ad
movb %bpl, 0xf(%rsp)
cmpq $0x1, %r12
sete %r13b
cmpl $0x2, %r14d
setb 0xe(%rsp)
movq %r14, 0x30(%rsp)
cmpl $0x4, %r14d
setb %r15b
movq 0x18(%rbx), %r14
movq 0x18(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0x18(%rsp), %rsi
movq %r12, (%rsi)
movq %r14, %rdi
callq 0xeeb4b
movl %eax, %ebp
cmpb $0xc, %al
setae %al
cmpb $0x14, %bpl
setne %cl
testb %cl, %al
je 0x263bc
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %bpl, %bpl
je 0x26436
andb 0x10(%rsp), %r13b
andb %r13b, %r15b
cmpb $0x14, %bpl
setae %al
orb %r15b, %al
movq 0x18(%rsp), %rsi
je 0x263ef
andb 0xe(%rsp), %r13b
addq $0xd, %rsi
movq %rsi, 0x18(%rsp)
testb %r13b, %r13b
je 0x26401
jmp 0x26436
movq %r14, %rdi
callq 0xeead3
cmpb $0x20, %al
jb 0x263ab
addq $0xd, 0x18(%rsp)
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xeed83
movq (%r15), %rcx
xorl %edx, %edx
cmpb $0x1, %bpl
setne %dl
leaq (%rax,%rdx,4), %rax
addq %rcx, %rax
addq $0x6, %rax
movq %rax, (%r15)
cmpq 0x28(%rsp), %rax
jne 0x26521
movb 0xf(%rsp), %r15b
testb %r15b, %r15b
setne %al
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
cmpq $0x2, %rsi
setne %cl
orb %al, %cl
je 0x264f5
addq $-0x2, %rsi
callq 0xeeb67
movq %rax, %r14
cmpq $0x1a00, %rax           # imm = 0x1A00
sete %bpl
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x8, %rsi
callq 0xeed0d
cmpq $0x504baa55, %rax       # imm = 0x504BAA55
sete %al
orb %al, %bpl
jne 0x264a7
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x2, %rsi
callq 0xeeb67
cmpq $0xfe00, %rax           # imm = 0xFE00
sete %al
testb %r15b, %r15b
je 0x2657b
cmpq $0x1a00, %r14           # imm = 0x1A00
sete %cl
orb %al, %cl
movl $0x5a, %eax
testb %cl, 0x10(%rsp)
jne 0x263ad
xorl %eax, %eax
cmpq $0x1, %r12
movl $0x13, %edx
cmovnel %eax, %edx
cmpb $0x0, 0x10(%rsp)
movl $0x18, %esi
cmovel %edx, %esi
testb %cl, %cl
movl $0x23, %eax
cmovel %esi, %eax
jmp 0x263ad
cmpb $0x0, 0x10(%rsp)
je 0x263ab
xorl %eax, %eax
movq 0x30(%rsp), %rdx
cmpl $0x4, %edx
movl $0xf, %ecx
cmovael %eax, %ecx
testl %edx, %edx
movl $0x64, %eax
cmovnel %ecx, %eax
jmp 0x263ad
movq 0x28(%rsp), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
jg 0x263ab
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0xeeb4b
cmpb $0x1a, %al
jne 0x263ab
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0xeeb4b
cmpb $0xc, %al
jb 0x26436
cmpb $0x1e, %al
ja 0x263ab
movzbl %al, %eax
movl $0x40700000, %ecx       # imm = 0x40700000
btl %eax, %ecx
jb 0x26436
jmp 0x263ab
xorl %ecx, %ecx
testb %al, %al
movl $0x19, %eax
cmovel %ecx, %eax
jmp 0x263ad