#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_aton(char *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strchr(char *, char);
char *strdup(char const *);
int /*<<< orphan*/ warn(char *);
int connect_to_dest(const char *addr_and_port) {
  int s;
  char *buf, *p;
  struct sockaddr_in sin;
  bzero(&sin, sizeof(sin));
  if ((buf = strdup(addr_and_port)) == NULL)
    err(1, "strdup");
  if ((p = strchr(buf, ':')) == NULL)
    return -1;
  *p++ = '\0';
  sin.sin_port = htons((in_port_t)atoi(p));
  if (inet_aton(buf, &sin.sin_addr) == -1) {
    free(buf);
    return -1;
  }
  free(buf);
  sin.sin_family = AF_INET;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    warn("socket");
    return -1;
  }
  if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    warn("connect");
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<connect_to_dest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f2 <connect_to_dest+0x42>
mov    $0x1,%edi
lea    0xe13(%rip),%rsi
call   1080 <err@plt>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    121a <connect_to_dest+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <connect_to_dest+0x148>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
add    $0x4,%rsi
call   1070 <inet_aton@plt>
cmp    $0xffffffff,%eax
jne    126e <connect_to_dest+0xbe>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <connect_to_dest+0x148>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12c0 <connect_to_dest+0x110>
lea    0xd58(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <connect_to_dest+0x148>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f2 <connect_to_dest+0x142>
lea    0xd2d(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <connect_to_dest+0x148>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_dest>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    %rbx,%rdi
call   10c0 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e9 <connect_to_dest+0x39>
lea    0xe21(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     128e <connect_to_dest+0xde>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <inet_aton@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebp
je     128e <connect_to_dest+0xde>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    %edi,(%rsp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     127c <connect_to_dest+0xcc>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    128e <connect_to_dest+0xde>
lea    0xd94(%rip),%rdi
jmp    1283 <connect_to_dest+0xd3>
lea    0xd84(%rip),%rdi
call   1040 <warn@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"strdup\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @connect_to_dest(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
%8 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%9 = load i8*, i8** %3
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = load i8*, i8** %5
%16 = call i8* @strchr(i8* %15, i8 signext 58)
store i8* %16, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -1, i32* %2
br label %54
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %6
store i8 0, i8* %20
%22 = load i8*, i8** %6
%23 = call i64 @atoi(i8* %22)
%24 = trunc i64 %23 to i32
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i8*, i8** %5
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%29 = call i32 @inet_aton(i8* %27, i32* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %19
%32 = load i8*, i8** %5
%33 = call i32 @free(i8* %32)
store i32 -1, i32* %2
br label %54
34: ; preds = %19
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %4
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
45: ; preds = %34
%46 = load i32, i32* %4
%47 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%48 = call i32 @connect(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
52: ; preds = %45
%53 = load i32, i32* %4
store i32 %53, i32* %2
br label %54
54: ; preds = %52, %50, %43, %31, %18
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"strdup\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @connect_to_dest(i8* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%5 = tail call i8* @strdup(i8* %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = tail call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 58)
%11 = icmp eq i8* %10, null
br i1 %11, label %35, label %12
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %10
%14 = tail call i64 @atoi(i8* %13)
%15 = trunc i64 %14 to i32
%16 = tail call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%19 = call i32 @inet_aton(i8* %5, i32* %18)
%20 = icmp eq i32 %19, -1
%21 = call i32 @free(i8* %5)
br i1 %20, label %35, label %22
22: ; preds = %12
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %23, i32 %25, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %22
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @connect(i32 %26, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %28, %22
%33 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %22 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %28 ]
%34 = call i32 @warn(i8* %33)
br label %35
35: ; preds = %32, %12, %28, %9
%36 = phi i32 [ -1, %9 ], [ %26, %28 ], [ -1, %12 ], [ -1, %32 ]
ret i32 %36
}