// File: /workspace/github_cpp_makefile_star2_O3/plasma-umass[P]simplesocket/simplesocket.cpp
// Function: simplesocket::connect()
bool simplesocket::connect (void) {
  long arg;
  fd_set myset; 
  struct timeval tv; 
  int valopt; 
  socklen_t lon; 
  int res;

  if (!resolved()) return false;

  // Stolen from the web -mdc.
  // Set non-blocking 
  if( (arg = fcntl(_socketfd, F_GETFL, NULL)) < 0) { 
    fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
    exit(0); 
  } 
  arg |= O_NONBLOCK; 
  if( fcntl(_socketfd, F_SETFL, arg) < 0) { 
    fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
    exit(0); 
  } 
  // Trying to connect with timeout 
  res = ::connect(_socketfd, _addresses->ai_addr, _addresses->ai_addrlen); 
  if (res < 0) { 
    if (errno == EINPROGRESS) { 
      //      fprintf(stderr, "EINPROGRESS in connect() - selecting\n"); 
      do { 
	tv.tv_sec = 5; 
	tv.tv_usec = 0; 
	FD_ZERO(&myset); 
	FD_SET(_socketfd, &myset); 
	res = select(_socketfd+1, NULL, &myset, NULL, &tv); 
	if (res < 0 && errno != EINTR) { 
	  //fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); 
	  res =-1;
	  return (res >= 0);
	} 
	else if (res > 0) { 
	  // Socket selected for write 
	  lon = sizeof(int); 
	  if (getsockopt(_socketfd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) { 
	    fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno)); 
	    exit (0);
	  } 
	  // Check the value returned... 
	  if (valopt) { 
	    //	    fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt)); 
	    res = -1;
	    return (res >= 0);
	  } 
	  break; 
	} 
	else { 
	  //	  fprintf(stderr, "Timeout in select() - Cancelling!\n"); 
	  res = -1;
	  return (res >= 0);
	} 
      } while (true); 
    } 
    else { 
      //fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); 
      res = -1;
      return (res >= 0);
    } 
  } 
  // Set to blocking mode again... 
  if( (arg = fcntl(_socketfd, F_GETFL, NULL)) < 0) { 
    fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
    exit(0); 
  } 
  arg &= (~O_NONBLOCK); 
  if( fcntl(_socketfd, F_SETFL, arg) < 0) { 
    fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
    exit(0); 
  } 
  
  /*  if (res < 0) {
      if (_debug) cerr << "NO CONNECTION, errno = " << errno << endl;
      }*/
  return (res >= 0);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0xa0, %rsp
cmpb $0x1, 0x14(%rdi)
jne 0x37a8
movq %rdi, %rbx
movl 0x10(%rdi), %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
js 0x37bb
orl $0x800, %eax            # imm = 0x800
movl 0x10(%rbx), %edi
movl $0x4, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
js 0x37c0
movl 0x10(%rbx), %edi
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x21c0
testl %eax, %eax
js 0x36f2
movl 0x10(%rbx), %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2130
testl %eax, %eax
js 0x37c5
andl $0x7ffff7ff, %eax       # imm = 0x7FFFF7FF
movl 0x10(%rbx), %edi
movl $0x4, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2130
movb $0x1, %r15b
testl %eax, %eax
jns 0x37ab
callq 0x2390
callq 0x2040
cmpl $0x73, (%rax)
jne 0x37a8
movq %rax, %r14
leaq 0x10(%rsp), %r8
movq $0x5, (%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)
movaps %xmm0, 0x60(%rdx)
movaps %xmm0, 0x70(%rdx)
movl 0x10(%rbx), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
movl %eax, %esi
andl $-0x40, %esi
movl %edi, %ecx
subl %esi, %ecx
movl $0x1, %esi
shlq %cl, %rsi
sarl $0x6, %eax
cltq
orq %rsi, 0x20(%rsp,%rax,8)
incl %edi
xorl %r15d, %r15d
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x21f0
testl %eax, %eax
jle 0x37ab
leaq 0xc(%rsp), %r8
movl $0x4, (%r8)
movl 0x10(%rbx), %edi
leaq 0x8(%rsp), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x21a0
testl %eax, %eax
js 0x37ca
cmpl $0x0, 0x8(%rsp)
je 0x36b2
xorl %r15d, %r15d
movl %r15d, %eax
addq $0xa0, %rsp
popq %rbx
popq %r14
popq %r15
retq
callq 0x2459
callq 0x2427
callq 0x23c2
movq %r14, %rdi
callq 0x23f4