#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
};

/* Variables and functions */
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int raise_socket_flags(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_by_addrinfo(struct addrinfo *address_info) {
  int handle_socket;
  if ((handle_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    return handle_socket;
  }
  if (raise_socket_flags(handle_socket, O_NONBLOCK) == -1) {
    return -1;
  }
  if (connect(handle_socket, address_info->ai_addr, address_info->ai_addrlen) !=
      -1) {
    return -1;
  }
  if (errno != EINPROGRESS) {
    return -1;
  }
  return handle_socket;
}


x86 assembly (clang, -O0):
<connect_by_addrinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1173 <connect_by_addrinfo+0x43>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ef <connect_by_addrinfo+0xbf>
mov    -0x14(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1030 <raise_socket_flags@plt>
cmp    $0xffffffff,%eax
jne    1199 <connect_by_addrinfo+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <connect_by_addrinfo+0xbf>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1040 <connect@plt>
cmp    $0xffffffff,%eax
je     11c3 <connect_by_addrinfo+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <connect_by_addrinfo+0xbf>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
je     11e9 <connect_by_addrinfo+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <connect_by_addrinfo+0xbf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_by_addrinfo>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11a7 <connect_by_addrinfo+0x77>
mov    %eax,%ebp
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <raise_socket_flags@plt>
cmp    $0xffffffff,%eax
je     11a7 <connect_by_addrinfo+0x77>
mov    (%r14),%edx
mov    0x4(%r14),%esi
mov    %ebp,%edi
call   1040 <connect@plt>
cmp    $0xffffffff,%eax
jne    11a7 <connect_by_addrinfo+0x77>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
mov    $0xffffffff,%ebx
cmove  %ebp,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @connect_by_addrinfo(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %3
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* %4
store i32 %11, i32* %2
br label %36
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @O_NONBLOCK
%15 = call i32 @raise_socket_flags(i32 %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %2
br label %36
18: ; preds = %12
%19 = load i32, i32* %4
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %3
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @connect(i32 %19, i32 %22, i32 %25)
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %18
store i32 -1, i32* %2
br label %36
29: ; preds = %18
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINPROGRESS
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -1, i32* %2
br label %36
34: ; preds = %29
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %33, %28, %17, %10
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @connect_by_addrinfo(%struct.addrinfo* nocapture %0) {
%2 = load i32, i32* @PF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = load i32, i32* @IPPROTO_TCP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %23, label %7
7: ; preds = %1
%8 = load i32, i32* @O_NONBLOCK
%9 = tail call i32 @raise_socket_flags(i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %23, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @connect(i32 %5, i32 %13, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %11
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINPROGRESS
%21 = icmp eq i64 %19, %20
%22 = select i1 %21, i32 %5, i32 -1
br label %23
23: ; preds = %18, %11, %7, %1
%24 = phi i32 [ -1, %1 ], [ -1, %7 ], [ -1, %11 ], [ %22, %18 ]
ret i32 %24
}