#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;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
scalar_t__ EALREADY;
scalar_t__ EBADF;
scalar_t__ EINVAL;
scalar_t__ EISCONN;
scalar_t__ ENOTSOCK;
scalar_t__ EOPNOTSUPP;
int /*<<< orphan*/ assert(int);
int connect(int const, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int sock_close(int const, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(const struct addrinfo *const ai) {
  const int fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  if (fd == -1)
    return (-1);
  if (connect(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
    assert(errno != EALREADY);
    assert(errno != EBADF);
    assert(errno != EISCONN);
    assert(errno != ENOTSOCK);
    assert(errno != EINVAL);
    assert(errno != EOPNOTSUPP);
    return sock_close(fd, (-4));
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    117f <tcp_connect+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <tcp_connect+0x13e>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1278 <tcp_connect+0x138>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x14(%rbp),%edi
mov    $0xfffffffc,%esi
call   1040 <sock_close@plt>
mov    %eax,-0x4(%rbp)
jmp    127e <tcp_connect+0x13e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x10(%rdi),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     121d <tcp_connect+0xdd>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    1222 <tcp_connect+0xe2>
mov    0x2e45(%rip),%rbx
mov    (%rbx),%rax
mov    0x2e4b(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1030 <assert@plt>
mov    (%rbx),%rax
mov    0x2e13(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1030 <assert@plt>
mov    (%rbx),%rax
mov    0x2e33(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1030 <assert@plt>
mov    (%rbx),%rax
mov    0x2e0b(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1030 <assert@plt>
mov    (%rbx),%rax
mov    0x2dd3(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1030 <assert@plt>
mov    (%rbx),%rax
mov    0x2dcb(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1030 <assert@plt>
mov    %ebp,%edi
mov    $0xfffffffc,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <sock_close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@errno = global i64 0
@EALREADY = global i64 0
@EBADF = global i64 0
@EISCONN = global i64 0
@ENOTSOCK = global i64 0
@EINVAL = global i64 0
@EOPNOTSUPP = global i64 0
define i32 @tcp_connect(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %3
%5 = load %struct.addrinfo*, %struct.addrinfo** %3
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
%7 = load i32, i32* %6
%8 = load %struct.addrinfo*, %struct.addrinfo** %3
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
%10 = load i32, i32* %9
%11 = load %struct.addrinfo*, %struct.addrinfo** %3
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 2
%13 = load i32, i32* %12
%14 = call i32 @socket(i32 %7, i32 %10, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %63
18: ; preds = %1
%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 eq i32 %26, -1
br i1 %27, label %28, label %61
28: ; preds = %18
%29 = load i64, i64* @errno
%30 = load i64, i64* @EALREADY
%31 = icmp ne i64 %29, %30
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i64, i64* @errno
%35 = load i64, i64* @EBADF
%36 = icmp ne i64 %34, %35
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i64, i64* @errno
%40 = load i64, i64* @EISCONN
%41 = icmp ne i64 %39, %40
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i64, i64* @errno
%45 = load i64, i64* @ENOTSOCK
%46 = icmp ne i64 %44, %45
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i64, i64* @errno
%50 = load i64, i64* @EINVAL
%51 = icmp ne i64 %49, %50
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = load i64, i64* @errno
%55 = load i64, i64* @EOPNOTSUPP
%56 = icmp ne i64 %54, %55
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
%59 = load i32, i32* %4
%60 = call i32 @sock_close(i32 %59, i32 -4)
store i32 %60, i32* %2
br label %63
61: ; preds = %18
%62 = load i32, i32* %4
store i32 %62, i32* %2
br label %63
63: ; preds = %61, %28, %17
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@errno = global i64 0
@EALREADY = global i64 0
@EBADF = global i64 0
@EISCONN = global i64 0
@ENOTSOCK = global i64 0
@EINVAL = global i64 0
@EOPNOTSUPP = global i64 0
define i32 @tcp_connect(%struct.addrinfo* nocapture %0) {
%2 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 4
%3 = load i32, i32* %2
%4 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 3
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%7 = load i32, i32* %6
%8 = tail call i32 @socket(i32 %3, i32 %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %49, label %10
10: ; preds = %1
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @connect(i32 %8, i32 %12, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %49
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EALREADY
%20 = icmp ne i64 %18, %19
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = load i64, i64* @errno
%24 = load i64, i64* @EBADF
%25 = icmp ne i64 %23, %24
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = load i64, i64* @errno
%29 = load i64, i64* @EISCONN
%30 = icmp ne i64 %28, %29
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOTSOCK
%35 = icmp ne i64 %33, %34
%36 = zext i1 %35 to i32
%37 = tail call i32 @assert(i32 %36)
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINVAL
%40 = icmp ne i64 %38, %39
%41 = zext i1 %40 to i32
%42 = tail call i32 @assert(i32 %41)
%43 = load i64, i64* @errno
%44 = load i64, i64* @EOPNOTSUPP
%45 = icmp ne i64 %43, %44
%46 = zext i1 %45 to i32
%47 = tail call i32 @assert(i32 %46)
%48 = tail call i32 @sock_close(i32 %8, i32 -4)
br label %49
49: ; preds = %10, %1, %17
%50 = phi i32 [ %48, %17 ], [ -1, %1 ], [ %8, %10 ]
ret i32 %50
}