// File: /workspace/github_c_makefile_star2_O2/icysunlight[P]miniftpd/sysutil.c
// Function: connect_timeout
int connect_timeout(int fd, struct sockaddr_in *addr, unsigned int wait_seconds)
{
    int ret;
    socklen_t addrlen = sizeof(struct sockaddr_in);

    if (wait_seconds > 0)
        activate_nonblock(fd);

    ret = connect(fd, (struct sockaddr*)addr, addrlen);
    if (ret < 0 && errno == EINPROGRESS)
    {
        //printf("AAAAA\n");
        fd_set connect_fdset;
        struct timeval timeout;
        FD_ZERO(&connect_fdset);
        FD_SET(fd, &connect_fdset);
        timeout.tv_sec = wait_seconds;
        timeout.tv_usec = 0;
        do
        {
            /* ????????????? */
            ret = select(fd + 1, NULL, &connect_fdset, NULL, &timeout);
        } while (ret < 0 && errno == EINTR);
        if (ret == 0)
        {
            ret = -1;
            errno = ETIMEDOUT;
        }
        else if (ret < 0)
            return -1;
        else if (ret == 1)
        {
            //printf("BBBBB\n");
            /* ret???1??????????????????????????????*/
            /* ???????????errno???????????getsockopt???? */
            int err;
            socklen_t socklen = sizeof(err);
            int sockoptret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &socklen);
            if (sockoptret == -1)
            {
                return -1;
            }
            if (err == 0)
            {
                //printf("DDDDDDD\n");
                ret = 0;
            }
            else
            {
                //printf("CCCCCC\n");
                errno = err;
                ret = -1;
            }
        }
    }
    if (wait_seconds > 0)
    {
        deactivate_nonblock(fd);
    }
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edx, %ebp
movq %rsi, %r14
movl %edi, %ebx
testl %edx, %edx
je 0x3eb9
movl %ebx, %edi
callq 0x3bec
pushq $0x10
popq %rdx
movl %ebx, %edi
movq %r14, %rsi
callq 0x34f0
movl %eax, %r15d
testl %eax, %eax
jns 0x3fa2
callq 0x3060
cmpl $0x73, (%rax)
jne 0x3fa2
movq %rax, %r14
xorl %eax, %eax
cmpq $0x10, %rax
je 0x3ef5
andq $0x0, 0x28(%rsp,%rax,8)
incq %rax
jmp 0x3ee4
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, 0x28(%rsp,%rdx,8)
movl %ebp, 0xc(%rsp)
movl %ebp, %eax
leaq 0x18(%rsp), %r12
movq %rax, (%r12)
andq $0x0, 0x8(%r12)
leal 0x1(%rbx), %r13d
leaq 0x28(%rsp), %rbp
movl %r13d, %edi
xorl %esi, %esi
movq %rbp, %rdx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x3370
testl %eax, %eax
jns 0x3f52
cmpl $0x4, (%r14)
je 0x3f30
pushq $-0x1
popq %r15
jmp 0x3fad
movl %eax, %r15d
cmpl $0x1, %eax
je 0x3f6c
testl %r15d, %r15d
movl 0xc(%rsp), %ebp
jne 0x3fa2
movl $0x6e, (%r14)
jmp 0x3f9e
leaq 0x14(%rsp), %r8
movl $0x4, (%r8)
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
leaq 0x10(%rsp), %rcx
movl %ebx, %edi
callq 0x32b0
cmpl $-0x1, %eax
movl 0xc(%rsp), %ebp
je 0x3f4c
movl 0x10(%rsp), %eax
testl %eax, %eax
je 0x3fc2
movl %eax, (%r14)
pushq $-0x1
popq %r15
testl %ebp, %ebp
je 0x3fad
movl %ebx, %edi
callq 0x3c2c
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
jmp 0x3fa2