// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/mp3.c
// Function: de_identify_mpegaudio
static int de_identify_mpegaudio(deark *c)
{
	unsigned int x;
	unsigned int ver_id, lyr_id;
	int has_mp1_ext = 0;
	int has_mp2_ext = 0;
	int has_mp3_ext = 0;
	int has_any_ext;
	int looks_valid = 0;
	u8 has_id3v2;
	i64 pos;

	if(!c->detection_data->id3.detection_attempted) {
		de_err(c, "mpegaudio detection requires id3 module");
		return 0;
	}

	if(de_input_file_has_ext(c, "mp3")) {
		has_mp3_ext = 1;
	}
	if(de_input_file_has_ext(c, "mp2")) {
		has_mp2_ext = 1;
	}
	if(de_input_file_has_ext(c, "mp1")) {
		has_mp1_ext = 1;
	}
	else if(de_input_file_has_ext(c, "mpa")) {
		has_mp1_ext = 1;
		has_mp2_ext = 1;
		has_mp3_ext = 1;
	}
	has_any_ext = has_mp3_ext || has_mp2_ext || has_mp1_ext;

	has_id3v2 = c->detection_data->id3.has_id3v2;

	if(!has_id3v2 && !has_any_ext) {
		// TODO: We could try harder to identify MP3.
		return 0;
	}

	if(has_id3v2) {
		pos = (i64)c->detection_data->id3.bytes_at_start;
	}
	else {
		pos = 0;
	}

	x = (unsigned int)de_getu16be(pos);
	if((x&0xffe0) == 0xffe0) {
		ver_id = (x&0x0018)>>3;
		lyr_id = (x&0x0006)>>1;

		if(has_mp3_ext) {
			if((lyr_id==1) && (ver_id!=1)) looks_valid = 1;
		}
		if(has_mp2_ext) {
			if((lyr_id==2) && (ver_id==2 || ver_id==3)) looks_valid = 1;
		}
		if(has_mp1_ext) {
			if((lyr_id==3) && (ver_id==2 || ver_id==3)) looks_valid = 1;
		}
	}

	if(has_id3v2 && looks_valid) return 100;
	if(has_id3v2 && !looks_valid) {
		// This must be lower than the corresponding confidence for other
		// audio formats that might start with ID3v2, like Ogg.
		return 80;
	}
	if(looks_valid) {
		return 100;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x28(%rdi), %rax
cmpb $0x0, 0x14(%rax)
je 0x98cd1
leaq 0x97e5c(%rip), %rsi     # 0x130aea
movq %rbx, %rdi
callq 0xea491
movl %eax, %ebp
testl %eax, %eax
setne %r13b
leaq 0x982bf(%rip), %rsi     # 0x130f64
movq %rbx, %rdi
callq 0xea491
movl %eax, %r14d
testl %eax, %eax
setne %r15b
leaq 0xb2670(%rip), %rsi     # 0x14b32d
movq %rbx, %rdi
callq 0xea491
testl %eax, %eax
je 0x98ce9
movl %r15d, %r12d
movl %r13d, %ecx
jmp 0x98d05
leaq 0x98264(%rip), %rsi     # 0x130f3c
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x98dab
leaq 0x98278(%rip), %rsi     # 0x130f68
movq %rbx, %rdi
callq 0xea491
movb $0x1, %r12b
movb $0x1, %cl
testl %eax, %eax
je 0x98dbc
movq 0x28(%rbx), %rax
movb $0x1, %r14b
cmpb $0x0, 0x15(%rax)
je 0x98d1f
movl %ecx, %ebp
movl 0x18(%rax), %esi
movb $0x1, %r13b
xorl %r15d, %r15d
jmp 0x98d29
movl %ecx, %ebp
xorl %r13d, %r13d
movb $0x1, %r15b
xorl %esi, %esi
movq 0x18(%rbx), %rdi
callq 0xeeb67
movl %eax, %edx
notl %edx
xorl %ecx, %ecx
testl $0xffe0, %edx           # imm = 0xFFE0
jne 0x98d9f
movl %eax, %ecx
shrl %ecx
andl $0x3, %ecx
testb %bpl, %bpl
je 0x98d63
cmpl $0x1, %ecx
sete %sil
movl %eax, %edx
andl $0x18, %edx
cmpl $0x8, %edx
setne %dl
andb %sil, %dl
jmp 0x98d65
xorl %edx, %edx
cmpl $0x2, %ecx
sete %sil
andb %sil, %r12b
shrb $0x4, %al
cmpl $0x3, %ecx
sete %cl
andb %cl, %r14b
xorb %r12b, %r14b
andb %al, %r14b
orb %dl, %r14b
movl $0x64, %ebp
testb %r14b, %r13b
jne 0x98dab
orb %r14b, %r15b
xorl %eax, %eax
testb $0x1, %r14b
movl $0x64, %ecx
cmovel %eax, %ecx
testb $0x1, %r15b
movl $0x50, %ebp
cmovnel %ecx, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orl %ebp, %r14d
sete %dl
movq 0x28(%rbx), %rax
movb 0x15(%rax), %cl
testb %cl, %cl
sete %sil
xorl %ebp, %ebp
testb %sil, %dl
jne 0x98dab
xorl %r14d, %r14d
movl %r15d, %r12d
testb %cl, %cl
je 0x98de8
movl %r13d, %ecx
jmp 0x98d12
movb $0x1, %r15b
movl %r13d, %ebp
xorl %r13d, %r13d
jmp 0x98d27