// File: /workspace/github_cpp_makefile_star2_O1/qian-jiahong[P]uvncrep017-ws/repeater.cpp
// Function: connectWithTimeout(int, sockaddr const*, unsigned int, int)
static int connectWithTimeout(int socket, const struct sockaddr *addr, socklen_t addrlen, int timeOutSecs)
{
    int res;
    long arg; 
    fd_set myset; 
    struct timeval tv; 
    int valopt;     
    socklen_t lon;
    
    //First, set socket non-blocking 
    arg = fcntl(socket, F_GETFL, NULL);
    if (arg < 0) { 
        debug(LEVEL_2, "connectWithTimeout(): error in fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
        return -1; 
    } 
    arg |= O_NONBLOCK; 
    if (fcntl(socket, F_SETFL, arg) < 0) { 
        debug(LEVEL_2, "connectWithTimeout(): error in fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
        return -1; 
    }     

    //Try to connect with timeout 
    res = connect(socket, addr, addrlen); 
    if (res < 0) { 
        if (errno == EINPROGRESS) { 
            debug(LEVEL_3, "connectWithTimeout(): EINPROGRESS in connect() - selecting\n"); 
        
            do { 
                tv.tv_sec = timeOutSecs; 
                tv.tv_usec = 0; 
                
                FD_ZERO(&myset); 
                FD_SET(socket, &myset); 
                
                res = select(socket + 1, NULL, &myset, NULL, &tv); 
           
                if ((res < 0) && (errno != EINTR)) { 
                    debug(LEVEL_3, "connectWithTimeout(): Error connecting %d (%s)\n", errno, strerror(errno)); 
                    return -1; 
                } 
                else if (res > 0) { 
                    // Socket selected for write, check if connection was succesful 
                    lon = sizeof(int); 
                    
                    if (getsockopt(socket, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) { 
                        debug(LEVEL_2, "connectWithTimeout(): Error in getsockopt() %d (%s)\n", errno, strerror(errno)); 
                        return -1; 
                    } 
              
                    // Check the value returned... 
                    if (valopt) { 
                        debug(LEVEL_2, "connectWithTimeout(): Error in delayed connection() %d (%s)\n", 
                            valopt, strerror(valopt)); 
                        return -1; 
                    } 
                    else {
                        debug(LEVEL_3, "connectWithTimeout(): connected OK\n"); 
                        break;
                    } 
                } 
                else { 
                    debug(LEVEL_3, "connectWithTimeout(): Timeout in select() - Cancelling!\n"); 
                    return -1; 
                } 
            } while (1); 
        } 
        else { 
            debug(LEVEL_3, "connectWithTimeout(): Error connecting %d (%s)\n", errno, strerror(errno)); 
            return -1; 
        } 
    } 
  
    //Set to blocking mode again... 
    if ((arg = fcntl(socket, F_GETFL, NULL)) < 0) { 
        debug(LEVEL_2, "connectWithTimeout(): Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
        return -1; 
    } 
    arg &= (~O_NONBLOCK); 
    if (fcntl(socket, F_SETFL, arg) < 0) { 
        debug(LEVEL_2, "connectWithTimeout(): Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
        return -1; 
    } 
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0xa0, %rsp
movq %rsi, %r14
movl %edi, %ebx
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x37af
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
movl $0x4, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x37c7
movl %ebx, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x21b0
testl %eax, %eax
js 0x37df
xorl %r14d, %r14d
movl %ebx, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x38d2
andl $0x7ffff7ff, %eax       # imm = 0x7FFFF7FF
movl %ebx, %edi
movl $0x4, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
jns 0x38fa
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3e5a(%rip), %rsi      # 0x7604
jmp 0x38e5
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3c5f(%rip), %rsi      # 0x7421
jmp 0x38e5
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3c80(%rip), %rsi      # 0x745a
jmp 0x38e5
callq 0x2030
movl (%rax), %ebp
cmpl $0x73, %ebp
jne 0x3909
movq %rax, %r14
leaq 0x3c9a(%rip), %rsi      # 0x7493
movl $0x3, %edi
xorl %eax, %eax
callq 0x355c
leaq 0x10(%rsp), %r8
movq $0xa, (%r8)
movq $0x0, 0x8(%r8)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x30(%rdx)
movaps %xmm0, 0x40(%rdx)
movaps %xmm0, 0x50(%rdx)
leal 0x3f(%rbx), %eax
testl %ebx, %ebx
cmovnsl %ebx, %eax
movl %eax, %esi
andl $-0x40, %esi
movl %ebx, %ecx
subl %esi, %ecx
movl $0x1, %esi
shlq %cl, %rsi
movaps %xmm0, 0x60(%rdx)
movaps %xmm0, 0x70(%rdx)
sarl $0x6, %eax
cltq
orq %rsi, 0x20(%rsp,%rax,8)
leal 0x1(%rbx), %edi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x21d0
testl %eax, %eax
jns 0x387f
movl (%r14), %ebp
cmpl $0x4, %ebp
jne 0x3909
testl %eax, %eax
jle 0x392a
leaq 0xc(%rsp), %r8
movl $0x4, (%r8)
leaq 0x8(%rsp), %rcx
movl %ebx, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x2170
testl %eax, %eax
js 0x393f
movl 0x8(%rsp), %ebp
testl %ebp, %ebp
je 0x3959
movl %ebp, %edi
callq 0x20a0
leaq 0x3c69(%rip), %rsi      # 0x7534
movl $0x2, %edi
jmp 0x391c
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3ce9(%rip), %rsi      # 0x75ce
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x355c
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0xa0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movl %ebp, %edi
callq 0x20a0
leaq 0x3bb8(%rip), %rsi      # 0x74cf
movl $0x3, %edi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x355c
jmp 0x38f4
leaq 0x3c64(%rip), %rsi      # 0x7595
movl $0x3, %edi
xorl %eax, %eax
callq 0x355c
jmp 0x38f4
movl (%r14), %ebx
movl %ebx, %edi
callq 0x20a0
leaq 0x3baf(%rip), %rsi      # 0x74ff
movl $0x2, %edi
movl %ebx, %edx
jmp 0x391e
leaq 0x3c11(%rip), %rsi      # 0x7571
movl $0x3, %edi
xorl %eax, %eax
callq 0x355c
jmp 0x375e