// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movl %edi, %ebx
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x27e2
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %ebx, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x27f7
pushq $0x10
popq %rdx
movl %ebx, %edi
movq %r14, %rsi
callq 0x21b0
testl %eax, %eax
js 0x280c
xorl %r14d, %r14d
pushq $0x3
popq %rsi
movl %ebx, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
js 0x284f
andl $0x7ffff7ff, %eax       # imm = 0x7FFFF7FF
pushq $0x4
popq %rsi
movl %ebx, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2120
testl %eax, %eax
jns 0x2873
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x4e27(%rip), %rsi      # 0x7604
jmp 0x2862
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x4c2c(%rip), %rsi      # 0x7421
jmp 0x2862
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x4c50(%rip), %rsi      # 0x745a
jmp 0x2862
callq 0x2030
movl (%rax), %ebp
cmpl $0x73, %ebp
jne 0x2884
movq %rax, %r14
leaq 0x4c71(%rip), %rsi      # 0x7493
xorl %r15d, %r15d
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x247c
movq $0xa, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
cmpq $0x10, %r15
je 0x289c
andq $0x0, 0x18(%rsp,%r15,8)
incq %r15
jmp 0x283e
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x4d6c(%rip), %rsi      # 0x75ce
pushq $0x2
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x247c
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %edi
callq 0x20a0
leaq 0x4c3d(%rip), %rsi      # 0x74cf
pushq $0x3
popq %rdi
movl %ebp, %edx
jmp 0x2954
pushq $0x40
popq %rcx
movl %ebx, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %ebx, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, 0x18(%rsp,%rdx,8)
leal 0x1(%rbx), %edi
leaq 0x18(%rsp), %rdx
leaq 0x8(%rsp), %r8
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x21d0
testl %eax, %eax
js 0x290e
je 0x2916
leaq 0x4(%rsp), %r8
movl $0x4, (%r8)
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rsp, %rcx
movl %ebx, %edi
callq 0x2170
testl %eax, %eax
js 0x293e
movl (%rsp), %ebp
testl %ebp, %ebp
je 0x2963
movl %ebp, %edi
callq 0x20a0
leaq 0x4c2a(%rip), %rsi      # 0x7534
pushq $0x2
jmp 0x2894
movl (%r14), %ebx
cmpl $0x4, %ebx
jne 0x292c
leaq 0x4c78(%rip), %rsi      # 0x7595
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x247c
jmp 0x286f
movl %ebx, %edi
callq 0x20a0
leaq 0x4b95(%rip), %rsi      # 0x74cf
pushq $0x3
jmp 0x2951
movl (%r14), %ebx
movl %ebx, %edi
callq 0x20a0
leaq 0x4bb0(%rip), %rsi      # 0x74ff
pushq $0x2
popq %rdi
movl %ebx, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x247c
jmp 0x286f
leaq 0x4c07(%rip), %rsi      # 0x7571
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x247c
jmp 0x2795