// File: /workspace/github_cpp_makefile_star2_O3/mmoanis[P]mpegts_demux/MpegTsDemux.cpp
// Function: MpegTsDemuxer::ReadESD(unsigned char const*&, unsigned char const*, Program const&)
bool MpegTsDemuxer::ReadESD(Packet::const_iterator &p, Packet::const_iterator e, const Program& prog)
{
    bool found = false;
    while (p + 3 < e) {
        // watch out not to read CRC
        if (p[4] == MPEGTS_STUFFING_BYTE) {
            // Oh, we hit it. Okay happy return.
            if (!found) {
                cerr << "[ERROR]: PKT#" << m_pnum << " Empty ESD section." << endl;
                return false;
            }
            return true;
        }

        const uint8_t st = *p++;
        if (((*p&0xE0)>>5) != 0x07) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" ESD reserved bits not set." << endl;
            return false;
        }

        // See what is the stream type
        const bool video = MPEGTS_VIDEO_STREAM_TYPE(st);
        const bool needed = MPEGTS_AUDIO_STREAM_TYPE(st) || video;

        PID id = PID((p[0]&0x1F)<<8) | PID(p[1]);
        p += 2;

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

        const uint16_t esil = uint16_t((p[0]&0x03)<<8) | uint16_t(p[1]);
        p += 2;

        if (p + esil >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" ESD invalid info length." << endl;
            return false;
        }
        p += esil;

        // Add video/audio streams only
        if (needed) {
            if (!RegisterStream(id, prog, video))
                return false;
            found = true; // we found a stream
        }
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r14
movq (%rsi), %rax
leaq 0x3(%rax), %rcx
movb $0x1, %bpl
cmpq %rdx, %rcx
jae 0x40cd
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
xorl %edx, %edx
movl $0x8018, %r13d          # imm = 0x8018
cmpb $-0x1, 0x4(%rax)
je 0x3fa6
leaq 0x1(%rax), %rcx
movq %rcx, (%r12)
movzbl 0x1(%rax), %ecx
movzwl %cx, %esi
cmpl $0xdf, %esi
jbe 0x3fed
movzbl (%rax), %ecx
cmpl $0x1c, %ecx
setb %r8b
movl $0x8010006, %edi        # imm = 0x8010006
shrl %cl, %edi
andb %r8b, %dil
movb $0x1, %r8b
cmpl $0xf, %ecx
ja 0x3f17
btl %ecx, %r13d
jb 0x3f22
cmpl $0x81, %ecx
je 0x3f22
movl %edi, %r8d
movzbl 0x2(%rax), %ecx
leaq 0x3(%rax), %r9
movq %r9, (%r12)
movzbl 0x3(%rax), %r9d
cmpq $0xef, %r9
jbe 0x4028
andl $0x3, %r9d
shll $0x8, %r9d
movzbl 0x4(%rax), %r10d
orq %r9, %r10
leaq 0x5(%rax), %r9
movq %r9, (%r12)
addq %r10, %rax
addq $0x5, %rax
cmpq %r15, %rax
jae 0x4063
movq %rax, (%r12)
testb %r8b, %r8b
je 0x3f91
andl $0x1f, %esi
shll $0x8, %esi
orl %ecx, %esi
movzwl %si, %esi
movzbl %dil, %ecx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x40de
movq (%r12), %rax
movb $0x1, %dl
leaq 0x3(%rax), %rcx
cmpq %r15, %rcx
jb 0x3ed0
movb $0x1, %bpl
jmp 0x40cd
testb $0x1, %dl
jne 0x40cd
movq 0x403a(%rip), %r14      # 0x7ff0
leaq 0x12b5(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x15fd(%rip), %rsi      # 0x55e0
movl $0x13, %edx
jmp 0x409c
movq 0x3ffc(%rip), %r14      # 0x7ff0
leaq 0x1277(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x15d3(%rip), %rsi      # 0x55f4
movl $0x1b, %edx
jmp 0x409c
movq 0x3fc1(%rip), %r14      # 0x7ff0
leaq 0x123c(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1599(%rip), %rsi      # 0x55f5
movl $0x1a, %edx
jmp 0x409c
movq 0x3f86(%rip), %r14      # 0x7ff0
leaq 0x1201(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1579(%rip), %rsi      # 0x5610
movl $0x19, %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