// File: /workspace/github_cpp_makefile_star2_O1/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, 0x40(%rsp)
movq %rdi, %r14
movq (%rdi), %rax
callq *0x50(%rax)
movq $-0x1, %rbx
testb %al, %al
je 0x47eef
movl %ebp, %r13d
xorl %r15d, %r15d
leaq 0x20(%rsp), %rbp
movq %r13, %r12
subq %r15, %r12
jle 0x47eec
cmpq $0x0, 0xa0(%r14)
jne 0x47cc5
movq %r14, %rdi
movq %r12, %rsi
callq 0x290d0
testb %al, %al
je 0x47db1
movq 0xa0(%r14), %rdi
callq 0x26740
cmpl $0xce, %eax
je 0x47d12
cmpl $0xc8, %eax
jne 0x47d3a
movq 0x40(%rsp), %rax
leaq (%rax,%r15), %rsi
movq 0x30(%r14), %rdi
movl %r12d, %edx
callq 0x26bd0
testq %rax, %rax
js 0x47df1
je 0x47e2f
addq %rax, %r15
addq %rax, 0xb0(%r14)
jmp 0x47dea
movq 0xb0(%r14), %rax
cmpq 0xc0(%r14), %rax
jne 0x47dbd
movq %r14, %rdi
movq %r12, %rsi
callq 0x290d0
testb %al, %al
je 0x47db1
jmp 0x47ea6
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rbx
movq %rbx, %rdi
leaq 0xee05(%rip), %rsi      # 0x56b55
leaq 0xee0b(%rip), %rdx      # 0x56b62
callq 0x26750
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rbp, %rdi
leaq 0xedf3(%rip), %rsi      # 0x56b63
leaq 0xee0f(%rip), %rdx      # 0x56b86
callq 0x26750
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x288c0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x47d9e
callq 0x27560
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x47db1
callq 0x27560
movq $-0x1, %rbx
jmp 0x47eaa
movq 0x40(%rsp), %rax
leaq (%rax,%r15), %rsi
movq 0x30(%r14), %rdi
movl %r12d, %edx
callq 0x26bd0
testq %rax, %rax
js 0x47e36
movq %rax, %r12
je 0x47eb6
addq %r12, %r15
addq %r12, 0xb0(%r14)
movb $0x1, %al
jmp 0x47ea2
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rbx
movq %rbx, %rdi
leaq 0xed4e(%rip), %rsi      # 0x56b55
leaq 0xed54(%rip), %rdx      # 0x56b62
callq 0x26750
movq 0x30(%r14), %rsi
movq %rbp, %rdi
callq 0x27b50
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x288c0
jmp 0x47e72
xorl %eax, %eax
movq %r15, %rbx
jmp 0x47ea2
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %rbx
movq %rbx, %rdi
leaq 0xed09(%rip), %rsi      # 0x56b55
leaq 0xed0f(%rip), %rdx      # 0x56b62
callq 0x26750
movq 0x30(%r14), %rsi
movq %rbp, %rdi
callq 0x27b50
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x288c0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x47e86
callq 0x27560
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x47e99
callq 0x27560
movq $-0x1, %rbx
xorl %eax, %eax
testb %al, %al
je 0x47eaa
movb $0x1, %al
jmp 0x47eac
xorl %eax, %eax
testb %al, %al
jne 0x47c9c
jmp 0x47eef
movq 0xb8(%r14), %rcx
xorl %eax, %eax
testq %rcx, %rcx
jle 0x47e31
cmpq %rcx, 0xb0(%r14)
jge 0x47e31
movl $0x10000, %esi          # imm = 0x10000
movq %r14, %rdi
callq 0x290d0
testb %al, %al
jne 0x47de0
jmp 0x47e99
movq %r15, %rbx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x47f0d
jmp 0x47f26
jmp 0x47f3e
jmp 0x47f0d
jmp 0x47f26
jmp 0x47f3e
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x47f29
callq 0x27560
jmp 0x47f29
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x47f41
callq 0x27560
jmp 0x47f41
movq %rax, %rbx
movq %rbx, %rdi
callq 0x28c20
nop