#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 */
typedef int /*<<< orphan*/ tv;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr 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*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_inetsock(struct in_addr inetaddr) {
  int s;
  struct sockaddr_in dest;
  memset(&dest, 0, sizeof(struct sockaddr_in));
  dest.sin_family = AF_INET;
  dest.sin_port = htons(1723);
  dest.sin_addr = inetaddr;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return s;
  struct timeval tv;
  tv.tv_sec = 6;
  tv.tv_usec = 0;
  if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0)
    printf("Can't set the sndtimeo value!\n");
  if (connect(s, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
    close(s);
    return -1;
  }
  fcntl(s, F_SETFL, O_NONBLOCK);
  return s;
}


x86 assembly (clang, -O0):
<open_inetsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x6bb,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11ea <open_inetsock+0x6a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1282 <open_inetsock+0x102>
movl   $0x6,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1233 <open_inetsock+0xb3>
lea    0xdd2(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1262 <open_inetsock+0xe2>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <open_inetsock+0x102>
mov    -0xc(%rbp),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_inetsock>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e37(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    $0x6bb,%edi
call   1060 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,0x8(%rsp)
mov    (%r14),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     125a <open_inetsock+0xda>
movl   $0x6,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
je     121d <open_inetsock+0x9d>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     124e <open_inetsock+0xce>
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1050 <fcntl@plt>
jmp    125a <open_inetsock+0xda>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@.str = [31 x i8] c"Can't set the sndtimeo value!\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @open_inetsock(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.in_addr
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.timeval
%7 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
store i32 %0, i32* %7
%8 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @htons(i32 1723)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%14 = bitcast %struct.in_addr* %13 to i8*
%15 = bitcast %struct.in_addr* %3 to i8*
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %46
22: ; preds = %1
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 6, i32* %23
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %24
%25 = load i32, i32* %4
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_SNDTIMEO
%28 = call i64 @setsockopt(i32 %25, i32 %26, i32 %27, %struct.timeval* %6, i32 16)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %46
40: ; preds = %32
%41 = load i32, i32* %4
%42 = load i32, i32* @F_SETFL
%43 = load i32, i32* @O_NONBLOCK
%44 = call i32 @fcntl(i32 %41, i32 %42, i32 %43)
%45 = load i32, i32* %4
store i32 %45, i32* %2
br label %46
46: ; preds = %40, %37, %20
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@str = [30 x i8] c"Can't set the sndtimeo value!\00"
define i32 @open_inetsock(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.timeval
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = call i32 @htons(i32 1723)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %0, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %37, label %15
15: ; preds = %1
%16 = bitcast %struct.timeval* %3 to i8*
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 6, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_SNDTIMEO
%21 = call i64 @setsockopt(i32 %13, i32 %19, i32 %20, %struct.timeval* %3, i32 16)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i64 @connect(i32 %13, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @close(i32 %13)
br label %35
31: ; preds = %25
%32 = load i32, i32* @F_SETFL
%33 = load i32, i32* @O_NONBLOCK
%34 = call i32 @fcntl(i32 %13, i32 %32, i32 %33)
br label %35
35: ; preds = %31, %29
%36 = phi i32 [ -1, %29 ], [ %13, %31 ]
br label %37
37: ; preds = %1, %35
%38 = phi i32 [ %36, %35 ], [ %13, %1 ]
ret i32 %38
}