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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ noly_set_tcp_nodelay(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bee_local_connect(char *ip, int port) {
  if (!ip || port < 0 || port > 65535)
    return -1;
  int sk;
  struct sockaddr_in dest;
  sk = socket(AF_INET, SOCK_STREAM, 0);
  if (sk < 0)
    return (-1);
  bzero(&dest, sizeof(dest));
  dest.sin_family = AF_INET;
  dest.sin_port = htons(port);
  dest.sin_addr.s_addr = inet_addr(ip);
  noly_set_tcp_nodelay(sk);
  int ret = connect(sk, (struct sockaddr *)&dest, sizeof(dest));
  if (ret == 0)
    return sk;
  return (-1);
}


x86 assembly (clang, -O0):
<bee_local_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1191 <bee_local_connect+0x31>
cmpl   $0x0,-0x14(%rbp)
jl     1191 <bee_local_connect+0x31>
cmpl   $0xffff,-0x14(%rbp)
jle    119d <bee_local_connect+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <bee_local_connect+0xd8>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cf <bee_local_connect+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <bee_local_connect+0xd8>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <noly_set_tcp_nodelay@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1231 <bee_local_connect+0xd1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1238 <bee_local_connect+0xd8>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bee_local_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     11fd <bee_local_connect+0x9d>
mov    %esi,%ebp
cmp    $0xffff,%esi
ja     11fd <bee_local_connect+0x9d>
mov    %rdi,%rbx
mov    0x2e5a(%rip),%r12
mov    (%r12),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f8 <bee_local_connect+0x98>
mov    %eax,%r14d
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %r14d,%edi
call   1030 <noly_set_tcp_nodelay@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
neg    %eax
sbb    %eax,%eax
or     %r14d,%eax
jmp    11fd <bee_local_connect+0x9d>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @bee_local_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* %5
%16 = icmp sgt i32 %15, 65535
br i1 %16, label %17, label %18
17: ; preds = %14, %11, %2
store i32 -1, i32* %3
br label %46
18: ; preds = %14
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -1, i32* %3
br label %46
25: ; preds = %18
%26 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* %5
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %30, i32* %31
%32 = load i8*, i8** %4
%33 = call i32 @inet_addr(i8* %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i32, i32* %6
%37 = call i32 @noly_set_tcp_nodelay(i32 %36)
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%40 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %25
%44 = load i32, i32* %6
store i32 %44, i32* %3
br label %46
45: ; preds = %25
store i32 -1, i32* %3
br label %46
46: ; preds = %45, %43, %24, %17
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @bee_local_connect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp eq i8* %0, null
%5 = icmp ugt i32 %1, 65535
%6 = or i1 %4, %5
br i1 %6, label %28, label %7
7: ; preds = %2
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %7
%14 = call i32 @bzero(%struct.sockaddr_in* %3, 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 = tail call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = tail call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @noly_set_tcp_nodelay(i32 %11)
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i32 @connect(i32 %11, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, 0
%25 = select i1 %24, i32 %11, i32 -1
br label %26
26: ; preds = %7, %13
%27 = phi i32 [ %25, %13 ], [ -1, %7 ]
br label %28
28: ; preds = %2, %26
%29 = phi i32 [ %27, %26 ], [ -1, %2 ]
ret i32 %29
}