#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_tomahawk(int port) {
  int sock = -1;
  struct sockaddr_in saddr4;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
    return -1;
  }
  memset(&saddr4, 0, sizeof(struct sockaddr_in));
  saddr4.sin_family = AF_INET;
  saddr4.sin_addr.s_addr = htonl(0x7F000001);
  saddr4.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&saddr4, sizeof(struct sockaddr_in)) !=
      0) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_tomahawk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     11a3 <connect_to_tomahawk+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <connect_to_tomahawk+0xac>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x7f000001,%edi
call   1040 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     1206 <connect_to_tomahawk+0xa6>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <connect_to_tomahawk+0xac>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_tomahawk>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    0x2e72(%rip),%r12
mov    (%r12),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jle    11e7 <connect_to_tomahawk+0x87>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    $0x7f000001,%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
je     11e5 <connect_to_tomahawk+0x85>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11e7 <connect_to_tomahawk+0x87>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_to_tomahawk(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 -1, i32* %4
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = icmp sle i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %30
11: ; preds = %1
%12 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htonl(i32 2130706433)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = load i32, i32* %3
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %4
%22 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%23 = call i64 @connect(i32 %21, %struct.sockaddr* %22, i32 12)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %11
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %2
br label %30
28: ; preds = %11
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %25, %10
%31 = load i32, i32* %2
ret i32 %31
}

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