// File: lukasz-wiecaszek[P]h264iframedecoder/h264_parser.cpp
// Function: ymn::h264_parser::parse_nal_unit(ymn::istream<0>&)
h264_parser_status_e h264_parser::parse_nal_unit(istream_be& s)
{
    uint8_t nal_header;
    uint32_t nal_ref_idc;
    uint32_t nal_unit_type;
    bool status;

    status = s.read_u8(nal_header);
    if (!status)
        return h264_parser_status_e::NAL_UNIT_CORRUPTED;

    nal_ref_idc = (nal_header & 0x60) >> 5;
    nal_unit_type = nal_header & 0x1f;

    std::cout << std::endl;
    std::cout << "nal_unit_type: " << nal_unit_type;
    std::cout << " '" << ymn::to_string(static_cast<nal_unit_type_e>(nal_unit_type)) << "'";
    std::cout << " size: " << s.size();
    std::cout << std::endl;

    switch (static_cast<nal_unit_type_e>(nal_unit_type)) {
        case nal_unit_type_e::AUD:
            return parse_aud(s);

        case nal_unit_type_e::SPS:
            return parse_sps(s);

        case nal_unit_type_e::PPS:
            return parse_pps(s);

        case nal_unit_type_e::SEI:
            return parse_sei(s);

        case nal_unit_type_e::CODED_SLICE_NON_IDR_PICTURE:
        case nal_unit_type_e::CODED_SLICE_IDR_PICTURE:
            return parse_slice_header(s, nal_ref_idc, nal_unit_type);

        default:
            break;
    }

    return h264_parser_status_e::NAL_UNIT_SKIPPED;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x3f(%rsp), %rsi
callq 0x17540
andb $0x1, %al
movb %al, 0x33(%rsp)
testb $0x1, 0x33(%rsp)
jne 0x16a11
movl $0x2, 0x54(%rsp)
jmp 0x16b81
movzbl 0x3f(%rsp), %eax
andl $0x60, %eax
shrl $0x5, %eax
movl %eax, 0x38(%rsp)
movzbl 0x3f(%rsp), %eax
andl $0x1f, %eax
movl %eax, 0x34(%rsp)
movq 0x1758d(%rip), %rdi     # 0x2dfc0
movq %rdi, 0x10(%rsp)
movq 0x17569(%rip), %rsi     # 0x2dfa8
movq %rsi, 0x18(%rsp)
callq 0x2180
movq 0x10(%rsp), %rdi
leaq 0xeda3(%rip), %rsi      # 0x257f8
callq 0x2140
movq %rax, %rdi
movl 0x34(%rsp), %esi
callq 0x2220
movq 0x10(%rsp), %rdi
leaq 0x116cd(%rip), %rsi     # 0x2813f
callq 0x2140
movq %rax, (%rsp)
movl 0x34(%rsp), %edi
callq 0x12000
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x2140
movq %rax, %rdi
leaq 0x116a6(%rip), %rsi     # 0x28140
callq 0x2140
movq 0x10(%rsp), %rdi
leaq 0xed5d(%rip), %rsi      # 0x25808
callq 0x2140
movq %rax, 0x8(%rsp)
movq 0x40(%rsp), %rdi
callq 0x17be0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x2070
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x2180
movl 0x34(%rsp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, 0x20(%rsp)
subl $0x8, %eax
ja 0x16b77
movq 0x20(%rsp), %rax
leaq 0xe747(%rip), %rcx      # 0x25244
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x13a50
movl %eax, 0x54(%rsp)
jmp 0x16b81
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x13b30
movl %eax, 0x54(%rsp)
jmp 0x16b81
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x14560
movl %eax, 0x54(%rsp)
jmp 0x16b81
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x152b0
movl %eax, 0x54(%rsp)
jmp 0x16b81
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl 0x38(%rsp), %edx
movl 0x34(%rsp), %ecx
callq 0x154a0
movl %eax, 0x54(%rsp)
jmp 0x16b81
jmp 0x16b79
movl $0x1, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw (%rax,%rax)