// File: /workspace/github_c_makefile_star2_O1/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
leaq 0x20(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x10d0
movw $0x0, 0xc(%rsp)
movl $0x333434, 0x8(%rsp)    # imm = 0x333434
movq %r14, %rdi
callq 0x1080
cmpb $0x2f, (%r14,%rax)
jne 0x14d0
movb $0x0, (%r14,%rax)
leaq 0xce4(%rip), %rsi       # 0x21bb
movq %r14, %rdi
callq 0x1050
leaq 0x3(%rax), %rsi
leaq 0x20(%rsp), %r14
movl $0xff, %edx
movq %r14, %rdi
callq 0x10b0
movq %r14, %rdi
movl $0x3a, %esi
callq 0x1070
testq %rax, %rax
je 0x1523
movq %rax, %r14
movq %rax, %rsi
incq %rsi
leaq 0x8(%rsp), %rdi
movl $0x5, %edx
callq 0x10b0
movb $0x0, (%r14)
leaq 0x8(%rsp), %rdi
callq 0x11a0
movl %eax, %ebp
leaq 0x20(%rsp), %rdi
callq 0x1170
testq %rax, %rax
je 0x15d4
movq %rax, %r15
movl $0x2, %edi
movl $0x1, %esi
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)
movq $0x0, 0x8(%r12)
callq 0x1090
movq %rax, %r15
movl %r14d, %edi
movq %r12, %rsi
movl $0x10, %edx
callq 0x1120
cmpl $-0x1, %eax
jne 0x15c1
leaq 0xc37(%rip), %rsi       # 0x21e3
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
movq %r15, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x11e0
movl %r14d, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbe4(%rip), %rsi       # 0x21bf
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x11e0
callq 0x10e0