// File: /workspace/github_c_makefile_star2_O0/phoboslab[P]pl_mpeg/./pl_mpeg.h
// Function: plm_demux_get_duration
double plm_demux_get_duration(plm_demux_t *self, int type) {
	size_t file_size = plm_buffer_get_size(self->buffer);

	if (
		self->duration != PLM_PACKET_INVALID_TS &&
		self->last_file_size == file_size
	) {
		return self->duration;
	}

	size_t previous_pos = plm_buffer_tell(self->buffer);
	int previous_start_code = self->start_code;
	
	// Find last video PTS. Start searching 64kb from the end and go further 
	// back if needed.
	long start_range = 64 * 1024;
	long max_range = 4096 * 1024;
	for (long range = start_range; range <= max_range; range *= 2) {
		long seek_pos = file_size - range;
		if (seek_pos < 0) {
			seek_pos = 0;
			range = max_range; // Make sure to bail after this round
		}
		plm_demux_buffer_seek(self, seek_pos);
		self->current_packet.length = 0;

		double last_pts = PLM_PACKET_INVALID_TS;
		plm_packet_t *packet = NULL;
		while ((packet = plm_demux_decode(self))) {
			if (packet->pts != PLM_PACKET_INVALID_TS && packet->type == type) {
				last_pts = packet->pts;
			}
		}
		if (last_pts != PLM_PACKET_INVALID_TS) {
			self->duration = last_pts - plm_demux_get_start_time(self, type);
			break;
		}
	}

	plm_demux_buffer_seek(self, previous_pos);
	self->start_code = previous_start_code;
	self->last_file_size = file_size;
	return self->duration;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x5000
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm0
movsd 0x98d8(%rip), %xmm1     # 0xd008
ucomisd %xmm1, %xmm0
jne 0x373a
jp 0x373a
jmp 0x375b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x20(%rbp), %rax
jne 0x375b
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x38bc
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x51e0
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x2c(%rbp)
movq $0x10000, -0x38(%rbp)   # imm = 0x10000
movq $0x400000, -0x40(%rbp)  # imm = 0x400000
movq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jg 0x388b
movq -0x20(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jge 0x37be
movq $0x0, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x5850
movq -0x10(%rbp), %rax
movq $0x0, 0x60(%rax)
movsd 0x9829(%rip), %xmm0     # 0xd008
movsd %xmm0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x42a0
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x3835
movq -0x60(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd 0x97f8(%rip), %xmm1     # 0xd008
ucomisd %xmm1, %xmm0
jne 0x381a
jp 0x381a
jmp 0x3833
movq -0x60(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x3833
movq -0x60(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x37ec
movsd -0x58(%rbp), %xmm0
movsd 0x97c6(%rip), %xmm1     # 0xd008
ucomisd %xmm1, %xmm0
jne 0x384c
jp 0x384c
jmp 0x3879
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x46e0
movaps %xmm0, %xmm1
movsd -0x68(%rbp), %xmm0
subsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x30(%rax)
jmp 0x388b
jmp 0x387b
movq -0x48(%rbp), %rax
shlq %rax
movq %rax, -0x48(%rbp)
jmp 0x378d
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5850
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)