// File: /workspace/github_c_makefile_star2_O2/fm4dd[P]learn-ssl/sslconnect.c
// Function: create_socket
int create_socket(char url_str[], BIO *out) {
  int sockfd;
  char hostname[256] = "";
  char    portnum[6] = "443";
  char      proto[6] = "";
  char      *tmp_ptr = NULL;
  int           port;
  struct hostent *host;
  struct sockaddr_in dest_addr;

  /* ---------------------------------------------------------- *
   * Remove the final / from url_str, if there is one           *
   * ---------------------------------------------------------- */
  if(url_str[strlen(url_str)] == '/')
    url_str[strlen(url_str)] = '\0';

  /* ---------------------------------------------------------- *
   * the first : ends the protocol string, i.e. http            *
   * ---------------------------------------------------------- */
  strncpy(proto, url_str, (strchr(url_str, ':')-url_str));

  /* ---------------------------------------------------------- *
   * the hostname starts after the "://" part                   *
   * ---------------------------------------------------------- */
  strncpy(hostname, strstr(url_str, "://")+3, sizeof(hostname)-1);

  /* ---------------------------------------------------------- *
   * if the hostname contains a colon :, we got a port number   *
   * ---------------------------------------------------------- */
  if(strchr(hostname, ':')) {
    tmp_ptr = strchr(hostname, ':');
    /* the last : starts the port number, if avail, i.e. 8443 */
    strncpy(portnum, tmp_ptr+1,  sizeof(portnum)-1);
    *tmp_ptr = '\0';
  }

  port = atoi(portnum);

  if ( (host = gethostbyname(hostname)) == NULL ) {
    BIO_printf(out, "Error: Cannot resolve hostname %s.\n",  hostname);
    abort();
  }

  /* ---------------------------------------------------------- *
   * create the basic TCP socket                                *
   * ---------------------------------------------------------- */
  sockfd = socket(AF_INET, SOCK_STREAM, 0);

  dest_addr.sin_family=AF_INET;
  dest_addr.sin_port=htons(port);
  dest_addr.sin_addr.s_addr = *(long*)(host->h_addr);

  /* ---------------------------------------------------------- *
   * Zeroing the rest of the struct                             *
   * ---------------------------------------------------------- */
  memset(&(dest_addr.sin_zero), '\0', 8);

  tmp_ptr = inet_ntoa(dest_addr.sin_addr);

  /* ---------------------------------------------------------- *
   * Try to make the host connect here                          *
   * ---------------------------------------------------------- */
  if ( connect(sockfd, (struct sockaddr *) &dest_addr,
                              sizeof(struct sockaddr)) == -1 ) {
    BIO_printf(out, "Error: Cannot connect to host %s [%s] on port %d.\n",
             hostname, tmp_ptr, port);
  }

  return sockfd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
movq %rsi, %rbx
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x110(%rsp)
andw $0x0, 0xc(%rsp)
movl $0x333434, 0x8(%rsp)    # imm = 0x333434
callq 0x1080
cmpb $0x2f, (%r14,%rax)
jne 0x14b6
movb $0x0, (%r14,%rax)
leaq 0xcfe(%rip), %rsi       # 0x21bb
movq %r14, %rdi
callq 0x1050
leaq 0x3(%rax), %rsi
leaq 0x20(%rsp), %r14
movl $0xff, %edx
movq %r14, %rdi
callq 0x10b0
pushq $0x3a
popq %rsi
movq %r14, %rdi
callq 0x1070
testq %rax, %rax
je 0x1505
movq %rax, %r14
movq %rax, %rsi
incq %rsi
leaq 0x8(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x10b0
movb $0x0, (%r14)
leaq 0x8(%rsp), %rdi
callq 0x1190
movl %eax, %ebp
leaq 0x20(%rsp), %rdi
callq 0x1160
testq %rax, %rax
je 0x15ad
movq %rax, %r15
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1060
movl %eax, %r14d
leaq 0x10(%rsp), %r12
movw $0x2, (%r12)
movl %ebp, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r12)
movq 0x18(%r15), %rax
movq (%rax), %rax
movl (%rax), %edi
movl %edi, 0x4(%r12)
andq $0x0, 0x8(%r12)
callq 0x1090
movq %rax, %r15
pushq $0x10
popq %rdx
movl %r14d, %edi
movq %r12, %rsi
callq 0x1110
cmpl $-0x1, %eax
jne 0x159a
leaq 0xc5e(%rip), %rsi       # 0x21e3
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
movq %r15, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x11d0
movl %r14d, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc0b(%rip), %rsi       # 0x21bf
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x11d0
callq 0x10d0