// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq (%rdi), %r15
xorl %eax, %eax
cmpl $0x0, 0x60(%r15)
sete %al
movq 0x10(%r15,%rax,8), %r14
movsd 0x30(%rdi), %xmm0
ucomisd 0x5efe(%rip), %xmm0     # 0x9008
jne 0x310e
jnp 0x3118
cmpq %r14, 0x18(%rbx)
je 0x324b
movq 0x48(%r15), %rax
testq %rax, %rax
je 0x313f
movq 0x50(%r15), %rsi
movq %r15, %rdi
callq *%rax
movq (%r15), %rcx
shrq $0x3, %rcx
addq %rax, %rcx
subq 0x10(%r15), %rcx
movq %rcx, 0x10(%rsp)
jmp 0x314b
movq (%r15), %rax
shrq $0x3, %rax
movq %rax, 0x10(%rsp)
movl 0x38(%rbx), %eax
movl %eax, 0xc(%rsp)
movl $0x10000, %r13d         # imm = 0x10000
xorl %r15d, %r15d
movq %r14, %r12
subq %r13, %r12
testq %r12, %r12
movl $0x0, %esi
cmovgq %r12, %rsi
movq (%rbx), %rdi
callq 0x430a
movq %r15, 0x60(%rbx)
movq %r15, 0x80(%rbx)
movl $0xffffffff, 0x38(%rbx) # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x387e
movsd 0x5e71(%rip), %xmm0     # 0x9008
movsd %xmm0, (%rsp)
testq %rax, %rax
je 0x31d5
movsd 0x5e5f(%rip), %xmm0     # 0x9008
movsd %xmm0, (%rsp)
movsd 0x8(%rax), %xmm0
ucomisd 0x5e4d(%rip), %xmm0     # 0x9008
jne 0x31bf
jnp 0x31c8
cmpl %ebp, (%rax)
jne 0x31c8
movsd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x387e
testq %rax, %rax
jne 0x31ae
movsd (%rsp), %xmm0
ucomisd 0x5e26(%rip), %xmm0     # 0x9008
jne 0x3204
jp 0x3204
addq %r13, %r13
testq %r12, %r12
movl $0x800000, %eax         # imm = 0x800000
cmovsq %rax, %r13
cmpq $0x400001, %r13         # imm = 0x400001
jb 0x315b
jmp 0x3221
movq %rbx, %rdi
movl %ebp, %esi
movsd %xmm0, (%rsp)
callq 0x3b5e
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rbx)
movq (%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0x430a
xorl %eax, %eax
movq %rax, 0x60(%rbx)
movq %rax, 0x80(%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0x38(%rbx)
movq %r14, 0x18(%rbx)
movsd 0x30(%rbx), %xmm0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq