// File: /workspace/github_cpp_makefile_star2_O2/mmoanis[P]mpegts_demux/MpegTsDemux.cpp
// Function: MpegTsDemuxer::ReadPAT(unsigned char const*&, unsigned char const*)
bool MpegTsDemuxer::ReadPAT(Packet::const_iterator &p, Packet::const_iterator e)
{
    const uint8_t pointer = *p++;
    p += pointer; // Jump to table at pointer offset

    bool done = false; // Must get at least 1 Program

    // Tables can be repeated.
    while (p < e) {
        const uint8_t id = *p++;
        if (id == MPEGTS_STUFFING_BYTE) {
            if (!done) {
                cerr << "[ERROR]: PKT#" << m_pnum << " Empty PAT packet." << endl;
                return false;
            }
            // we are done, happy return
            return true;
        }
        else if (id == MPEGTS_TABLE_NIL) {
            if (!done) {
                cerr << "[ERROR]: PKT#" << m_pnum << " PAT packet has no associated PSI." << endl;
                return false;
            }
            return true;
        } else if (id != MPEGTS_TABLE_PAS) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" Expected PAT SECTION=" << MPEGTS_TABLE_PAS << " found " << id << endl;
            return false;
        }

        const bool ssi = static_cast<bool> (*p&0x80); // PAT/PMT/CAT == 1
        if (!ssi) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PAT section syntax indicator not set." << endl;
            return false;
        }

        const bool pb = static_cast<bool> (*p&0x40); // PAT/PMT/CAT == 0
        if (pb) {
            cerr << "[ERROR]: PKT#" << m_pnum << " PAT private bit is set." << endl;
            return false;
        }

        if (((*p&0x30)>>4) != 0x03 ) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PAT PSI reserved bits not set." << endl;
            return false;
        }

        if (p + 1 >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum << " PAT PSI not enough data." << endl;
            return false;
        }
        const uint16_t slength = uint16_t((p[0]&0x03)<<8) | uint16_t(p[1]);
        p += 2;
        if (p + slength >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PAT bad section length." << endl;
            return false;
        }

        PacketSection section;
        if (!ReadSection(p, e, section))
            return false;

        // Read PAT, can be repeated
        if (!ReadPrograms(p, e))
            return false;

        // End of the section
        //uint32_t crc = uint32_t(p[0]<<24) | uint32_t(p[1]<<16) | uint32_t(p[2]<<8) | uint32_t(p[3]);
        //cout << "CRC: " << hex << crc << dec << endl;
        p += 4;
        done = true;
    }

    return done;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rsi)
movzbl (%rax), %ecx
addq %rcx, %rax
incq %rax
movq %rax, (%rsi)
xorl %ebp, %ebp
leaq 0x2(%rsp), %r12
cmpq %r14, %rax
jae 0x2dc2
leaq 0x1(%rax), %rcx
movq %rcx, (%r15)
movzbl (%rax), %r13d
testl %r13d, %r13d
jne 0x2c2f
movzbl (%rcx), %ecx
testb %cl, %cl
jns 0x2cd8
testb $0x40, %cl
jne 0x2d06
movl %ecx, %edx
notl %edx
testb $0x30, %dl
jne 0x2d31
leaq 0x2(%rax), %rdx
cmpq %r14, %rdx
jae 0x2d5c
andl $0x3, %ecx
shll $0x8, %ecx
movzbl 0x2(%rax), %edx
orq %rcx, %rdx
leaq 0x3(%rax), %rcx
movq %rcx, (%r15)
addq %rdx, %rax
addq $0x3, %rax
cmpq %r14, %rax
jae 0x2d87
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x342a
testb %al, %al
je 0x2dc0
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x34ee
testb %al, %al
je 0x2dc0
movq (%r15), %rax
addq $0x4, %rax
movq %rax, (%r15)
movb $0x1, %bpl
jmp 0x2b7f
cmpl $0xff, %r13d
jne 0x2c73
testb $0x1, %bpl
movb $0x1, %bpl
jne 0x2dc2
movq 0x53a4(%rip), %rdi      # 0x7ff0
leaq 0x261f(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2612(%rip), %rsi      # 0x5280
jmp 0x2db0
movq 0x5376(%rip), %rdi      # 0x7ff0
leaq 0x25f1(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x25f7(%rip), %rsi      # 0x5293
movq %rax, %rdi
callq 0x2110
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x21e0
leaq 0x2509(%rip), %rsi      # 0x51c0
movq %rax, %rdi
callq 0x2110
movsbl %r13b, %esi
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2080
jmp 0x2dc2
movq 0x5311(%rip), %rdi      # 0x7ff0
leaq 0x258c(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x25a9(%rip), %rsi      # 0x52aa
jmp 0x2db0
movq 0x52e3(%rip), %rdi      # 0x7ff0
leaq 0x255e(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x25a2(%rip), %rsi      # 0x52d1
jmp 0x2db0
movq 0x52b8(%rip), %rdi      # 0x7ff0
leaq 0x2533(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2590(%rip), %rsi      # 0x52ea
jmp 0x2db0
movq 0x528d(%rip), %rdi      # 0x7ff0
leaq 0x2508(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2585(%rip), %rsi      # 0x530a
jmp 0x2db0
movq 0x5262(%rip), %rdi      # 0x7ff0
leaq 0x24dd(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2574(%rip), %rsi      # 0x5324
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2080
xorl %ebp, %ebp
andb $0x1, %bpl
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop