// File: mmoanis[P]mpegts_demux/MpegTsDemux.cpp
// Function: MpegTsDemuxer::ReadPES(unsigned char const*&, unsigned char const*, PacketHeader&, unsigned short)
bool MpegTsDemuxer::ReadPES(Packet::const_iterator &p, Packet::const_iterator e, PacketHeader& header, PID id)
{
    if (header.pusi) {
        /// PES PSI
        if (p[0] == 0x0 && p[1] == 0x0 && p[2] == 0x01) {
            p += 3;
            const uint8_t sidx = *p++;
            const bool video = MPEGTS_VIDEO_STREAM(sidx);
            const bool expected = MPEGTS_AUDIO_STREAM(sidx) || video;
            if (!expected) {
                cerr << "[ERROR]: PKT#" << m_pnum <<" Expected audio/video packets only. FOUND=" << hex << uint16_t(sidx) << dec << endl;
                return false;
            }

            Streams::iterator s = m_streams.find(id);
            if (s == m_streams.end()) {
                if (!RegisterStream(id, Program(), video))
                    return false;
                s = m_streams.find(id);
            }
            Stream* S = (*s).second;

            // escape packet length
            p += 2;

            // The rest of the header is of variable length.
            // but we know that there are at least 3 bytes. The third is the length of the optional fields followed by a byte that stores the
            p += 2;



            const uint8_t pl = *p++;
            if (p + pl >= e) {
                cerr << "[ERROR]: PKT#" << m_pnum <<" PES Invalid length." << m_pnum << endl;
                return false;
            }
            p += pl;

            // write to stream
            S->Write(p, e);
        } else {
            cerr << "[ERROR]: PKT#" << m_pnum <<" Expected PES start sequence." << endl;
            return false;
        }
    } else {
        Streams::iterator s = m_streams.find(id);
        if (s == m_streams.end()) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" Invalid STREAM=" << id << endl;
            return false;
        }
        Stream* S = (*s).second;
        S->Write(p, e);
    }
    return true;
}

// Assembly:
subq $0x88, %rsp
movw %r8w, %ax
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movw %ax, 0x5e(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rax
testb $0x1, 0x1(%rax)
je 0x4339
movq 0x70(%rsp), %rax
movq (%rax), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x42e2
movq 0x70(%rsp), %rax
movq (%rax), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x42e2
movq 0x70(%rsp), %rax
movq (%rax), %rax
movzbl 0x2(%rax), %eax
cmpl $0x1, %eax
jne 0x42e2
movq 0x70(%rsp), %rax
movq (%rax), %rcx
addq $0x3, %rcx
movq %rcx, (%rax)
movq 0x70(%rsp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
movb (%rax), %al
movb %al, 0x5d(%rsp)
movzbl 0x5d(%rsp), %ecx
xorl %eax, %eax
cmpl $0xe0, %ecx
movb %al, 0x7(%rsp)
jl 0x4066
movzbl 0x5d(%rsp), %eax
cmpl $0xef, %eax
setle %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
andb $0x1, %al
movb %al, 0x5c(%rsp)
movzbl 0x5d(%rsp), %ecx
movb $0x1, %al
cmpl $0xbd, %ecx
movb %al, 0x6(%rsp)
je 0x40aa
movzbl 0x5d(%rsp), %eax
cmpl $0xc0, %eax
jl 0x40a2
movzbl 0x5d(%rsp), %ecx
movb $0x1, %al
cmpl $0xdf, %ecx
movb %al, 0x6(%rsp)
jle 0x40aa
movb 0x5c(%rsp), %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %al
andb $0x1, %al
movb %al, 0x5b(%rsp)
testb $0x1, 0x5b(%rsp)
jne 0x413f
movq 0x8f2a(%rip), %rdi      # 0xcff0
leaq 0x51a9(%rip), %rsi      # 0x9276
callq 0x2140
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rsi
callq 0x2040
movq %rax, %rdi
leaq 0x5402(%rip), %rsi      # 0x94f2
callq 0x2140
movq %rax, %rdi
leaq 0x17d1(%rip), %rsi      # 0x58d0
callq 0x2060
movq %rax, %rdi
movzbl 0x5d(%rsp), %eax
movzwl %ax, %esi
callq 0x2200
movq %rax, %rdi
leaq 0x17d2(%rip), %rsi      # 0x58f0
callq 0x2060
movq %rax, %rdi
movq 0x8e93(%rip), %rsi      # 0xcfc0
callq 0x2180
movb $0x0, 0x87(%rsp)
jmp 0x4407
movq 0x8(%rsp), %rdi
addq $0x30, %rdi
leaq 0x5e(%rsp), %rsi
callq 0x5910
movq 0x8(%rsp), %rdi
movq %rax, 0x50(%rsp)
addq $0x30, %rdi
callq 0x56a0
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x5940
testb $0x1, %al
jne 0x417f
jmp 0x41ea
movw 0x5e(%rsp), %ax
movw %ax, 0x4(%rsp)
leaq 0x44(%rsp), %rdi
callq 0x5960
movq 0x8(%rsp), %rdi
movw 0x4(%rsp), %cx
movb 0x5c(%rsp), %al
leaq 0x44(%rsp), %rdx
andb $0x1, %al
movzwl %cx, %esi
movzbl %al, %ecx
callq 0x4eb0
xorb $-0x1, %al
testb $0x1, %al
jne 0x41bb
jmp 0x41c8
movb $0x0, 0x87(%rsp)
jmp 0x4407
movq 0x8(%rsp), %rdi
addq $0x30, %rdi
leaq 0x5e(%rsp), %rsi
callq 0x5910
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x56f0
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x70(%rsp), %rax
movq (%rax), %rcx
addq $0x2, %rcx
movq %rcx, (%rax)
movq 0x70(%rsp), %rax
movq (%rax), %rcx
addq $0x2, %rcx
movq %rcx, (%rax)
movq 0x70(%rsp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
movb (%rax), %al
movb %al, 0x2f(%rsp)
movq 0x70(%rsp), %rax
movq (%rax), %rax
movzbl 0x2f(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
cmpq 0x68(%rsp), %rax
jb 0x42b3
movq 0x8d9c(%rip), %rdi      # 0xcff0
leaq 0x501b(%rip), %rsi      # 0x9276
callq 0x2140
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rsi
callq 0x2040
movq %rax, %rdi
leaq 0x529f(%rip), %rsi      # 0x951d
callq 0x2140
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rsi
callq 0x2040
movq %rax, %rdi
movq 0x8d1f(%rip), %rsi      # 0xcfc0
callq 0x2180
movb $0x0, 0x87(%rsp)
jmp 0x4407
movzbl 0x2f(%rsp), %edx
movq 0x70(%rsp), %rax
movq (%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, (%rax)
movq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rax
movq (%rax), %rsi
movq 0x68(%rsp), %rdx
callq 0x2b80
jmp 0x4334
movq 0x8d07(%rip), %rdi      # 0xcff0
leaq 0x4f86(%rip), %rsi      # 0x9276
callq 0x2140
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rsi
callq 0x2040
movq %rax, %rdi
leaq 0x521f(%rip), %rsi      # 0x9532
callq 0x2140
movq %rax, %rdi
movq 0x8c9e(%rip), %rsi      # 0xcfc0
callq 0x2180
movb $0x0, 0x87(%rsp)
jmp 0x4407
jmp 0x43ff
movq 0x8(%rsp), %rdi
addq $0x30, %rdi
leaq 0x5e(%rsp), %rsi
callq 0x5910
movq 0x8(%rsp), %rdi
movq %rax, 0x20(%rsp)
addq $0x30, %rdi
callq 0x56a0
movq %rax, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x5940
testb $0x1, %al
jne 0x4379
jmp 0x43d5
movq 0x8c70(%rip), %rdi      # 0xcff0
leaq 0x4eef(%rip), %rsi      # 0x9276
callq 0x2140
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x90(%rax), %rsi
callq 0x2040
movq %rax, %rdi
leaq 0x51a6(%rip), %rsi      # 0x9550
callq 0x2140
movq %rax, %rdi
movzwl 0x5e(%rsp), %esi
callq 0x2200
movq %rax, %rdi
movq 0x8bfa(%rip), %rsi      # 0xcfc0
callq 0x2180
movb $0x0, 0x87(%rsp)
jmp 0x4407
leaq 0x20(%rsp), %rdi
callq 0x56f0
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movq 0x70(%rsp), %rax
movq (%rax), %rsi
movq 0x68(%rsp), %rdx
callq 0x2b80
movb $0x1, 0x87(%rsp)
movb 0x87(%rsp), %al
andb $0x1, %al
addq $0x88, %rsp
retq
nopl (%rax,%rax)