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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ INADDR_NONE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
void *inet_addr(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int initConnect(char *strSourceIP, char *strDestIP, int serverport) {
  int sockfd;
  struct sockaddr_in source_addr, dest_addr;
  source_addr.sin_addr.s_addr = inet_addr(strSourceIP);
  if (source_addr.sin_addr.s_addr == INADDR_NONE) {
    printf("sourcename is unavailable!\n");
    return -1;
  }
  dest_addr.sin_addr.s_addr = inet_addr(strDestIP);
  if (dest_addr.sin_addr.s_addr == INADDR_NONE) {
    printf("destname is unavailable!\n");
    return -1;
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("socket create error!\n");
    return -1;
  }
  dest_addr.sin_family = AF_INET;
  dest_addr.sin_port = htons(serverport);
  bzero(&(dest_addr.sin_zero), 8);
  if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) !=
      0) {
    printf("connection error!\n ");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<initConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jne    11ac <initConnect+0x4c>
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <initConnect+0x120>
mov    -0x18(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    11e5 <initConnect+0x85>
lea    0xe48(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <initConnect+0x120>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1222 <initConnect+0xc2>
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <initConnect+0x120>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x20(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     127a <initConnect+0x11a>
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <initConnect+0x120>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initConnect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
call   1060 <inet_addr@plt>
mov    0x2e54(%rip),%rbp
cmp    %rax,0x0(%rbp)
jne    119e <initConnect+0x2e>
lea    0xea9(%rip),%rdi
jmp    1227 <initConnect+0xb7>
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %rax,0x10(%rsp)
cmp    %rax,0x0(%rbp)
jne    11ba <initConnect+0x4a>
lea    0xe71(%rip),%rdi
jmp    1227 <initConnect+0xb7>
mov    0x2e27(%rip),%r14
mov    (%r14),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <initConnect+0xb0>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
je     1231 <initConnect+0xc1>
lea    0xde7(%rip),%rdi
call   1050 <printf@plt>
jmp    122c <initConnect+0xbc>
lea    0xded(%rip),%rdi
call   1030 <puts@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, i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@INADDR_NONE = global i64 0
@.str = [28 x i8] c"sourcename is unavailable!\0A\00"
@.str.1 = [26 x i8] c"destname is unavailable!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [22 x i8] c"socket create error!\0A\00"
@.str.3 = [20 x i8] c"connection error!\0A \00"
define i32 @initConnect(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = call i8* @inet_addr(i8* %11)
%13 = ptrtoint i8* %12 to i64
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 3
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i64 %13, i64* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 3
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @INADDR_NONE
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
23: ; preds = %3
%24 = load i8*, i8** %6
%25 = call i8* @inet_addr(i8* %24)
%26 = ptrtoint i8* %25 to i64
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 3
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i64 %26, i64* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 3
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* @INADDR_NONE
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
36: ; preds = %23
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
43: ; preds = %36
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %44, i32* %45
%46 = load i32, i32* %7
%47 = call i32 @htons(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%50 = call i32 @bzero(i32* %49, i32 8)
%51 = load i32, i32* %8
%52 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%53 = call i64 @connect(i32 %51, %struct.sockaddr* %52, i32 4)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %43
%56 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
57: ; preds = %43
%58 = load i32, i32* %8
store i32 %58, i32* %4
br label %59
59: ; preds = %57, %55, %41, %34, %21
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@INADDR_NONE = global i64 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [20 x i8] c"connection error!\0A \00"
@str = [21 x i8] c"socket create error!\00"
@str.4 = [25 x i8] c"destname is unavailable!\00"
@str.5 = [27 x i8] c"sourcename is unavailable!\00"
define i32 @initConnect(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = tail call i8* @inet_addr(i8* %0)
%7 = ptrtoint i8* %6 to i64
%8 = load i64, i64* @INADDR_NONE
%9 = icmp eq i64 %8, %7
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
br label %39
12: ; preds = %3
%13 = tail call i8* @inet_addr(i8* %1)
%14 = ptrtoint i8* %13 to i64
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3, i32 0
store i64 %14, i64* %15
%16 = load i64, i64* @INADDR_NONE
%17 = icmp eq i64 %16, %14
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
br label %39
20: ; preds = %12
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %39
27: ; preds = %20
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %28, i32* %29
%30 = tail call i32 @htons(i32 %2)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%33 = call i32 @bzero(i32* %32, i32 8)
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i64 @connect(i32 %23, %struct.sockaddr* %34, i32 4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %27
%38 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %27, %37, %25, %18, %10
%40 = phi i32 [ -1, %10 ], [ -1, %18 ], [ -1, %25 ], [ -1, %37 ], [ %23, %27 ]
ret i32 %40
}