#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*/ server_addr;
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ fnet_set_nonblocking(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int fnet_conn_async(const char *ip, in_port_t port, int *outfd) {
  int sockfd;
  struct sockaddr_in server_addr;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    return -1;
  }
  *outfd = sockfd;
  fnet_set_nonblocking(sockfd);
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = inet_addr(ip);
  int r = connect(sockfd, (struct sockaddr *)(&server_addr),
                  sizeof(struct sockaddr));
  if (r == -1) {
    if (errno == EINPROGRESS) {
      return 1;
    } else {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<fnet_conn_async>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11a4 <fnet_conn_async+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <fnet_conn_async+0xdf>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%edi
call   1030 <fnet_set_nonblocking@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1238 <fnet_conn_async+0xd8>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    122c <fnet_conn_async+0xcc>
movl   $0x1,-0x4(%rbp)
jmp    123f <fnet_conn_async+0xdf>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <fnet_conn_async+0xdf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnet_conn_async>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e6a(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %r12d,%r12d
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <fnet_conn_async+0xb0>
mov    %eax,%ebx
mov    %eax,0x0(%rbp)
mov    %eax,%edi
call   1030 <fnet_set_nonblocking@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2de6(%rip),%rdx
xor    %esi,%esi
cmp    (%rdx),%rcx
sete   %sil
cmp    $0xffffffff,%eax
lea    -0x1(%rsi,%rsi,1),%eax
cmovne %r12d,%eax
jmp    1215 <fnet_conn_async+0xb5>
mov    $0xffffffff,%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.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @fnet_conn_async(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %43
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i32*, i32** %7
store i32 %17, i32* %18
%19 = load i32, i32* %8
%20 = call i32 @fnet_set_nonblocking(i32 %19)
%21 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %25, i32* %26
%27 = load i8*, i8** %5
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %8
%32 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%33 = call i32 @connect(i32 %31, %struct.sockaddr* %32, i32 4)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %42
36: ; preds = %16
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINPROGRESS
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 1, i32* %4
br label %43
41: ; preds = %36
store i32 -1, i32* %4
br label %43
42: ; preds = %16
store i32 0, i32* %4
br label %43
43: ; preds = %42, %41, %40, %15
%44 = load i32, i32* %4
ret i32 %44
}

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
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @fnet_conn_async(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %27, label %10
10: ; preds = %3
store i32 %8, i32* %2
%11 = tail call i32 @fnet_set_nonblocking(i32 %8)
%12 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%20 = call i32 @connect(i32 %8, %struct.sockaddr* %19, i32 4)
%21 = icmp eq i32 %20, -1
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINPROGRESS
%24 = icmp eq i64 %22, %23
%25 = select i1 %24, i32 1, i32 -1
%26 = select i1 %21, i32 %25, i32 0
br label %27
27: ; preds = %3, %10
%28 = phi i32 [ %26, %10 ], [ -1, %3 ]
ret i32 %28
}