// File: /workspace/github_c_makefile_star2_O2/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 0x93477
leaq 0x903e7(%rip), %rsi     # 0x123807
movq %rbx, %rdi
callq 0xdfcd5
testl %eax, %eax
setne %r12b
leaq 0x9084c(%rip), %rsi     # 0x123c81
movq %rbx, %rdi
callq 0xdfcd5
testl %eax, %eax
setne %r14b
leaq 0xaaa13(%rip), %rsi     # 0x13de5d
movq %rbx, %rdi
callq 0xdfcd5
movb $0x1, %r15b
testl %eax, %eax
jne 0x93492
leaq 0x90825(%rip), %rsi     # 0x123c85
movq %rbx, %rdi
callq 0xdfcd5
testl %eax, %eax
je 0x9348f
movb $0x1, %r14b
movb $0x1, %r12b
movb $0x1, %r15b
jmp 0x93492
leaq 0x907db(%rip), %rsi     # 0x123c59
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x93550
xorl %r15d, %r15d
movl %r14d, %ecx
orb %r12b, %cl
orb %r15b, %cl
notb %cl
movq 0x28(%rbx), %rax
movb 0x15(%rax), %r13b
testb %r13b, %r13b
sete %dl
xorl %ebp, %ebp
testb %dl, %cl
jne 0x93550
testb %r13b, %r13b
je 0x934bf
movl 0x18(%rax), %esi
jmp 0x934c1
xorl %esi, %esi
movq 0x18(%rbx), %rdi
callq 0xe3f46
movl %eax, %ecx
notl %ecx
testw $0xffe0, %cx            # imm = 0xFFE0
jne 0x934f8
movl %eax, %ecx
shrl %ecx
andl $0x3, %ecx
testb %r12b, %r12b
je 0x93502
cmpl $0x1, %ecx
sete %sil
movl %eax, %edx
andl $0x18, %edx
cmpl $0x8, %edx
setne %dl
andb %sil, %dl
jmp 0x93504
testb %r13b, %r13b
sete %al
xorl %ecx, %ecx
jmp 0x93548
xorl %edx, %edx
cmpl $0x2, %ecx
sete %sil
andb %sil, %r14b
shrb $0x4, %al
cmpl $0x3, %ecx
sete %cl
andb %cl, %r15b
xorb %r14b, %r15b
andb %al, %r15b
orb %dl, %r15b
testb %r13b, %r13b
je 0x93533
testb $0x1, %r15b
je 0x93533
pushq $0x64
popq %rbp
jmp 0x93550
testb %r13b, %r13b
sete %al
orb %r15b, %al
xorl %edx, %edx
testb $0x1, %r15b
pushq $0x64
popq %rcx
cmovel %edx, %ecx
testb $0x1, %al
pushq $0x50
popq %rbp
cmovnel %ecx, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq