#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;
};
struct addrinfo {
  int ai_addrlen;
  struct sockaddr *ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ strport;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ F_SETFL;
int NI_MAXSERV;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int stream_request_connect(const char *address, int port) {
  struct addrinfo ai, *aitop;
  char strport[NI_MAXSERV];
  struct sockaddr *sa;
  int slen;
  int fd;
  memset(&ai, 0, sizeof(struct addrinfo));
  ai.ai_family = AF_INET;
  ai.ai_socktype = SOCK_STREAM;
  snprintf(strport, sizeof(strport), "%d", port);
  if (getaddrinfo(address, strport, &ai, &aitop) != 0) {
    fprintf(stderr, "ERROR: getaddrinfo() failed\n");
    return -1;
  }
  sa = aitop->ai_addr;
  slen = aitop->ai_addrlen;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    fprintf(stderr, "ERROR: socket() failed\n");
    return -1;
  }
  if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
    fprintf(stderr, "ERROR: fcntl(O_NONBLOCK) failed\n");
    return -1;
  }
  if (connect(fd, sa, slen) == -1) {
    if (errno != EINPROGRESS) {
      fprintf(stderr, "ERROR: connect() failed\n");
      return -1;
    }
  }
  freeaddrinfo(aitop);
  return fd;
}


x86 assembly (clang, -O0):
<stream_request_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    0x2e0a(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,-0x1c(%rbp)
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,-0x20(%rbp)
mov    %eax,%esi
mov    -0x14(%rbp),%ecx
lea    0xe05(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x68(%rbp),%rsi
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1247 <stream_request_connect+0xc7>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    136a <stream_request_connect+0x1ea>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    12aa <stream_request_connect+0x12a>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    136a <stream_request_connect+0x1ea>
mov    -0x58(%rbp),%edi
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12f5 <stream_request_connect+0x175>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd5b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    136a <stream_request_connect+0x1ea>
mov    -0x58(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1354 <stream_request_connect+0x1d4>
mov    0x2c94(%rip),%rax
mov    (%rax),%rax
mov    0x2cba(%rip),%rcx
cmp    (%rcx),%rax
je     134f <stream_request_connect+0x1cf>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    136a <stream_request_connect+0x1ea>
jmp    1354 <stream_request_connect+0x1d4>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stream_request_connect>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rsp,-0x30(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x50(%rbp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    0xe15(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15d,%ecx
call   1040 <snprintf@plt>
lea    -0x38(%rbp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
je     1242 <stream_request_connect+0xc2>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%r14
mov    (%rax),%r15d
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c1 <stream_request_connect+0x141>
mov    %eax,%ebx
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12d6 <stream_request_connect+0x156>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b3 <stream_request_connect+0x133>
mov    0x2d04(%rip),%rax
mov    (%rax),%rax
mov    0x2d2a(%rip),%rcx
cmp    (%rcx),%rax
jne    12eb <stream_request_connect+0x16b>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    122a <stream_request_connect+0xaa>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
jmp    1220 <stream_request_connect+0xa0>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    0xd52(%rip),%rsi
jmp    1220 <stream_request_connect+0xa0>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
jmp    1220 <stream_request_connect+0xa0>

LLVM IR (-O0):
%struct.addrinfo = type { i32, %struct.sockaddr*, i32, i32 }
%struct.sockaddr = type { i32 }
@NI_MAXSERV = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"ERROR: getaddrinfo() failed\0A\00"
@.str.2 = [24 x i8] c"ERROR: socket() failed\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [33 x i8] c"ERROR: fcntl(O_NONBLOCK) failed\0A\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.4 = [25 x i8] c"ERROR: connect() failed\0A\00"
define i32 @stream_request_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.sockaddr*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%14 = load i32, i32* @NI_MAXSERV
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i32 %21, i32* %22
%23 = trunc i64 %15 to i32
%24 = load i32, i32* %5
%25 = call i32 @snprintf(i8* %17, i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = load i8*, i8** %4
%27 = call i64 @getaddrinfo(i8* %26, i8* %17, %struct.addrinfo* %6, %struct.addrinfo** %7)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %2
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %74
32: ; preds = %2
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 1
%35 = load %struct.sockaddr*, %struct.sockaddr** %34
store %struct.sockaddr* %35, %struct.sockaddr** %10
%36 = load %struct.addrinfo*, %struct.addrinfo** %7
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 0
%38 = load i32, i32* %37
store i32 %38, i32* %11
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %32
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %74
47: ; preds = %32
%48 = load i32, i32* %12
%49 = load i32, i32* @F_SETFL
%50 = load i32, i32* @O_NONBLOCK
%51 = call i32 @fcntl(i32 %48, i32 %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %74
56: ; preds = %47
%57 = load i32, i32* %12
%58 = load %struct.sockaddr*, %struct.sockaddr** %10
%59 = load i32, i32* %11
%60 = call i32 @connect(i32 %57, %struct.sockaddr* %58, i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %70
62: ; preds = %56
%63 = load i64, i64* @errno
%64 = load i64, i64* @EINPROGRESS
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %74
69: ; preds = %62
br label %70
70: ; preds = %69, %56
%71 = load %struct.addrinfo*, %struct.addrinfo** %7
%72 = call i32 @freeaddrinfo(%struct.addrinfo* %71)
%73 = load i32, i32* %12
store i32 %73, i32* %3
store i32 1, i32* %13
br label %74
74: ; preds = %70, %66, %53, %44, %29
%75 = load i8*, i8** %8
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.sockaddr*, i32, i32 }
%struct.sockaddr = type { i32 }
@NI_MAXSERV = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"ERROR: getaddrinfo() failed\0A\00"
@.str.2 = [24 x i8] c"ERROR: socket() failed\0A\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [33 x i8] c"ERROR: fcntl(O_NONBLOCK) failed\0A\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.4 = [25 x i8] c"ERROR: connect() failed\0A\00"
define i32 @stream_request_connect(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = load i32, i32* @NI_MAXSERV
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @snprintf(i8* %10, i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%17 = call i64 @getaddrinfo(i8* %0, i8* %10, %struct.addrinfo* %3, %struct.addrinfo** %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %56
22: ; preds = %2
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%25 = load %struct.sockaddr*, %struct.sockaddr** %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %56
35: ; preds = %22
%36 = load i32, i32* @F_SETFL
%37 = load i32, i32* @O_NONBLOCK
%38 = call i32 @fcntl(i32 %30, i32 %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %56
43: ; preds = %35
%44 = call i32 @connect(i32 %30, %struct.sockaddr* %25, i32 %27)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINPROGRESS
%49 = icmp eq i64 %47, %48
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %56
53: ; preds = %46, %43
%54 = load %struct.addrinfo*, %struct.addrinfo** %4
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
br label %56
56: ; preds = %53, %50, %40, %32, %19
%57 = phi i32 [ -1, %19 ], [ -1, %32 ], [ -1, %40 ], [ -1, %50 ], [ %30, %53 ]
ret i32 %57
}