#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 /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  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*/ atoi(char *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int socket_connect(int sockfd, char *dest, char *port) {
  int status;
  struct sockaddr_in address;
  struct in_addr in_addr;
  address.sin_family = AF_INET;
  address.sin_port = htons(atoi(port));
  inet_aton(dest, &in_addr.s_addr);
  address.sin_addr = in_addr;
  if ((status = connect(sockfd, (struct sockaddr *)&address,
                        sizeof(struct sockaddr_in))) < 0) {
    return -1;
  }
  return status;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <inet_aton@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11ae <socket_connect+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <socket_connect+0x74>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rdx,%rdi
call   1050 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <inet_aton@plt>
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
test   %eax,%eax
mov    $0xffffffff,%ecx
cmovs  %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32 @socket_connect(i32 %0, i8* %1, i8* nocapture %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.in_addr
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast %struct.in_addr* %5 to i8*
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @atoi(i8* %2)
%11 = tail call i32 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i64 0, i32 0
%14 = call i32 @inet_aton(i8* %1, i32* %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%16 = load i32, i32* %13
store i32 %16, i32* %15
%17 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%18 = call i32 @connect(i32 %0, %struct.sockaddr* %17, i32 12)
%19 = icmp sgt i32 %18, -1
%20 = select i1 %19, i32 %18, i32 -1
ret i32 %20
}