// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x53e0
testb $0x1, %al
jne 0x4d9c
movb $0x0, -0x1(%rbp)
jmp 0x5190
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %edi
movl $0x3, %esi
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x3120
cltq
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jge 0x4dfe
movq 0x3207(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq -0xd0(%rbp), %rdi
movq %rax, %rdx
leaq 0x1452(%rip), %rsi      # 0x6242
movb $0x0, %al
callq 0x3370
xorl %edi, %edi
callq 0x3300
movq -0xc8(%rbp), %rax
movq -0x18(%rbp), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, -0x18(%rbp)
movl 0x10(%rax), %edi
movq -0x18(%rbp), %rdx
movl $0x4, %esi
movb $0x0, %al
callq 0x3120
cmpl $0x0, %eax
jge 0x4e68
movq 0x319d(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq -0xd8(%rbp), %rdi
movq %rax, %rdx
leaq 0x1408(%rip), %rsi      # 0x6262
movb $0x0, %al
callq 0x3370
xorl %edi, %edi
callq 0x3300
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %edi
movq 0x8(%rax), %rcx
movq 0x18(%rcx), %rsi
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
callq 0x31f0
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
jge 0x50b5
callq 0x3040
cmpl $0x73, (%rax)
jne 0x5095
jmp 0x4ea9
movq $0x5, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl $0x0, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
cmpq $0x10, %rax
jae 0x4f09
movq -0xc0(%rbp), %rax
movl -0xb8(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x4ed7
jmp 0x4f0b
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0xc8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, -0xe0(%rbp)
movl 0x10(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0xe0(%rbp), %rdx
movl %eax, %ecx
movq -0xc8(%rbp), %rax
movslq %ecx, %rcx
orq -0x98(%rbp,%rcx,8), %rdx
movq %rdx, -0x98(%rbp,%rcx,8)
movl 0x10(%rax), %edi
addl $0x1, %edi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x98(%rbp), %rdx
leaq -0xa8(%rbp), %r8
movq %rcx, %rsi
callq 0x3240
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
jge 0x4fba
callq 0x3040
cmpl $0x4, (%rax)
je 0x4fba
movl $0xffffffff, -0xb4(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0xb4(%rbp)
setge %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5190
cmpl $0x0, -0xb4(%rbp)
jle 0x5075
movq -0xc8(%rbp), %rax
movl $0x4, -0xb0(%rbp)
movl 0x10(%rax), %edi
movl $0x1, %esi
movl $0x4, %edx
leaq -0xac(%rbp), %rcx
leaq -0xb0(%rbp), %r8
callq 0x31c0
cmpl $0x0, %eax
jge 0x504c
movq 0x2fcc(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
callq 0x3040
movl (%rax), %eax
movl %eax, -0xe4(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq -0xf0(%rbp), %rdi
movl -0xe4(%rbp), %edx
movq %rax, %rcx
leaq 0x1244(%rip), %rsi      # 0x6282
movb $0x0, %al
callq 0x3370
xorl %edi, %edi
callq 0x3300
cmpl $0x0, -0xac(%rbp)
je 0x5073
movl $0xffffffff, -0xb4(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0xb4(%rbp)
setge %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5190
jmp 0x5093
movl $0xffffffff, -0xb4(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0xb4(%rbp)
setge %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5190
jmp 0x50b3
movl $0xffffffff, -0xb4(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0xb4(%rbp)
setge %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5190
jmp 0x50b5
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %edi
movl $0x3, %esi
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x3120
cltq
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jge 0x5117
movq 0x2eee(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq -0xf8(%rbp), %rdi
movq %rax, %rdx
leaq 0x1139(%rip), %rsi      # 0x6242
movb $0x0, %al
callq 0x3370
xorl %edi, %edi
callq 0x3300
movq -0xc8(%rbp), %rax
movq -0x18(%rbp), %rcx
andq $-0x801, %rcx           # imm = 0xF7FF
movq %rcx, -0x18(%rbp)
movl 0x10(%rax), %edi
movq -0x18(%rbp), %rdx
movl $0x4, %esi
movb $0x0, %al
callq 0x3120
cmpl $0x0, %eax
jge 0x5181
movq 0x2e84(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, -0x100(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq -0x100(%rbp), %rdi
movq %rax, %rdx
leaq 0x10ef(%rip), %rsi      # 0x6262
movb $0x0, %al
callq 0x3370
xorl %edi, %edi
callq 0x3300
cmpl $0x0, -0xb4(%rbp)
setge %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nop