// File: lukasz-wiecaszek[P]h264iframedecoder/./istream.hpp
// Function: ymn::istream<0>::read_bits(unsigned int, unsigned int&)
bool read_bits(uint32_t number_of_bits, uint32_t& value)
    {
        assert(number_of_bits <= 32);

        /* verify whether we don't go beyond size of the stream */
        if ((m_position.to_bits() + number_of_bits) <= (m_size * BITS_PER_BYTE)) {

            uint32_t v = 0;
            uint32_t bits_remaining = number_of_bits;
            uint32_t bits_to_read;

            while (bits_remaining > 0) {
                /* there is more bits to be read than bits left in current byte */
                if ((m_position.m_bit_position + bits_remaining) >= BITS_PER_BYTE ) {
                    /* number of bits left in current byte */
                    bits_to_read = BITS_PER_BYTE - m_position.m_bit_position;

                    /* shift the result by number of bits to be read
                    to make room for new data */
                    v <<= bits_to_read;
                    /* add to the result value */
                    v |= m_buffer[m_position.m_byte_position] & ((1U << bits_to_read) - 1);

                    /* move to the next byte in the stream */
                    m_position.m_byte_position++;
                    /* and reset bit position to the begining of a byte */
                    m_position.m_bit_position = 0;
                }
                else {
                    /* there remains less than 8 bits to be read */
                    bits_to_read = bits_remaining;

                    /* shift the result by number of bits to be read
                    to make room for new data */
                    v <<= bits_to_read;

                    /* advance bit position */
                    m_position.m_bit_position += bits_to_read;

                    /* add to the result value */
                    v |= (m_buffer[m_position.m_byte_position] >> (BITS_PER_BYTE - m_position.m_bit_position)) & ((1U << bits_to_read) - 1);
                }

                /* decrease number of remaining bits by number of bits read in this iteration */
                bits_remaining -= bits_to_read;
          }

          value = v;
          return true;
       }
       else {
            m_status |= ISTREAM_STATUS_EOS_REACHED;
            if (m_error_function)
                m_error_function(m_position, m_position + number_of_bits);
            return false;
       }
    }

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movl %esi, 0x54(%rsp)
movq %rdx, 0x48(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpl $0x20, 0x54(%rsp)
ja 0x16f55
jmp 0x16f74
leaq 0xe8c4(%rip), %rdi      # 0x25820
leaq 0xe8d2(%rip), %rsi      # 0x25835
movl $0x1a4, %edx            # imm = 0x1A4
leaq 0xe921(%rip), %rcx      # 0x25890
callq 0x20f0
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
callq 0x17fc0
movq 0x20(%rsp), %rcx
movl 0x54(%rsp), %edx
addq %rdx, %rax
movq 0x8(%rcx), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
ja 0x170af
movl $0x0, 0x44(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x40(%rsp)
cmpl $0x0, 0x40(%rsp)
jbe 0x1709d
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x40(%rsp), %ecx
addq %rcx, %rax
cmpq $0x8, %rax
jb 0x17033
movq 0x20(%rsp), %rax
movl $0x8, %ecx
subq 0x18(%rax), %rcx
movl %ecx, 0x3c(%rsp)
movl 0x3c(%rsp), %ecx
movl 0x44(%rsp), %edx
shll %cl, %edx
movl %edx, %ecx
movl %ecx, 0x44(%rsp)
movq (%rax), %rcx
movq 0x10(%rax), %rdx
movzbl (%rcx,%rdx), %ecx
movl %ecx, 0x1c(%rsp)
movl 0x3c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl 0x1c(%rsp), %ecx
subl $0x1, %edx
andl %edx, %ecx
orl 0x44(%rsp), %ecx
movl %ecx, 0x44(%rsp)
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq $0x0, 0x18(%rax)
jmp 0x1708a
movq 0x20(%rsp), %rdx
movl 0x40(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %ecx
movl 0x44(%rsp), %eax
shll %cl, %eax
movl %eax, 0x44(%rsp)
movl 0x3c(%rsp), %eax
addq 0x18(%rdx), %rax
movq %rax, 0x18(%rdx)
movq (%rdx), %rax
movq 0x10(%rdx), %rcx
movzbl (%rax,%rcx), %eax
movl $0x8, %ecx
subq 0x18(%rdx), %rcx
sarl %cl, %eax
movl 0x3c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
andl %ecx, %eax
orl 0x44(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x3c(%rsp), %ecx
movl 0x40(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x40(%rsp)
jmp 0x16faf
movl 0x44(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, (%rax)
movb $0x1, 0x67(%rsp)
jmp 0x1711b
movq 0x20(%rsp), %rdi
movl 0x20(%rdi), %eax
orl $0x1, %eax
movl %eax, 0x20(%rdi)
addq $0x28, %rdi
callq 0x17fe0
testb $0x1, %al
jne 0x170cc
jmp 0x17116
movq 0x20(%rsp), %rdi
movq %rdi, %rax
addq $0x28, %rax
movq %rax, 0x8(%rsp)
movq %rdi, %rax
addq $0x10, %rax
movq %rax, 0x10(%rsp)
addq $0x10, %rdi
movl 0x54(%rsp), %eax
movl %eax, %esi
callq 0x18050
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rax, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
leaq 0x28(%rsp), %rdx
callq 0x18000
movb $0x0, 0x67(%rsp)
movb 0x67(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)