// File: /workspace/github_cpp_makefile_star2_O2/zeeev[P]DASVC/bamtools/src/api/internal/io/BamHttp_p.cpp
// Function: BamTools::Internal::BamHttp::Read(char*, unsigned int)
int64_t BamHttp::Read(char* data, const unsigned int numBytes) {

    // if BamHttp not in a valid state
    if ( !IsOpen() )
        return -1;

    int64_t numBytesReadSoFar = 0;
    while ( numBytesReadSoFar < numBytes ) {

        const size_t remaining = static_cast<size_t>( numBytes - numBytesReadSoFar );

        // if we're not holding a valid GET reponse, get one
        if ( m_response == 0 ) {
            if ( !SendGetRequest(remaining) )
                return -1;
        }
        BT_ASSERT_X(m_response, "null HTTP response");

        // check response status code
        const int statusCode = m_response->GetStatusCode();

        // if we receieved full file contents in response
        if ( statusCode == 200 ) {

            // try to read 'remaining' bytes from socket
            const int64_t socketBytesRead = ReadFromSocket(data+numBytesReadSoFar, remaining);

            // if error
            if ( socketBytesRead < 0 ) {
                SetErrorString("BamHttp::Read", m_socket->GetErrorString());
                return -1;
            }

            // EOF
            else if ( socketBytesRead == 0 )
                return numBytesReadSoFar;

            // update counters
            numBytesReadSoFar += socketBytesRead;
            m_filePosition    += socketBytesRead;

        }

        // else if we received a range of bytes in response
        else if ( statusCode == 206 ) {

            // if we've exhausted the last request
            if ( m_filePosition == m_rangeEndPosition ) {
                if ( !SendGetRequest(remaining) )
                    return -1;
            }

            else {

                // try to read 'remaining' bytes from socket
                const int64_t socketBytesRead = ReadFromSocket(data+numBytesReadSoFar, remaining);

                // if error
                if ( socketBytesRead < 0 ) {
                    SetErrorString("BamHttp::Read", m_socket->GetErrorString());
                    return -1;
                }

                // maybe EOF
                else if ( socketBytesRead == 0 ) {

                    // if we know we're not at end position, fire off a new request
                    if ( m_fileEndPosition > 0 && m_filePosition < m_fileEndPosition ) {
                        if ( !SendGetRequest() )
                            return -1;
                    } else
                        return numBytesReadSoFar;
                }

                // update counters
                numBytesReadSoFar += socketBytesRead;
                m_filePosition    += socketBytesRead;
            }
        }


        // else some other HTTP status
        else {
            SetErrorString("BamHttp::Read", "unsupported status code in response");
            return -1;
        }
    }

    // return actual number of bytes read
    return numBytesReadSoFar;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %rax
callq *0x50(%rax)
testb %al, %al
je 0x4f464
movl %ebp, %ebp
xorl %r13d, %r13d
pushq $-0x1
popq %r12
movq %rbp, %r15
subq %r13, %r15
jle 0x4f4cd
movq 0xa0(%rbx), %rdi
testq %rdi, %rdi
jne 0x4f39b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x35560
testb %al, %al
je 0x4f4bb
movq 0xa0(%rbx), %rdi
callq 0x31b00
cmpl $0xce, %eax
jne 0x4f3dd
movq 0xb0(%rbx), %rax
cmpq 0xc0(%rbx), %rax
jne 0x4f40a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x35560
movl %eax, %ecx
movq %r12, %rax
testb %cl, %cl
cmovneq %r13, %rax
je 0x4f4be
cmpq %r13, %rbp
jg 0x4f375
jmp 0x4f4be
cmpl $0xc8, %eax
jne 0x4f469
leaq (%r14,%r13), %rsi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x326b0
testq %rax, %rax
js 0x4f4d2
movq %rax, %r15
jne 0x4f455
jmp 0x4f4cd
leaq (%r14,%r13), %rsi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x326b0
testq %rax, %rax
js 0x4f51e
movq %rax, %r15
jne 0x4f455
movq 0xb8(%rbx), %rax
testq %rax, %rax
jle 0x4f4cd
cmpq %rax, 0xb0(%rbx)
jge 0x4f4cd
movl $0x10000, %esi          # imm = 0x10000
movq %rbx, %rdi
callq 0x35560
testb %al, %al
je 0x4f4bb
addq %r15, %r13
addq %r15, 0xb0(%rbx)
jmp 0x4f369
pushq $-0x1
popq %rax
jmp 0x4f4be
leaq 0xd714(%rip), %rsi      # 0x5cb84
leaq 0x28(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x31b30
leaq 0xd70c(%rip), %rsi      # 0x5cb92
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x31b30
leaq 0x28(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x34ab0
leaq 0x8(%rsp), %rdi
callq 0x33a00
leaq 0x28(%rsp), %rdi
callq 0x33a00
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rax
jmp 0x4f4be
leaq 0xd6ab(%rip), %rsi      # 0x5cb84
leaq 0x28(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x31b30
movq 0x30(%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0x337f0
leaq 0x28(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x34ab0
leaq 0x8(%rsp), %rdi
callq 0x33a00
leaq 0x28(%rsp), %rdi
callq 0x33a00
jmp 0x4f4bb
leaq 0xd65f(%rip), %rsi      # 0x5cb84
leaq 0x28(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x31b30
movq 0x30(%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0x337f0
leaq 0x28(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x34ab0
leaq 0x8(%rsp), %rdi
callq 0x33a00
leaq 0x28(%rsp), %rdi
callq 0x33a00
jmp 0x4f4bb
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x33a00
jmp 0x4f590
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x33a00
jmp 0x4f5a1
jmp 0x4f5bc
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x33a00
jmp 0x4f5ce
jmp 0x4f5bc
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x33a00
jmp 0x4f5ce
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x33a00
jmp 0x4f5c4
movq %rax, %rbx
jmp 0x4f5ce
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x33a00
movq %rbx, %rdi
callq 0x34ed0
movq %rax, %rdi
callq 0x39b2f