// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movl %esi, %ebp
movq %rdi, %r14
callq 0x2220
testq %rax, %rax
je 0x4c98
movw $0x2, 0x20(%rsp)
movl %ebp, 0x1c(%rsp)
movl %ebp, %ecx
rolw $0x8, %cx
movw %cx, 0x22(%rsp)
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %edi
movl %edi, 0x24(%rsp)
movq $0x0, 0x28(%rsp)
callq 0x20e0
movl $0x32, %edx
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5804
leaq 0x10829(%rip), %rax     # 0x152c0
cmpl $0x1, (%rax)
jne 0x4cd1
movq 0x10(%rsp), %rdi
callq 0x66d4
movq %rax, %r14
movq %rdx, %r10
movl $0xff, %eax
cmpl %eax, %r14d
sete %sil
movq %r14, %rcx
shrq $0x20, %rcx
movl $0xff, %edx
cmpq %rdx, %rcx
sete %dil
orb %sil, %dil
movb %dil, 0xf(%rsp)
cmpl %eax, %r10d
sete %al
movq %r10, %r13
shrq $0x20, %r13
cmpq %rdx, %r13
sete %dl
orb %al, %dl
movb %dl, 0xe(%rsp)
movb $0x1, %bpl
leaq 0x7906(%rip), %r12      # 0xc400
leaq 0x7c1f(%rip), %rax      # 0xc720
addq $0xc, %rax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movq %r15, %r9
shlq $0x4, %r9
movl (%r12,%r9), %esi
cmpl $0xff, %esi
je 0x4c6c
addq %r12, %r9
movl 0x4(%r9), %edx
cmpl $0xff, %edx
je 0x4c6c
movl 0x8(%r9), %eax
cmpl $0xff, %eax
je 0x4c6c
testq %r13, %r13
sete %dil
testl %r14d, %r14d
sete %r8b
movl 0xc(%r9), %r11d
cmpl $0xff, %r11d
sete %r9b
orb %dil, %r8b
orb 0xf(%rsp), %r8b
orb 0xe(%rsp), %r8b
orb %r9b, %r8b
testb $0x1, %r8b
jne 0x4c6c
cmpl %r14d, %esi
setne %dil
testl %esi, %esi
setne %sil
testb %sil, %dil
jne 0x4c6c
cmpl %ecx, %edx
setne %sil
testl %edx, %edx
setne %dl
testb %dl, %sil
jne 0x4c6c
cmpl %r10d, %eax
setne %dl
testl %eax, %eax
setne %al
testb %al, %dl
jne 0x4c6c
cmpl %r13d, %r11d
setne %al
testl %r11d, %r11d
setne %dl
testb %dl, %al
jne 0x4c6c
movb $0x1, %bl
movq 0x40(%rsp), %rax
xorl %edi, %edi
movl -0xc(%rax), %edx
cmpl %r14d, %edx
setne %sil
testl %edx, %edx
setne %dl
testb %dl, %sil
jne 0x4c1d
movl -0x8(%rax), %edx
cmpl %ecx, %edx
setne %sil
testl %edx, %edx
setne %dl
testb %dl, %sil
jne 0x4c1d
movl -0x4(%rax), %edx
cmpl %r10d, %edx
setne %sil
testl %edx, %edx
setne %dl
testb %dl, %sil
jne 0x4c1d
movl (%rax), %edx
cmpl %r13d, %edx
je 0x4c37
testl %edx, %edx
je 0x4c37
cmpq $0x31, %rdi
leaq 0x1(%rdi), %rdx
setb %bl
addq $0x10, %rax
movq %rdx, %rdi
cmpq $0x32, %rdx
jne 0x4bd7
jmp 0x4cb3
movl $0x3, %edi
leaq 0x3ca8(%rip), %rsi      # 0x88eb
movl %r14d, %edx
movl %r10d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
movq %r10, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
callq 0x352c
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r10
testb $0x1, %bl
je 0x4cb3
cmpq $0x31, %r15
leaq 0x1(%r15), %rax
setb %bpl
movq %rax, %r15
cmpq $0x32, %rax
jne 0x4b0d
leaq 0x3b81(%rip), %rsi      # 0x880d
movl $0x2, %edi
movq 0x10(%rsp), %rdx
jmp 0x4ca7
leaq 0x3b35(%rip), %rsi      # 0x87d4
movl $0x2, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x352c
jmp 0x4d42
leaq 0x3bf9(%rip), %rsi      # 0x88b3
movl $0x3, %edi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x352c
testb $0x1, %bpl
je 0x4c85
movzwl 0x1c(%rsp), %ecx
leaq 0x3b69(%rip), %rsi      # 0x8846
movl $0x3, %edi
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x352c
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, %ebp
leaq 0x20(%rsp), %rsi
movl %eax, %edi
callq 0x36d3
testl %eax, %eax
je 0x4d47
leaq 0x3b5d(%rip), %rsi      # 0x8878
movl $0x2, %edi
xorl %eax, %eax
callq 0x352c
movl %ebp, %edi
callq 0x2360
leaq 0x38a5(%rip), %rsi      # 0x85da
movl $0x32, %edx
movq %rbx, %rdi
callq 0x5804
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq