// File: /workspace/github_c_makefile_star2_O1/schroeder-lvb[P]sissm/src/rdrv.c
// Function: rdrvConnect
int rdrvConnect( rdrvObj *rPtr )
{
    struct hostent *server = NULL;
    int errCode = 1;
    char buf[BUFSIZE_T];

    // Create a socket
    //
#ifdef _WIN32
    rPtr->sockfd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
#else 
    rPtr->sockfd = socket( AF_INET, SOCK_STREAM, 0 );
#endif
    rPtr->isConnected = 0;
    if ( rPtr->sockfd < 0 ) {
        logPrintf(LOG_LEVEL_CRITICAL, "rdrv", "Critical Error, socket creation error");
    }
    else {
        // gethostbyname: get the server's DNS entry 
        // 
        server = gethostbyname( rPtr->hostName );
        if ( server == NULL ) {
            logPrintf(LOG_LEVEL_CRITICAL, "rdrv", "Critical Error, no such host ::%s::", rPtr->hostName );
#ifdef _WIN32
            closesocket( rPtr->sockfd );
#else
            close( rPtr->sockfd );
#endif
            rPtr->sockfd = -1;
        }
    }

    // Connect to the server
    //
    if ( rPtr->sockfd >= 0 ) {

        // Build the Internet address
        // 
        // bzero( (char *) &rPtr->serveraddr, sizeof( rPtr->serveraddr) );
        memset( (char *) &rPtr->serveraddr, 0, sizeof( rPtr->serveraddr) );
        rPtr->serveraddr.sin_family = AF_INET;
        // bcopy( (char *) server->h_addr, (char *) &rPtr->serveraddr.sin_addr.s_addr, server->h_length );
        memcpy( (char *) &rPtr->serveraddr.sin_addr.s_addr, (char *) server->h_addr, server->h_length );
        rPtr->serveraddr.sin_port = htons( rPtr->portNo );

        if ( connect( rPtr->sockfd, (struct sockaddr *) &rPtr->serveraddr, sizeof( rPtr->serveraddr ) ) < 0) {
            logPrintf(LOG_LEVEL_CRITICAL, "rdrv", "Warning: unable to connect to host via TCP/IP");
#ifdef _WIN32
            closesocket( rPtr->sockfd );
#else
            close( rPtr->sockfd );
#endif
            rPtr->sockfd = -1;
        }
        else {
            SetSocketBlockingEnabled( rPtr->sockfd, 0 );
            rPtr->isConnected = 1;
            errCode = 0;
        }
    }

    // Send RCON Password to the server
    //
    if (0 == errCode ) {
        rdrvSend( rPtr, 3, rPtr->rconPassword );
        usleep( RDRV_DELAY_PASSWORD );
        rdrvReceive( rPtr, buf );
    }

    return( errCode );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movq %rdi, %rbx
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x8400
movl %eax, 0x154(%rbx)
movl $0x0, 0x16c(%rbx)
testl %eax, %eax
js 0x94d9
movq %rbx, %rdi
callq 0x8240
movq %rax, %r14
testq %rax, %rax
jne 0x94f3
leaq 0x120f4(%rip), %rsi     # 0x1b5a0
leaq 0x12118(%rip), %rdx     # 0x1b5cb
xorl %r14d, %r14d
xorl %edi, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1a227
movl 0x154(%rbx), %edi
callq 0x81d0
movl $0xffffffff, 0x154(%rbx) # imm = 0xFFFFFFFF
jmp 0x94f3
leaq 0x120c0(%rip), %rsi     # 0x1b5a0
leaq 0x120be(%rip), %rdx     # 0x1b5a5
xorl %r14d, %r14d
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a227
movl 0x154(%rbx), %r15d
movl $0x1, %ebp
testl %r15d, %r15d
js 0x95a3
leaq 0x158(%rbx), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x158(%rbx)
movw $0x2, 0x158(%rbx)
leaq 0x15c(%rbx), %rdi
movq 0x18(%r14), %rax
movq (%rax), %rsi
movslq 0x14(%r14), %rdx
callq 0x8290
movzwl 0x100(%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x15a(%rbx)
movl %r15d, %edi
movq %r12, %rsi
movl $0x10, %edx
callq 0x8380
testl %eax, %eax
js 0x95a8
movl 0x154(%rbx), %ebp
testl %ebp, %ebp
js 0x9592
movl %ebp, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x80f0
testl %eax, %eax
js 0x9592
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x80f0
movl $0x1, 0x16c(%rbx)
xorl %ebp, %ebp
movb $0x1, %r14b
jmp 0x95d7
xorl %r14d, %r14d
jmp 0x95d7
leaq 0x11ff1(%rip), %rsi     # 0x1b5a0
leaq 0x12039(%rip), %rdx     # 0x1b5ef
xorl %r14d, %r14d
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a227
movl 0x154(%rbx), %edi
callq 0x81d0
movl $0xffffffff, 0x154(%rbx) # imm = 0xFFFFFFFF
testb %r14b, %r14b
je 0x9605
leaq 0x104(%rbx), %rdx
movq %rbx, %rdi
movl $0x3, %esi
callq 0x935a
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x83f0
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x93f4
movl %ebp, %eax
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq