#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fd_close(int);
scalar_t__ fd_tcp_nodelay(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int fd_connect(int family, const struct sockaddr *addr, socklen_t addrlen) {
  int fd;
  if ((fd = socket(family, SOCK_STREAM, 0)) == -1)
    goto error;
  if (connect(fd, addr, addrlen) == -1) {
    fprintf(stderr, "%s\n", strerror(errno));
    goto error;
  }
  if (fd_tcp_nodelay(fd) != 0)
    goto error;
  return fd;
error:
  fd_close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<fd_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1196 <fd_connect+0x36>
jmp    1201 <fd_connect+0xa1>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11df <fd_connect+0x7f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe2b(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1201 <fd_connect+0xa1>
mov    -0x18(%rbp),%edi
call   1050 <fd_tcp_nodelay@plt>
cmp    $0x0,%rax
je     11f6 <fd_connect+0x96>
jmp    1201 <fd_connect+0xa1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1210 <fd_connect+0xb0>
mov    -0x18(%rbp),%edi
call   1030 <fd_close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_connect>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11c9 <fd_connect+0x69>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     11a1 <fd_connect+0x41>
mov    %ebx,%edi
call   1050 <fd_tcp_nodelay@plt>
test   %rax,%rax
jne    11c9 <fd_connect+0x69>
jmp    11d5 <fd_connect+0x75>
mov    0x2e38(%rip),%rax
mov    (%rax),%ebp
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe41(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    %ebx,%edi
call   1030 <fd_close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @fd_connect(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %3
br label %32
14: ; preds = %3
%15 = load i32, i32* %8
%16 = load %struct.sockaddr*, %struct.sockaddr** %6
%17 = load i32, i32* %7
%18 = call i32 @connect(i32 %15, %struct.sockaddr* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %32
25: ; preds = %14
%26 = load i32, i32* %8
%27 = call i64 @fd_tcp_nodelay(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
br label %32
30: ; preds = %25
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %35
32: ; preds = %29, %20, %13
%33 = load i32, i32* %8
%34 = call i32 @fd_close(i32 %33)
store i32 -1, i32* %4
br label %35
35: ; preds = %32, %30
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @fd_connect(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %0, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = tail call i32 @connect(i32 %5, %struct.sockaddr* %1, i32 %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %18
15: ; preds = %7
%16 = tail call i64 @fd_tcp_nodelay(i32 %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15, %3, %10
%19 = tail call i32 @fd_close(i32 %5)
br label %20
20: ; preds = %15, %18
%21 = phi i32 [ -1, %18 ], [ %5, %15 ]
ret i32 %21
}