// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x18, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movl $0x0, 0xc(%rsp)
movq 0x3fbf(%rip), %r13      # 0x7ff0
movq (%r12), %rdx
leaq 0x3(%rdx), %rsi
cmpq %r15, %rsi
setae %r14b
jae 0x4265
cmpb $-0x1, 0x4(%rdx)
je 0x41f0
leaq 0x1(%rdx), %rax
movq %rax, (%r12)
movzbl 0x1(%rdx), %eax
movzwl %ax, %eax
cmpl $0xdf, %eax
jbe 0x4114
movzbl (%rdx), %ecx
cmpl $0x1c, %ecx
setb %r8b
movl $0x8010006, %edi        # imm = 0x8010006
shrl %cl, %edi
andb %r8b, %dil
movb $0x1, %r8b
cmpl $0xf, %ecx
ja 0x4092
movl $0x8018, %r9d           # imm = 0x8018
btl %ecx, %r9d
jb 0x409d
cmpl $0x81, %ecx
je 0x409d
movl %edi, %r8d
movzbl 0x2(%rdx), %ecx
movq %rsi, (%r12)
movzbl 0x3(%rdx), %esi
cmpq $0xef, %rsi
jbe 0x414b
andl $0x3, %esi
shll $0x8, %esi
movzbl 0x4(%rdx), %r9d
orq %rsi, %r9
leaq 0x5(%rdx), %rsi
movq %rsi, (%r12)
addq %r9, %rdx
addq $0x5, %rdx
cmpq %r15, %rdx
jae 0x4182
movq %rdx, (%r12)
movb $0x1, %dl
testb %r8b, %r8b
je 0x41e6
andl $0x1f, %eax
shll $0x8, %eax
orl %ecx, %eax
movzwl %ax, %esi
movzbl %dil, %ecx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0x4278
movb $0x1, %dl
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x41e6
movl $0xd, %edx
movq %r13, %rdi
leaq 0x1153(%rip), %rsi      # 0x5276
callq 0x2170
movq 0x90(%rbx), %rsi
movq %r13, %rdi
callq 0x20c0
movq %rax, %rbp
movl $0x1b, %edx
movq %rax, %rdi
leaq 0x14af(%rip), %rsi      # 0x55f8
jmp 0x41b7
movl $0xd, %edx
movq %r13, %rdi
leaq 0x111c(%rip), %rsi      # 0x5276
callq 0x2170
movq 0x90(%rbx), %rsi
movq %r13, %rdi
callq 0x20c0
movq %rax, %rbp
movl $0x1a, %edx
movq %rax, %rdi
leaq 0x1479(%rip), %rsi      # 0x55f9
jmp 0x41b7
movl $0xd, %edx
movq %r13, %rdi
leaq 0x10e5(%rip), %rsi      # 0x5276
callq 0x2170
movq 0x90(%rbx), %rsi
movq %r13, %rdi
callq 0x20c0
movq %rax, %rbp
movl $0x19, %edx
movq %rax, %rdi
leaq 0x145d(%rip), %rsi      # 0x5614
callq 0x2170
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
xorl %edx, %edx
testb %dl, %dl
jne 0x4031
jmp 0x4265
movb $0x1, %r14b
testb $0x1, 0xc(%rsp)
jne 0x4265
movq 0x3def(%rip), %r14      # 0x7ff0
leaq 0x106e(%rip), %rsi      # 0x5276
movl $0xd, %edx
movq %r14, %rdi
callq 0x2170
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20c0
movq %rax, %rbx
leaq 0x13b6(%rip), %rsi      # 0x55e4
movl $0x13, %edx
movq %rax, %rdi
callq 0x2170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop