// File: /workspace/github_cpp_makefile_star2_O3/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 0x377f
orl $0x800, %eax            # imm = 0x800
movl %ebx, %edi
movl $0x4, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x3797
movl %ebx, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x21b0
testl %eax, %eax
js 0x37af
xorl %r14d, %r14d
movl %ebx, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x3898
andl $0x7ffff7ff, %eax       # imm = 0x7FFFF7FF
movl %ebx, %edi
movl $0x4, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
jns 0x38c0
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3e8a(%rip), %rsi      # 0x7604
jmp 0x38ab
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3c8f(%rip), %rsi      # 0x7421
jmp 0x38ab
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3cb0(%rip), %rsi      # 0x745a
jmp 0x38ab
callq 0x2030
movl (%rax), %ebp
cmpl $0x73, %ebp
jne 0x38cf
movq %rax, %r14
leaq 0x3cca(%rip), %rsi      # 0x7493
movl $0x3, %edi
xorl %eax, %eax
callq 0x352c
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
js 0x38e6
je 0x38ee
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 0x3918
movl 0x8(%rsp), %ebp
testl %ebp, %ebp
je 0x393f
movl %ebp, %edi
callq 0x20a0
leaq 0x3ca3(%rip), %rsi      # 0x7534
movl $0x2, %edi
jmp 0x38e2
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x3d23(%rip), %rsi      # 0x75ce
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x352c
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0xa0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movl %ebp, %edi
callq 0x20a0
leaq 0x3bf2(%rip), %rsi      # 0x74cf
movl $0x3, %edi
movl %ebp, %edx
jmp 0x3930
movl (%r14), %ebx
cmpl $0x4, %ebx
jne 0x3903
leaq 0x3ca0(%rip), %rsi      # 0x7595
movl $0x3, %edi
xorl %eax, %eax
callq 0x352c
jmp 0x38ba
movl %ebx, %edi
callq 0x20a0
leaq 0x3bbe(%rip), %rsi      # 0x74cf
movl $0x3, %edi
jmp 0x392e
movl (%r14), %ebx
movl %ebx, %edi
callq 0x20a0
leaq 0x3bd6(%rip), %rsi      # 0x74ff
movl $0x2, %edi
movl %ebx, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x352c
jmp 0x38ba
leaq 0x3c2b(%rip), %rsi      # 0x7571
movl $0x3, %edi
xorl %eax, %eax
callq 0x352c
jmp 0x372e