// File: /workspace/github_c_makefile_star2_O0/phoboslab[P]pl_mpeg/./pl_mpeg.h
// Function: plm_audio_decode_header
int plm_audio_decode_header(plm_audio_t *self) {
	if (!plm_buffer_has(self->buffer, 48)) {
		return 0;
	}

	plm_buffer_skip_bytes(self->buffer, 0x00);
	int sync = plm_buffer_read(self->buffer, 11);


	// Attempt to resync if no syncword was found. This sucks balls. The MP2 
	// stream contains a syncword just before every frame (11 bits set to 1).
	// However, this syncword is not guaranteed to not occur elsewhere in the
	// stream. So, if we have to resync, we also have to check if the header 
	// (samplerate, bitrate) differs from the one we had before. This all
	// may still lead to garbage data being decoded :/

	if (sync != PLM_AUDIO_FRAME_SYNC && !plm_audio_find_frame_sync(self)) {
		return 0;
	}

	self->version = plm_buffer_read(self->buffer, 2);
	self->layer = plm_buffer_read(self->buffer, 2);
	int hasCRC = !plm_buffer_read(self->buffer, 1);

	if (
		self->version != PLM_AUDIO_MPEG_1 ||
		self->layer != PLM_AUDIO_LAYER_II
	) {
		return 0;
	}

	int bitrate_index = plm_buffer_read(self->buffer, 4) - 1;
	if (bitrate_index > 13) {
		return 0;
	}

	int samplerate_index = plm_buffer_read(self->buffer, 2);
	if (samplerate_index == 3) {
		return 0;
	}

	int padding = plm_buffer_read(self->buffer, 1);
	plm_buffer_skip(self->buffer, 1); // f_private
	int mode = plm_buffer_read(self->buffer, 2);

	// If we already have a header, make sure the samplerate, bitrate and mode
	// are still the same, otherwise we might have missed sync.
	if (
		self->has_header && (
			self->bitrate_index != bitrate_index ||
			self->samplerate_index != samplerate_index ||
			self->mode != mode
		)
	) {
		return 0;
	}

	self->bitrate_index = bitrate_index;
	self->samplerate_index = samplerate_index;
	self->mode = mode;
	self->has_header = TRUE;

	// Parse the mode_extension, set up the stereo bound
	if (mode == PLM_AUDIO_MODE_JOINT_STEREO) {
		self->bound = (plm_buffer_read(self->buffer, 2) + 1) << 2;
	}
	else {
		plm_buffer_skip(self->buffer, 2);
		self->bound = (mode == PLM_AUDIO_MODE_MONO) ? 0 : 32;
	}

	// Discard the last 4 bits of the header and the CRC value, if present
	plm_buffer_skip(self->buffer, 4); // copyright(1), original(1), emphasis(2)
	if (hasCRC) {
		plm_buffer_skip(self->buffer, 16);
	}

	// Compute frame size, check if we have enough data to decode the whole
	// frame.
	int bitrate = PLM_AUDIO_BIT_RATE[self->bitrate_index];
	int samplerate = PLM_AUDIO_SAMPLE_RATE[self->samplerate_index];
	int frame_size = (144000 * bitrate / samplerate) + padding;
	return frame_size - (hasCRC ? 6 : 4);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x30, %esi
callq 0x5270
cmpl $0x0, %eax
jne 0x949f
movl $0x0, -0x4(%rbp)
jmp 0x9722
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
xorl %esi, %esi
callq 0x5490
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0xb, %esi
callq 0x5320
movl %eax, -0x14(%rbp)
cmpl $0x7ff, -0x14(%rbp)     # imm = 0x7FF
je 0x94e6
movq -0x10(%rbp), %rdi
callq 0xa0b0
cmpl $0x0, %eax
jne 0x94e6
movl $0x0, -0x4(%rbp)
jmp 0x9722
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x2, %esi
callq 0x5320
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x2, %esi
callq 0x5320
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x1, %esi
callq 0x5320
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, 0x14(%rax)
jne 0x9552
movq -0x10(%rbp), %rax
cmpl $0x2, 0x18(%rax)
je 0x955e
movl $0x0, -0x4(%rbp)
jmp 0x9722
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x4, %esi
callq 0x5320
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0xd, -0x1c(%rbp)
jle 0x9588
movl $0x0, -0x4(%rbp)
jmp 0x9722
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x2, %esi
callq 0x5320
movl %eax, -0x20(%rbp)
cmpl $0x3, -0x20(%rbp)
jne 0x95af
movl $0x0, -0x4(%rbp)
jmp 0x9722
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x1, %esi
callq 0x5320
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x1, %esi
callq 0x5450
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x2, %esi
callq 0x5320
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x9625
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0x1c(%rbp), %eax
jne 0x9619
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
cmpl -0x20(%rbp), %eax
jne 0x9619
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
cmpl -0x28(%rbp), %eax
je 0x9625
movl $0x0, -0x4(%rbp)
jmp 0x9722
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x2c(%rax)
cmpl $0x1, -0x28(%rbp)
jne 0x9677
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x2, %esi
callq 0x5320
movl %eax, %ecx
addl $0x1, %ecx
shll $0x2, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0x96a0
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x2, %esi
callq 0x5450
movl -0x28(%rbp), %edx
movl $0x20, %ecx
xorl %eax, %eax
cmpl $0x3, %edx
cmovel %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x4, %esi
callq 0x5450
cmpl $0x0, -0x18(%rbp)
je 0x96ca
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movl $0x10, %esi
callq 0x5450
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rcx
leaq 0x4b37(%rip), %rax      # 0xe210
movswl (%rax,%rcx,2), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rcx
leaq 0x4b11(%rip), %rax      # 0xe200
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x30(%rbp)
imull $0x23280, -0x2c(%rbp), %eax # imm = 0x23280
cltd
idivl -0x30(%rbp)
addl -0x24(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0x18(%rbp), %esi
movl $0x4, %ecx
movl $0x6, %edx
cmpl $0x0, %esi
cmovnel %edx, %ecx
subl %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)