// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x10e0
movl 0xcca(%rip), %eax       # 0x2216
movl %eax, -0x126(%rbp)
movw 0xcc1(%rip), %ax        # 0x221a
movw %ax, -0x122(%rbp)
leaq -0x12c(%rbp), %rdi
xorl %esi, %esi
movl $0x6, %edx
callq 0x10e0
movq $0x0, -0x138(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1090
movq %rax, %rcx
movq -0x160(%rbp), %rax
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x15c7
movq -0x8(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1090
movq %rax, %rcx
movq -0x168(%rbp), %rax
movb $0x0, (%rax,%rcx)
leaq -0x12c(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x8(%rbp), %rdi
movl $0x3a, %esi
callq 0x1080
movq -0x180(%rbp), %rdi
movq -0x178(%rbp), %rsi
movq %rax, %rdx
movq -0x8(%rbp), %rax
subq %rax, %rdx
callq 0x10c0
leaq -0x120(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xb97(%rip), %rsi       # 0x21bb
callq 0x1060
movq -0x170(%rbp), %rdi
movq %rax, %rsi
addq $0x3, %rsi
movl $0xff, %edx
callq 0x10c0
leaq -0x120(%rbp), %rdi
movl $0x3a, %esi
callq 0x1080
cmpq $0x0, %rax
je 0x1696
leaq -0x120(%rbp), %rdi
movl $0x3a, %esi
callq 0x1080
movq %rax, -0x138(%rbp)
leaq -0x126(%rbp), %rdi
movq -0x138(%rbp), %rsi
addq $0x1, %rsi
movl $0x5, %edx
callq 0x10c0
movq -0x138(%rbp), %rax
movb $0x0, (%rax)
leaq -0x126(%rbp), %rdi
callq 0x11b0
movl %eax, -0x13c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x1180
movq %rax, -0x148(%rbp)
cmpq $0x0, %rax
jne 0x16df
movq -0x10(%rbp), %rdi
leaq -0x120(%rbp), %rdx
leaq 0xaec(%rip), %rsi       # 0x21bf
movb $0x0, %al
callq 0x11f0
callq 0x10f0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1070
movl %eax, -0x14(%rbp)
movw $0x2, -0x158(%rbp)
movl -0x13c(%rbp), %eax
movzwl %ax, %edi
callq 0x1040
movw %ax, -0x156(%rbp)
movq -0x148(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq (%rax), %rax
movl %eax, -0x154(%rbp)
leaq -0x158(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x10e0
movl -0x154(%rbp), %edi
callq 0x10a0
movq %rax, -0x138(%rbp)
movl -0x14(%rbp), %edi
leaq -0x158(%rbp), %rsi
movl $0x10, %edx
callq 0x1130
cmpl $-0x1, %eax
jne 0x1791
movq -0x10(%rbp), %rdi
leaq -0x120(%rbp), %rdx
movq -0x138(%rbp), %rcx
movl -0x13c(%rbp), %r8d
leaq 0xa59(%rip), %rsi       # 0x21e3
movb $0x0, %al
callq 0x11f0
movl -0x14(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq