#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connectTo(struct in_addr *host, int port) {
  int sockfd;
  struct sockaddr_in target_addr;
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    return -1;
  target_addr.sin_family = AF_INET;
  target_addr.sin_port = htons(port);
  target_addr.sin_addr = *host;
  memset(&(target_addr.sin_zero), '\0', 8);
  if (connect(sockfd, (struct sockaddr *)&target_addr,
              sizeof(struct sockaddr)) == -1)
    return -1;
  return sockfd;
}


x86 assembly (clang, -O0):
<connectTo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1180 <connectTo+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <connectTo+0x9c>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11d6 <connectTo+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <connectTo+0x9c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectTo>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11b5 <connectTo+0x75>
mov    %eax,%ebx
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    11ba <connectTo+0x7a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @connectTo(%struct.in_addr* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.in_addr*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store %struct.in_addr* %0, %struct.in_addr** %4
store i32 %1, i32* %5
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %32
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* %5
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%20 = load %struct.in_addr*, %struct.in_addr** %4
%21 = bitcast %struct.in_addr* %19 to i8*
%22 = bitcast %struct.in_addr* %20 to i8*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%24 = call i32 @memset(i32* %23, i8 signext 0, i32 8)
%25 = load i32, i32* %6
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %13
store i32 -1, i32* %3
br label %32
30: ; preds = %13
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %29, %12
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @connectTo(%struct.in_addr* nocapture %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 %1)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = getelementptr %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%16 = load i32, i32* %14
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%18 = call i32 @memset(i32* %17, i8 signext 0, i32 8)
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i32 @connect(i32 %7, %struct.sockaddr* %19, i32 4)
%21 = icmp eq i32 %20, -1
%22 = select i1 %21, i32 -1, i32 %7
br label %23
23: ; preds = %9, %2
%24 = phi i32 [ -1, %2 ], [ %22, %9 ]
ret i32 %24
}