// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdi, %rbx
movq (%rdi), %rax
callq *0x50(%rax)
testb %al, %al
je 0x47e3d
testl %ebp, %ebp
je 0x47d28
movl %ebp, %r13d
xorl %r14d, %r14d
movq %r13, %r12
subq %r14, %r12
movq 0xa0(%rbx), %rdi
testq %rdi, %rdi
jne 0x47c62
movq %rbx, %rdi
movq %r12, %rsi
callq 0x290c0
testb %al, %al
je 0x47e3d
movq 0xa0(%rbx), %rdi
callq 0x26740
cmpl $0xce, %eax
je 0x47c9c
cmpl $0xc8, %eax
jne 0x47d30
leaq (%r15,%r14), %rsi
movq 0x30(%rbx), %rdi
movl %r12d, %edx
callq 0x26bd0
testq %rax, %rax
js 0x47d97
movq %rax, %r12
jne 0x47d10
jmp 0x47e44
movq 0xb0(%rbx), %rax
cmpq 0xc0(%rbx), %rax
jne 0x47cc0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x290c0
testb %al, %al
jne 0x47d1a
jmp 0x47e3d
leaq (%r15,%r14), %rsi
movq 0x30(%rbx), %rdi
movl %r12d, %edx
callq 0x26bd0
testq %rax, %rax
js 0x47dda
movq %rax, %r12
jne 0x47d10
movq 0xb8(%rbx), %rax
testq %rax, %rax
jle 0x47e44
cmpq %rax, 0xb0(%rbx)
jge 0x47e44
movl $0x10000, %esi          # imm = 0x10000
movq %rbx, %rdi
callq 0x290c0
testb %al, %al
je 0x47e3d
addq %r12, %r14
addq %r12, 0xb0(%rbx)
cmpq %r13, %r14
jl 0x47c36
jmp 0x47e44
xorl %r14d, %r14d
jmp 0x47e44
leaq 0x18(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xee15(%rip), %rsi      # 0x56b55
leaq 0xee1b(%rip), %rdx      # 0x56b62
leaq 0x8(%rsp), %rdi
callq 0x26750
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xee02(%rip), %rsi      # 0x56b63
leaq 0xee1e(%rip), %rdx      # 0x56b86
leaq 0x28(%rsp), %rdi
callq 0x26750
leaq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
callq 0x288b0
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
jne 0x47e29
jmp 0x47e2e
leaq 0x18(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xedae(%rip), %rsi      # 0x56b55
leaq 0xedb4(%rip), %rdx      # 0x56b62
leaq 0x8(%rsp), %rdi
callq 0x26750
movq 0x30(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x27b40
leaq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
callq 0x288b0
jmp 0x47e1b
leaq 0x18(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xed6b(%rip), %rsi      # 0x56b55
leaq 0xed71(%rip), %rdx      # 0x56b62
leaq 0x8(%rsp), %rdi
callq 0x26750
movq 0x30(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x27b40
leaq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
callq 0x288b0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x47e2e
callq 0x27560
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x47e3d
callq 0x27560
movq $-0x1, %r14
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x47e5c
jmp 0x47e87
jmp 0x47e9b
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x47e80
jmp 0x47e8a
jmp 0x47e87
jmp 0x47e9b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x47e8a
callq 0x27560
jmp 0x47e8a
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x47e9e
callq 0x27560
jmp 0x47e9e
movq %rax, %rbx
movq %rbx, %rdi
callq 0x28c10