// File: /workspace/github_cpp_makefile_star2_O3/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 (%rsi), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rsi)
movzbl (%rax), %ecx
addq %rcx, %rax
incq %rax
movq %rax, (%rsi)
cmpq %rdx, %rax
jae 0x315d
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorl %ebp, %ebp
leaq 0x2(%rsp), %r12
leaq 0x1(%rax), %rcx
movq %rcx, (%r15)
movzbl (%rax), %r13d
testl %r13d, %r13d
jne 0x2f0c
movzbl (%rcx), %ecx
testb %cl, %cl
jns 0x3008
testb $0x40, %cl
jne 0x3046
movl %ecx, %edx
notl %edx
testb $0x30, %dl
jne 0x307f
leaq 0x2(%rax), %rdx
cmpq %r14, %rdx
jae 0x30ba
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 0x30f5
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x3a08
testb %al, %al
je 0x315d
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3b16
testb %al, %al
je 0x315d
movq (%r15), %rax
addq $0x4, %rax
movq %rax, (%r15)
movb $0x1, %bpl
cmpq %r14, %rax
jb 0x2e5c
jmp 0x315f
cmpl $0xff, %r13d
jne 0x2f60
testb $0x1, %bpl
movb $0x1, %bpl
jne 0x315f
movq 0x50c7(%rip), %r14      # 0x7ff0
leaq 0x2342(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x232a(%rip), %rsi      # 0x5280
movl $0x12, %edx
jmp 0x312e
movq 0x5089(%rip), %r14      # 0x7ff0
leaq 0x2304(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x22ff(%rip), %rsi      # 0x5293
movl $0x16, %edx
movq %rax, %rdi
callq 0x2160
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x21e0
movq %rax, %rbx
leaq 0x2209(%rip), %rsi      # 0x51c0
movl $0x7, %edx
movq %rax, %rdi
callq 0x2160
leaq 0x1(%rsp), %rsi
movb %r13b, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20c0
jmp 0x315f
movq 0x4fe1(%rip), %r14      # 0x7ff0
leaq 0x225c(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x226e(%rip), %rsi      # 0x52aa
movl $0x26, %edx
jmp 0x312e
movq 0x4fa3(%rip), %r14      # 0x7ff0
leaq 0x221e(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x2257(%rip), %rsi      # 0x52d1
jmp 0x3129
movq 0x4f6a(%rip), %r14      # 0x7ff0
leaq 0x21e5(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x2237(%rip), %rsi      # 0x52ea
movl $0x1f, %edx
jmp 0x312e
movq 0x4f2f(%rip), %r14      # 0x7ff0
leaq 0x21aa(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x221c(%rip), %rsi      # 0x530a
movl $0x19, %edx
jmp 0x312e
movq 0x4ef4(%rip), %r14      # 0x7ff0
leaq 0x216f(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x21fb(%rip), %rsi      # 0x5324
movl $0x18, %edx
movq %rax, %rdi
callq 0x2160
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20c0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq