#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  long s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_zero;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ portnum;
typedef int /*<<< orphan*/ hostname;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ BIO_printf(int /*<<< orphan*/ *, char *, char *, ...);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ abort();
int atoi(char *);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strchr(char *, char);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
int create_socket(char url_str[], BIO *out) {
  int sockfd;
  char hostname[256] = "";
  char portnum[6] = "19050";
  char proto[6] = "";
  char *tmp_ptr = NULL;
  int port;
  struct hostent *host;
  struct sockaddr_in dest_addr;
  if (url_str[strlen(url_str)] == '/')
    url_str[strlen(url_str)] = '\0';
  strncpy(proto, url_str, (strchr(url_str, ':') - url_str));
  strncpy(hostname, strstr(url_str, "://") + 3, sizeof(hostname));
  if (strchr(hostname, ':')) {
    tmp_ptr = strchr(hostname, ':');
    strncpy(portnum, tmp_ptr + 1, sizeof(portnum));
    *tmp_ptr = '\0';
  }
  port = atoi(portnum);
  if ((host = gethostbyname(hostname)) == NULL) {
    BIO_printf(out, "Error: Cannot resolve hostname %s.\n", hostname);
    abort();
  }
  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);
  memset(&(dest_addr.sin_zero), '\0', 8);
  tmp_ptr = inet_ntoa(dest_addr.sin_addr);
  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;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   10a0 <memset@plt>
mov    0xe04(%rip),%eax
mov    %eax,-0x126(%rbp)
mov    0xdfb(%rip),%ax
mov    %ax,-0x122(%rbp)
lea    -0x12c(%rbp),%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   10a0 <memset@plt>
movq   $0x0,-0x138(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x168(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x168(%rbp),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    127b <create_socket+0xab>
mov    -0x8(%rbp),%rax
mov    %rax,-0x170(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x170(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
lea    -0x12c(%rbp),%rax
mov    %rax,-0x188(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x180(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
mov    -0x188(%rbp),%rdi
mov    -0x180(%rbp),%rsi
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x178(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xd2f(%rip),%rsi
call   10e0 <strstr@plt>
mov    -0x178(%rbp),%rdi
mov    %rax,%rsi
add    $0x3,%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    -0x120(%rbp),%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
cmp    $0x0,%rax
je     134d <create_socket+0x17d>
lea    -0x120(%rbp),%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
mov    %rax,-0x138(%rbp)
lea    -0x126(%rbp),%rdi
mov    -0x138(%rbp),%rsi
add    $0x1,%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x138(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x126(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x13c(%rbp)
lea    -0x120(%rbp),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x148(%rbp)
cmp    $0x0,%rax
jne    139c <create_socket+0x1cc>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    0xc7c(%rip),%rsi
mov    $0x0,%al
call   1090 <BIO_printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
mov    0x2c2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2c29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x150(%rbp)
mov    -0x13c(%rbp),%edi
call   1070 <htons@plt>
mov    %eax,-0x154(%rbp)
mov    -0x148(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x160(%rbp)
lea    -0x160(%rbp),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10a0 <memset@plt>
mov    -0x160(%rbp),%rdi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x138(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    $0x4,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    145a <create_socket+0x28a>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rdx
mov    -0x138(%rbp),%rcx
mov    -0x13c(%rbp),%r8d
lea    0xbdb(%rip),%rsi
mov    $0x0,%al
call   1090 <BIO_printf@plt>
mov    -0x14(%rbp),%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x130,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movw   $0x30,0xc(%rsp)
movl   $0x35303931,0x8(%rsp)
35
movw   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
call   1060 <strlen@plt>
cmpb   $0x2f,(%rbx,%rax,1)
jne    1286 <create_socket+0xb6>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
sub    %ebx,%eax
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
lea    0xd5b(%rip),%rsi
mov    %rbx,%rdi
call   10e0 <strstr@plt>
lea    0x3(%rax),%rsi
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
test   %rax,%rax
je     12f7 <create_socket+0x127>
mov    %rax,%rbx
mov    %rax,%rsi
add    $0x1,%rsi
lea    0x8(%rsp),%rdi
mov    $0x6,%edx
call   1040 <strncpy@plt>
movb   $0x0,(%rbx)
lea    0x8(%rsp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
lea    0x30(%rsp),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
je     13b8 <create_socket+0x1e8>
mov    %rax,%rbp
mov    0x2cc7(%rip),%rbx
mov    (%rbx),%edi
mov    0x2cae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r12d
mov    (%rbx),%eax
mov    %eax,0x28(%rsp)
mov    %r15d,%edi
call   1070 <htons@plt>
mov    %eax,0x24(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10a0 <memset@plt>
mov    0x18(%rsp),%rdi
call   1050 <inet_ntoa@plt>
mov    %rax,%rbp
lea    0x18(%rsp),%rsi
mov    %r12d,%edi
mov    $0x4,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    13a5 <create_socket+0x1d5>
lea    0xc9e(%rip),%rsi
lea    0x30(%rsp),%rdx
mov    %r14,%rdi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1090 <BIO_printf@plt>
mov    %r12d,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc4b(%rip),%rsi
lea    0x30(%rsp),%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1090 <BIO_printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@__const.create_socket.portnum = [6 x i8] c"19050\00"
@.str = [4 x i8] c"://\00"
@.str.1 = [36 x i8] c"Error: Cannot resolve hostname %s.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [51 x i8] c"Error: Cannot connect to host %s [%s] on port %d.\0A\00"
define i32 @create_socket(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca [6 x i8]
%8 = alloca [6 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.hostent*
%12 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32* %1, i32** %4
%13 = bitcast [256 x i8]* %6 to i8*
%14 = bitcast [6 x i8]* %7 to i8*
%15 = bitcast [6 x i8]* %8 to i8*
store i8* null, i8** %9
%16 = load i8*, i8** %3
%17 = load i8*, i8** %3
%18 = call i64 @strlen(i8* %17)
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 47
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i8*, i8** %3
%25 = load i8*, i8** %3
%26 = call i64 @strlen(i8* %25)
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %23, %2
%29 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%30 = load i8*, i8** %3
%31 = load i8*, i8** %3
%32 = call i8* @strchr(i8* %31, i8 signext 58)
%33 = load i8*, i8** %3
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
%38 = call i32 @strncpy(i8* %29, i8* %30, i32 %37)
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%40 = load i8*, i8** %3
%41 = call i8* @strstr(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%42 = getelementptr inbounds i8, i8* %41, i64 3
%43 = call i32 @strncpy(i8* %39, i8* %42, i32 256)
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%45 = call i8* @strchr(i8* %44, i8 signext 58)
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %55
47: ; preds = %28
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%49 = call i8* @strchr(i8* %48, i8 signext 58)
store i8* %49, i8** %9
%50 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%51 = load i8*, i8** %9
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = call i32 @strncpy(i8* %50, i8* %52, i32 6)
%54 = load i8*, i8** %9
store i8 0, i8* %54
br label %55
55: ; preds = %47, %28
%56 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%57 = call i32 @atoi(i8* %56)
store i32 %57, i32* %10
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%59 = call %struct.hostent* @gethostbyname(i8* %58)
store %struct.hostent* %59, %struct.hostent** %11
%60 = icmp eq %struct.hostent* %59, null
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = load i32*, i32** %4
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%64 = call i32 (i32*, i8*, i8*, ...) @BIO_printf(i32* %62, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %63)
%65 = call i32 (...) @abort()
unreachable
66: ; preds = %55
%67 = load i32, i32* @AF_INET
%68 = load i32, i32* @SOCK_STREAM
%69 = call i32 @socket(i32 %67, i32 %68, i32 0)
store i32 %69, i32* %5
%70 = load i32, i32* @AF_INET
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 3
store i32 %70, i32* %71
%72 = load i32, i32* %10
%73 = call i32 @htons(i32 %72)
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %73, i32* %74
%75 = load %struct.hostent*, %struct.hostent** %11
%76 = getelementptr inbounds %struct.hostent, %struct.hostent* %75, i32 0, i32 0
%77 = load i64, i64* %76
%78 = inttoptr i64 %77 to i64*
%79 = load i64, i64* %78
%80 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%81 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %80, i32 0, i32 0
store i64 %79, i64* %81
%82 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%83 = call i32 @memset(i32* %82, i8 signext 0, i32 8)
%84 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%85 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %84, i32 0, i32 0
%86 = load i64, i64* %85
%87 = call i8* @inet_ntoa(i64 %86)
store i8* %87, i8** %9
%88 = load i32, i32* %5
%89 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%90 = call i32 @connect(i32 %88, %struct.sockaddr* %89, i32 4)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %98
92: ; preds = %66
%93 = load i32*, i32** %4
%94 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%95 = load i8*, i8** %9
%96 = load i32, i32* %10
%97 = call i32 (i32*, i8*, i8*, ...) @BIO_printf(i32* %93, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %94, i8* %95, i32 %96)
br label %98
98: ; preds = %92, %66
%99 = load i32, i32* %5
ret i32 %99
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@__const.create_socket.portnum = [6 x i8] c"19050\00"
@.str = [4 x i8] c"://\00"
@.str.1 = [36 x i8] c"Error: Cannot resolve hostname %s.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [51 x i8] c"Error: Cannot connect to host %s [%s] on port %d.\0A\00"
define i32 @create_socket(i8* %0, i32* %1) {
%3 = alloca [256 x i8]
%4 = alloca [6 x i8]
%5 = alloca [6 x i8]
%6 = alloca %struct.sockaddr_in
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 47
br i1 %14, label %15, label %16
15: ; preds = %2
store i8 0, i8* %12
br label %16
16: ; preds = %15, %2
%17 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%18 = ptrtoint i8* %17 to i64
%19 = ptrtoint i8* %0 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = call i32 @strncpy(i8* %9, i8* %0, i32 %21)
%23 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%24 = getelementptr inbounds i8, i8* %23, i64 3
%25 = call i32 @strncpy(i8* %7, i8* %24, i32 256)
%26 = call i8* @strchr(i8* %7, i8 signext 58)
%27 = icmp eq i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %16
%29 = getelementptr inbounds i8, i8* %26, i64 1
%30 = call i32 @strncpy(i8* %8, i8* %29, i32 6)
store i8 0, i8* %26
br label %31
31: ; preds = %28, %16
%32 = call i32 @atoi(i8* %8)
%33 = call %struct.hostent* @gethostbyname(i8* %7)
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 (i32*, i8*, i8*, ...) @BIO_printf(i32* %1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %31
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
%42 = load i32, i32* @AF_INET
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 3
store i32 %42, i32* %43
%44 = call i32 @htons(i32 %32)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %44, i32* %45
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i64 0, i32 0
%47 = load i64, i64* %46
%48 = inttoptr i64 %47 to i64*
%49 = load i64, i64* %48
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i64 %49, i64* %50
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%52 = call i32 @memset(i32* %51, i8 signext 0, i32 8)
%53 = load i64, i64* %50
%54 = call i8* @inet_ntoa(i64 %53)
%55 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%56 = call i32 @connect(i32 %41, %struct.sockaddr* %55, i32 4)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %38
%59 = call i32 (i32*, i8*, i8*, ...) @BIO_printf(i32* %1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* %54, i32 %32)
br label %60
60: ; preds = %58, %38
ret i32 %41
}