// File: /workspace/github_c_makefile_star2_O1/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 0x995d6
leaq 0x97230(%rip), %rsi     # 0x1307af
movq %rbx, %rdi
callq 0xea936
testl %eax, %eax
setne %r13b
leaq 0x97695(%rip), %rsi     # 0x130c29
movq %rbx, %rdi
callq 0xea936
testl %eax, %eax
setne %r15b
leaq 0xb178d(%rip), %rsi     # 0x14ad36
movq %rbx, %rdi
callq 0xea936
movb $0x1, %r14b
testl %eax, %eax
jne 0x995f1
leaq 0x9766e(%rip), %rsi     # 0x130c2d
movq %rbx, %rdi
callq 0xea936
testl %eax, %eax
je 0x995ee
movb $0x1, %r15b
movb $0x1, %r13b
movb $0x1, %r14b
jmp 0x995f1
leaq 0x97624(%rip), %rsi     # 0x130c01
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x996a5
xorl %r14d, %r14d
movl %r15d, %ecx
orb %r13b, %cl
orb %r14b, %cl
notb %cl
movq 0x28(%rbx), %rax
movb 0x15(%rax), %r12b
testb %r12b, %r12b
sete %dl
xorl %ebp, %ebp
testb %dl, %cl
jne 0x996a5
testb %r12b, %r12b
je 0x9961e
movl 0x18(%rax), %esi
jmp 0x99620
xorl %esi, %esi
movq 0x18(%rbx), %rdi
callq 0xef169
movl %eax, %ecx
notl %ecx
testl $0xffe0, %ecx           # imm = 0xFFE0
jne 0x99658
movl %eax, %ecx
shrl %ecx
andl $0x3, %ecx
testb %r13b, %r13b
je 0x9965d
cmpl $0x1, %ecx
sete %sil
movl %eax, %edx
andl $0x18, %edx
cmpl $0x8, %edx
setne %dl
andb %sil, %dl
jmp 0x9965f
xorl %r14d, %r14d
jmp 0x9967e
xorl %edx, %edx
cmpl $0x2, %ecx
sete %sil
andb %sil, %r15b
shrb $0x4, %al
cmpl $0x3, %ecx
sete %cl
andb %cl, %r14b
xorb %r15b, %r14b
andb %al, %r14b
orb %dl, %r14b
testb %r12b, %r12b
je 0x9968d
movl $0x64, %ebp
testb %r14b, %r14b
jne 0x996a5
xorl %eax, %eax
testb %r12b, %r12b
movl $0x50, %ecx
cmovel %eax, %ecx
testb %r14b, %r14b
movl $0x64, %ebp
cmovel %ecx, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq