// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x98, %rsp
cmpb $0x1, 0x14(%rdi)
jne 0x317c
movq %rdi, %rbx
movl 0x10(%rdi), %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
js 0x3190
orl $0x800, %eax            # imm = 0x800
movl 0x10(%rbx), %edi
pushq $0x4
popq %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
js 0x30c0
movl 0x10(%rbx), %edi
movq 0x8(%rbx), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x2190
testl %eax, %eax
js 0x30e2
movl 0x10(%rbx), %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2110
testl %eax, %eax
js 0x3190
andl $0x7ffff7ff, %eax       # imm = 0x7FFFF7FF
movl 0x10(%rbx), %edi
pushq $0x4
popq %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2110
movb $0x1, %r15b
testl %eax, %eax
jns 0x317f
movq 0x2f09(%rip), %rax      # 0x5fd0
movq (%rax), %rbx
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x1185(%rip), %rsi      # 0x4262
jmp 0x31ad
callq 0x2030
cmpl $0x73, (%rax)
jne 0x317c
movq %rax, %r14
movq $0x5, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x3115
andq $0x0, 0x18(%rsp,%rax,8)
incq %rax
jmp 0x3104
movl 0x10(%rbx), %edi
pushq $0x40
popq %rcx
movl %edi, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %edi, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, 0x18(%rsp,%rdx,8)
incl %edi
xorl %r15d, %r15d
leaq 0x18(%rsp), %rdx
leaq 0x8(%rsp), %r8
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x21d0
testl %eax, %eax
jle 0x317f
leaq 0x4(%rsp), %r8
movl $0x4, (%r8)
movl 0x10(%rbx), %edi
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rsp, %rcx
callq 0x2170
testl %eax, %eax
js 0x31c1
cmpl $0x0, (%rsp)
je 0x3089
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x98, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x2e39(%rip), %rax      # 0x5fd0
movq (%rax), %rbx
callq 0x2030
movl (%rax), %edi
callq 0x20a0
leaq 0x1095(%rip), %rsi      # 0x4242
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2280
xorl %edi, %edi
callq 0x2230
movq 0x2e08(%rip), %rax      # 0x5fd0
movq (%rax), %rbx
movl (%r14), %ebp
movl %ebp, %edi
callq 0x20a0
leaq 0x10a6(%rip), %rsi      # 0x4282
movq %rbx, %rdi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2280
jmp 0x31ba
nop