// File: /workspace/github_c_makefile_star2_O0/phoboslab[P]pl_mpeg/./pl_mpeg.h
// Function: plm_demux_decode_packet
plm_packet_t *plm_demux_decode_packet(plm_demux_t *self, int type) {
	if (!plm_buffer_has(self->buffer, 16 << 3)) {
		return NULL;
	}

	self->start_code = -1;

	self->next_packet.type = type;
	self->next_packet.length = plm_buffer_read(self->buffer, 16);
	self->next_packet.length -= plm_buffer_skip_bytes(self->buffer, 0xff); // stuffing

	// skip P-STD
	if (plm_buffer_read(self->buffer, 2) == 0x01) {
		plm_buffer_skip(self->buffer, 16);
		self->next_packet.length -= 2;
	}

	int pts_dts_marker = plm_buffer_read(self->buffer, 2);
	if (pts_dts_marker == 0x03) {
		self->next_packet.pts = plm_demux_decode_time(self);
		self->last_decoded_pts = self->next_packet.pts;
		plm_buffer_skip(self->buffer, 40); // skip dts
		self->next_packet.length -= 10;
	}
	else if (pts_dts_marker == 0x02) {
		self->next_packet.pts = plm_demux_decode_time(self);
		self->last_decoded_pts = self->next_packet.pts;
		self->next_packet.length -= 5;
	}
	else if (pts_dts_marker == 0x00) {
		self->next_packet.pts = PLM_PACKET_INVALID_TS;
		plm_buffer_skip(self->buffer, 4);
		self->next_packet.length -= 1;
	}
	else {
		return NULL; // invalid
	}
	
	return plm_demux_get_packet(self);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x80, %esi
callq 0x5270
cmpl $0x0, %eax
jne 0x58d2
movq $0x0, -0x8(%rbp)
jmp 0x5a73
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x38(%rax) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x10, %esi
callq 0x5320
movslq %eax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0xff, %esi
callq 0x5490
movslq %eax, %rdx
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x2, %esi
callq 0x5320
cmpl $0x1, %eax
jne 0x596c
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x10, %esi
callq 0x5450
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
subq $0x2, %rcx
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x2, %esi
callq 0x5320
movl %eax, -0x18(%rbp)
cmpl $0x3, -0x18(%rbp)
jne 0x59d6
movq -0x10(%rbp), %rdi
callq 0x57a0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x78(%rax)
movq -0x10(%rbp), %rax
movsd 0x78(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x28, %esi
callq 0x5450
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
subq $0xa, %rcx
movq %rcx, 0x80(%rax)
jmp 0x5a66
cmpl $0x2, -0x18(%rbp)
jne 0x5a18
movq -0x10(%rbp), %rdi
callq 0x57a0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x78(%rax)
movq -0x10(%rbp), %rax
movsd 0x78(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
subq $0x5, %rcx
movq %rcx, 0x80(%rax)
jmp 0x5a64
cmpl $0x0, -0x18(%rbp)
jne 0x5a58
movq -0x10(%rbp), %rax
movsd 0x75de(%rip), %xmm0     # 0xd008
movsd %xmm0, 0x78(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x4, %esi
callq 0x5450
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
subq $0x1, %rcx
movq %rcx, 0x80(%rax)
jmp 0x5a62
movq $0x0, -0x8(%rbp)
jmp 0x5a73
jmp 0x5a64
jmp 0x5a66
movq -0x10(%rbp), %rdi
callq 0x5a80
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)