// File: /workspace/github_cpp_makefile_star2_O1/qian-jiahong[P]uvncrep017-ws/repeater.cpp
// Function: openConnectionToVncServer(char const*, unsigned short, char*)
static int openConnectionToVncServer(const char *host, unsigned short port, char *serverIp)
{
    int s;
    struct sockaddr_in saddr;
    struct hostent *h;
        
    h = gethostbyname(host);
    if (NULL == h) {
        debug(LEVEL_2, "openConnectionToVncServer(): can't resolve hostname: %s\n", host);
        return -1;
    }
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(port);
    
    //Interesting;-) typecast / indirection thing copied from "Beej's Guide to network programming". 
    //See http://beej.us/guide/bgnet/ for more info
    saddr.sin_addr = *((struct in_addr *)h->h_addr);

    memset(&(saddr.sin_zero), '\0', 8); // zero the rest of the struct
    
    strlcpy(serverIp, inet_ntoa(saddr.sin_addr), MAX_IP_LEN);
    
    //Check server addresses against list of allowed addresses / ranges
    if (requireListedServer == 1) {
        if (!isServerAddressAllowed(serverIp)) {
            debug(LEVEL_2, "openConnectionToVncServer(): server is not allowed (%s)\n", serverIp);
            return -1;
        }
    }
    
    debug(LEVEL_3, "openConnectionToVncServer(): connecting to %s:%u\n", serverIp, port);

    s = socket(AF_INET, SOCK_STREAM, 0);
    
    //Trying to connect with timeout
    if (connectWithTimeout(s, (struct sockaddr *) &saddr, sizeof(saddr), TIMEOUT_10SECS) != 0) {
        debug(LEVEL_2, "openConnectionToVncServer(): connectWithTimeout() failed.\n");
        close(s);
        strlcpy(serverIp, "", MAX_IP_LEN);
        return -1;
    }
    else
        return s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movl %esi, %ebx
movq %rdi, %r14
callq 0x2220
testq %rax, %rax
je 0x4ccf
movw $0x2, 0x18(%rsp)
movl %ebx, %ecx
rolw $0x8, %cx
movw %cx, 0x1a(%rsp)
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %edi
movl %edi, 0x1c(%rsp)
movq $0x0, 0x20(%rsp)
callq 0x20e0
movl $0x32, %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x5844
leaq 0x107f8(%rip), %rax     # 0x152c0
cmpl $0x1, (%rax)
jne 0x4d02
movl %ebx, 0xc(%rsp)
movq %r15, %rdi
callq 0x6754
movq %rax, %r14
movq %rdx, %r8
movl $0xff, %eax
cmpl %eax, %r14d
sete %cl
movq %r14, %r10
shrq $0x20, %r10
movl $0xff, %edx
cmpq %rdx, %r10
sete %sil
orb %cl, %sil
cmpl %eax, %r8d
sete %al
movq %r8, %r9
shrq $0x20, %r9
cmpq %rdx, %r9
sete %bpl
orb %al, %bpl
orb %sil, %bpl
testl %r14d, %r14d
sete %al
testq %r9, %r9
sete %r13b
orb %al, %r13b
movb $0x1, %bl
leaq 0x78c9(%rip), %rdi      # 0xc400
leaq 0x7be2(%rip), %r11      # 0xc720
addq $0xc, %r11
xorl %r12d, %r12d
movq %r12, %rcx
shlq $0x4, %rcx
leaq (%rdi,%rcx), %rax
movl (%rdi,%rcx), %ecx
cmpl $0xff, %ecx
je 0x4b7a
cmpl $0xff, 0x4(%rax)
je 0x4b7a
cmpl $0xff, 0x8(%rax)
je 0x4b7a
cmpl $0xff, 0xc(%rax)
jne 0x4c76
movb $0x1, %dl
orb %bpl, %dl
orb %r13b, %dl
testb $0x1, %dl
jne 0x4c58
cmpl %r14d, %ecx
setne %dl
testl %ecx, %ecx
setne %cl
testb %cl, %dl
jne 0x4c58
movl 0x4(%rax), %ecx
cmpl %r10d, %ecx
setne %dl
testl %ecx, %ecx
setne %cl
testb %cl, %dl
jne 0x4c58
movl 0x8(%rax), %ecx
cmpl %r8d, %ecx
setne %dl
testl %ecx, %ecx
setne %cl
testb %cl, %dl
jne 0x4c58
movl 0xc(%rax), %eax
cmpl %r9d, %eax
setne %cl
testl %eax, %eax
setne %al
testb %al, %cl
jne 0x4c58
movq %r15, 0x40(%rsp)
movb $0x1, %r15b
movq %r11, %rax
xorl %ecx, %ecx
movl -0xc(%rax), %edx
cmpl %r14d, %edx
setne %sil
testl %edx, %edx
setne %dl
testb %dl, %sil
jne 0x4c30
movl -0x8(%rax), %edx
cmpl %r10d, %edx
setne %sil
testl %edx, %edx
setne %dl
testb %dl, %sil
jne 0x4c30
movl -0x4(%rax), %edx
cmpl %r8d, %edx
setne %sil
testl %edx, %edx
setne %dl
testb %dl, %sil
jne 0x4c30
movl (%rax), %edx
cmpl %r9d, %edx
je 0x4c7d
testl %edx, %edx
je 0x4c7d
cmpq $0x31, %rcx
leaq 0x1(%rcx), %rdx
setb %r15b
addq $0x10, %rax
movq %rdx, %rcx
cmpq $0x32, %rdx
jne 0x4be9
testb $0x1, %r15b
movq 0x40(%rsp), %r15
je 0x4ce3
cmpq $0x31, %r12
leaq 0x1(%r12), %rax
setb %bl
movq %rax, %r12
cmpq $0x32, %rax
jne 0x4b45
jmp 0x4cf9
xorl %edx, %edx
jmp 0x4b7c
movl $0x3, %edi
leaq 0x3c62(%rip), %rsi      # 0x88eb
movl %r14d, %edx
movl %r10d, %ecx
movq %r8, 0x10(%rsp)
movq 0x10(%rsp), %r8
movq %r10, 0x38(%rsp)
xorl %eax, %eax
movq %r9, 0x30(%rsp)
movq %r11, 0x28(%rsp)
callq 0x355c
movq 0x28(%rsp), %r11
leaq 0x7745(%rip), %rdi      # 0xc400
movq 0x30(%rsp), %r9
movq 0x38(%rsp), %r10
movq 0x10(%rsp), %r8
jmp 0x4c49
leaq 0x3afe(%rip), %rsi      # 0x87d4
movl $0x2, %edi
movq %r14, %rdx
jmp 0x4d7d
leaq 0x3bc9(%rip), %rsi      # 0x88b3
movl $0x3, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x355c
testb $0x1, %bl
movl 0xc(%rsp), %ebx
je 0x4d6e
movzwl %bx, %ecx
leaq 0x3b3a(%rip), %rsi      # 0x8846
movl $0x3, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x355c
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, %ebp
leaq 0x18(%rsp), %rsi
movl %eax, %edi
callq 0x3703
testl %eax, %eax
je 0x4d89
leaq 0x3b33(%rip), %rsi      # 0x8878
movl $0x2, %edi
xorl %eax, %eax
callq 0x355c
movl %ebp, %edi
callq 0x2360
leaq 0x387b(%rip), %rsi      # 0x85da
movl $0x32, %edx
movq %r15, %rdi
callq 0x5844
jmp 0x4d84
leaq 0x3a98(%rip), %rsi      # 0x880d
movl $0x2, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x355c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq