// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
xorl %edx, %edx
callq 0x8410
movl %eax, 0x154(%rbx)
andl $0x0, 0x16c(%rbx)
testl %eax, %eax
js 0x9416
movq %rbx, %rdi
callq 0x8240
movq %rax, %r14
testq %rax, %rax
jne 0x9430
leaq 0x1119f(%rip), %rsi     # 0x1a59d
leaq 0x111c3(%rip), %rdx     # 0x1a5c8
xorl %edi, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1920b
jmp 0x9510
leaq 0x11180(%rip), %rsi     # 0x1a59d
leaq 0x1117e(%rip), %rdx     # 0x1a5a2
xorl %r14d, %r14d
xorl %edi, %edi
xorl %eax, %eax
callq 0x1920b
movl 0x154(%rbx), %r15d
testl %r15d, %r15d
js 0x9522
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)
pushq $0x10
popq %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x8390
testl %eax, %eax
js 0x94f9
movl 0x154(%rbx), %ebp
testl %ebp, %ebp
js 0x94c4
pushq $0x3
popq %rsi
movl %ebp, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x80f0
testl %eax, %eax
js 0x94c4
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %ebp, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x80f0
movl $0x1, 0x16c(%rbx)
leaq 0x104(%rbx), %rdx
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x92a9
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x8400
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x9342
xorl %ebp, %ebp
jmp 0x9522
leaq 0x1109d(%rip), %rsi     # 0x1a59d
leaq 0x110e5(%rip), %rdx     # 0x1a5ec
xorl %edi, %edi
xorl %eax, %eax
callq 0x1920b
movl 0x154(%rbx), %edi
callq 0x81d0
orl $-0x1, 0x154(%rbx)
movl %ebp, %eax
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq