#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;
};
struct in_addr {
  int dummy;
};
typedef int /*<<< orphan*/ dest;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ ipc_setnoblocking(int *);
int /*<<< orphan*/ logger(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int vs_connect_initiative(char *ip, int port, int *socket_id) {
  struct sockaddr_in dest;
  if (!ip || !port) {
    return -1;
  }
  if ((*socket_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    logger(3, "Vs_Connect_Initiative socket error!");
    return -1;
  }
  bzero(&dest, sizeof(dest));
  dest.sin_family = AF_INET;
  dest.sin_port = htons(port);
  if (inet_aton(ip, (struct in_addr *)&dest.sin_addr.s_addr) == 0) {
    return -1;
  }
  ipc_setnoblocking(socket_id);
  if (connect(*socket_id, (struct sockaddr *)&dest, sizeof(dest)) != 0) {
    logger(3, "Vs_Connect_Initiative connet error!");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<vs_connect_initiative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1198 <vs_connect_initiative+0x28>
cmpl   $0x0,-0x14(%rbp)
jne    11a4 <vs_connect_initiative+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <vs_connect_initiative+0x10c>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    -0x20(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    11e9 <vs_connect_initiative+0x79>
mov    $0x3,%edi
lea    0xe28(%rip),%rsi
call   1040 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <vs_connect_initiative+0x10c>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%rax
jne    1231 <vs_connect_initiative+0xc1>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <vs_connect_initiative+0x10c>
mov    -0x20(%rbp),%rdi
call   1080 <ipc_setnoblocking@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     1275 <vs_connect_initiative+0x105>
mov    $0x3,%edi
lea    0xdc0(%rip),%rsi
call   1040 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <vs_connect_initiative+0x10c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vs_connect_initiative>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1232 <vs_connect_initiative+0xc2>
mov    %esi,%ebx
test   %esi,%esi
je     1232 <vs_connect_initiative+0xc2>
mov    %rdx,%r15
mov    %rdi,%rbp
mov    0x2e44(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,(%r15)
test   %eax,%eax
js     121c <vs_connect_initiative+0xac>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <inet_aton@plt>
test   %rax,%rax
je     1232 <vs_connect_initiative+0xc2>
mov    %r15,%rdi
call   1080 <ipc_setnoblocking@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
je     122f <vs_connect_initiative+0xbf>
lea    0xe0a(%rip),%rsi
jmp    1223 <vs_connect_initiative+0xb3>
lea    0xddd(%rip),%rsi
mov    $0x3,%edi
call   1040 <logger@plt>
jmp    1232 <vs_connect_initiative+0xc2>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
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.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"Vs_Connect_Initiative socket error!\00"
@.str.1 = [36 x i8] c"Vs_Connect_Initiative connet error!\00"
define i32 @vs_connect_initiative(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %11, %3
store i32 -1, i32* %4
br label %48
15: ; preds = %11
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = load i32*, i32** %7
store i32 %18, i32* %19
%20 = icmp slt i32 %18, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @logger(i32 3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
23: ; preds = %15
%24 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %6
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i8*, i8** %5
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
%33 = bitcast i32* %32 to %struct.in_addr*
%34 = call i64 @inet_aton(i8* %30, %struct.in_addr* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %23
store i32 -1, i32* %4
br label %48
37: ; preds = %23
%38 = load i32*, i32** %7
%39 = call i32 @ipc_setnoblocking(i32* %38)
%40 = load i32*, i32** %7
%41 = load i32, i32* %40
%42 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%43 = call i64 @connect(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 @logger(i32 3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
47: ; preds = %37
store i32 0, i32* %4
br label %48
48: ; preds = %47, %45, %36, %21, %14
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"Vs_Connect_Initiative socket error!\00"
@.str.1 = [36 x i8] c"Vs_Connect_Initiative connet error!\00"
define i32 @vs_connect_initiative(i8* %0, i32 %1, i32* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = icmp ne i8* %0, null
%7 = icmp ne i32 %1, 0
%8 = and i1 %6, %7
br i1 %8, label %9, label %33
9: ; preds = %3
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @logger(i32 3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %33
16: ; preds = %9
%17 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %4 to %struct.in_addr*
%23 = call i64 @inet_aton(i8* %0, %struct.in_addr* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %16
%26 = call i32 @ipc_setnoblocking(i32* %2)
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i64 @connect(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = call i32 @logger(i32 3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %25, %16, %3, %31, %14
%34 = phi i32 [ -1, %14 ], [ -1, %31 ], [ -1, %3 ], [ -1, %16 ], [ 0, %25 ]
ret i32 %34
}