// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
xorl %edx, %edx
leaq 0x1e6a(%rip), %r14      # 0x55f4
leaq 0x1e64(%rip), %r11      # 0x55f5
movl $0x8018, %ebp           # imm = 0x8018
movq (%r13), %rsi
leaq 0x3(%rsi), %r8
movb $0x1, %al
cmpq %r12, %r8
jae 0x38c5
cmpb $-0x1, 0x4(%rsi)
je 0x3874
leaq 0x1(%rsi), %rax
movq %rax, (%r13)
movzbl 0x1(%rsi), %eax
cmpw $0xe0, %ax
jb 0x388e
movzbl (%rsi), %ecx
cmpl $0x1c, %ecx
setb %r9b
movl $0x8010006, %edi        # imm = 0x8010006
shrl %cl, %edi
andb %r9b, %dil
movb $0x1, %r9b
cmpl $0xf, %ecx
ja 0x37ea
btl %ecx, %ebp
jb 0x37f5
cmpl $0x81, %ecx
je 0x37f5
movl %edi, %r9d
movzbl 0x2(%rsi), %ecx
movq %r8, (%r13)
movzbl 0x3(%rsi), %r8d
cmpq $0xf0, %r8
jb 0x3882
andl $0x3, %r8d
shll $0x8, %r8d
movzbl 0x4(%rsi), %r10d
orq %r8, %r10
leaq 0x5(%rsi), %r8
movq %r8, (%r13)
addq %r10, %rsi
addq $0x5, %rsi
cmpq %r12, %rsi
jae 0x3887
movq %rsi, (%r13)
testb %r9b, %r9b
je 0x379a
movzwl %ax, %eax
andl $0x1f, %eax
shll $0x8, %eax
movzwl %cx, %esi
orl %eax, %esi
movzbl %dil, %ecx
movq %rbx, %rdi
movq %r15, %rdx
movq %rbx, (%rsp)
movq %r15, %rbx
movq %r11, %r15
callq 0x38d4
movq %r15, %r11
movq %rbx, %r15
movq (%rsp), %rbx
movb $0x1, %dl
jmp 0x3796
testb $0x1, %dl
jne 0x38c5
leaq 0x1d60(%rip), %r14      # 0x55e0
jmp 0x388e
movq %r11, %r14
jmp 0x388e
leaq 0x1d82(%rip), %r14      # 0x5610
movq 0x475b(%rip), %rdi      # 0x7ff0
leaq 0x19d6(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x2110
movq %rax, %rdi
callq 0x2080
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq