#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strtok(char *, char *);
int check_con_status(const char *addr_str, int addr_len) {
  int sockfd, rc, port;
  char *tok, *ipaddr, *portstr, addr[32] = {0};
  struct sockaddr_in servaddr;
  strncpy(addr, addr_str, addr_len);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    return -1;
  }
  tok = addr;
  ;
  ipaddr = strtok(tok, ":");
  portstr = addr + strlen(ipaddr) + 1;
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  rc = inet_pton(AF_INET, ipaddr, &servaddr.sin_addr);
  if (rc != 1) {
    close(sockfd);
    return -1;
  }
  port = atoi(portstr);
  servaddr.sin_port = htons(port);
  rc = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (rc != 0) {
    close(sockfd);
    return -1;
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<check_con_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1214 <check_con_status+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <check_con_status+0x152>
lea    -0x60(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdd9(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x30(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
lea    -0x70(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x70(%rbp),%rdx
add    $0x4,%rdx
call   1080 <inet_pton@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
je     12aa <check_con_status+0xfa>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <check_con_status+0x152>
mov    -0x38(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x70(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12f3 <check_con_status+0x143>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <check_con_status+0x152>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_con_status>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %esi,%edx
mov    %rdi,%rsi
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rdi
call   1030 <strncpy@plt>
mov    0x2e19(%rip),%r12
mov    (%r12),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     127a <check_con_status+0xda>
mov    %eax,%ebx
lea    0xe05(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <strtok@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %rsp,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    (%r12),%edi
mov    %edi,0x8(%rsp)
lea    0x4(%rsp),%rdx
mov    %rbp,%rsi
call   1070 <inet_pton@plt>
cmp    $0x1,%eax
jne    1273 <check_con_status+0xd3>
movslq %r14d,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x11,%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
neg    %ebp
sbb    %r15d,%r15d
jmp    127a <check_con_status+0xda>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [2 x i8] c":\00"
define i32 @check_con_status(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [32 x i8]
%13 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%14 = bitcast [32 x i8]* %12 to i8*
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = load i32, i32* %5
%18 = call i32 @strncpy(i8* %15, i8* %16, i32 %17)
%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 eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
br label %64
25: ; preds = %2
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
store i8* %26, i8** %9
%27 = load i8*, i8** %9
%28 = call i8* @strtok(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %28, i8** %10
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%30 = load i8*, i8** %10
%31 = call i32 @strlen(i8* %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8* %34, i8** %11
%35 = call i32 @bzero(%struct.sockaddr_in* %13, i32 12)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* @AF_INET
%39 = load i8*, i8** %10
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%41 = call i32 @inet_pton(i32 %38, i8* %39, i32* %40)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp ne i32 %42, 1
br i1 %43, label %44, label %47
44: ; preds = %25
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %64
47: ; preds = %25
%48 = load i8*, i8** %11
%49 = call i32 @atoi(i8* %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = call i32 @htons(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
store i32 %51, i32* %52
%53 = load i32, i32* %6
%54 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%55 = call i32 @connect(i32 %53, %struct.sockaddr* %54, i32 12)
store i32 %55, i32* %7
%56 = load i32, i32* %7
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %47
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %3
br label %64
61: ; preds = %47
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
store i32 0, i32* %3
br label %64
64: ; preds = %61, %58, %44, %24
%65 = load i32, i32* %3
ret i32 %65
}

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