#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ destAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int create_connection(char *ip_address, unsigned short port) {
  int sock;
  struct sockaddr_in destAddr;
  char *controllerIPaddr;
  if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    fprintf(stderr, "socket() to controller failed\n");
    return -1;
  }
  memset(&destAddr, 0, sizeof(destAddr));
  destAddr.sin_family = AF_INET;
  destAddr.sin_addr.s_addr = inet_addr(ip_address);
  destAddr.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&destAddr, sizeof(destAddr)) < 0) {
    error("connect() to controller failed\n");
    return -2;
  }
  return sock;
}


x86 assembly (clang, -O0):
<create_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11d0 <create_connection+0x60>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <create_connection+0xcd>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1237 <create_connection+0xc7>
lea    0xdf9(%rip),%rdi
call   1070 <error@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    123d <create_connection+0xcd>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1207 <create_connection+0x97>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rbp,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rsp)
movzwl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    1221 <create_connection+0xb1>
lea    0xe24(%rip),%rdi
call   1070 <error@plt>
mov    $0xfffffffe,%ebx
jmp    1221 <create_connection+0xb1>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"socket() to controller failed\0A\00"
@AF_INET = global i32 0
@.str.1 = [32 x i8] c"connect() to controller failed\0A\00"
define i32 @create_connection(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i8*
store i8* %0, i8** %4
store i16 %1, i16* %5
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
17: ; preds = %2
%18 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i8*, i8** %4
%22 = call i32 @inet_addr(i8* %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i16, i16* %5
%26 = call i32 @htons(i16 zeroext %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %36
34: ; preds = %17
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %32, %14
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"socket() to controller failed\0A\00"
@AF_INET = global i32 0
@.str.1 = [32 x i8] c"connect() to controller failed\0A\00"
define i32 @create_connection(i8* %0, i16 zeroext %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 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %26
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = call i32 @htons(i16 zeroext %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i64 @connect(i32 %8, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %13, %24, %10
%27 = phi i32 [ -1, %10 ], [ -2, %24 ], [ %8, %13 ]
ret i32 %27
}