#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;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int remote_connect(char *host, char *port, struct addrinfo hints) {
  struct addrinfo *res, *res0;
  int s, error;
  if ((error = getaddrinfo(host, port, &hints, &res)))
    errx(1, "getaddrinfo: %s", gai_strerror(error));
  res0 = res;
  do {
    if ((s = socket(res0->ai_family, res0->ai_socktype, res0->ai_protocol)) < 0)
      continue;
    if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
      break;
    close(s);
    s = -1;
  } while ((res0 = res0->ai_next) != NULL);
  freeaddrinfo(res);
  return (s);
}


x86 assembly (clang, -O0):
<remote_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0x10(%rbp),%rdx
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     11bc <remote_connect+0x4c>
mov    -0x28(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe49(%rip),%rsi
call   1030 <errx@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ef <remote_connect+0x7f>
jmp    1222 <remote_connect+0xb2>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jne    1213 <remote_connect+0xa3>
jmp    1238 <remote_connect+0xc8>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c4 <remote_connect+0x54>
mov    -0x18(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remote_connect>:
push   %rbp
push   %rbx
push   %rax
lea    0x20(%rsp),%rdx
mov    %rsp,%rcx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     119e <remote_connect+0x2e>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe6e(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
mov    (%rsp),%rbx
jmp    11b9 <remote_connect+0x49>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     11ec <remote_connect+0x7c>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11b0 <remote_connect+0x40>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1060 <connect@plt>
test   %rax,%rax
je     11ec <remote_connect+0x7c>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
jmp    11b0 <remote_connect+0x40>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [16 x i8] c"getaddrinfo: %s\00"
define i32 @remote_connect(i8* %0, i8* %1, %struct.addrinfo* byval(%struct.addrinfo) align 8 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i8*, i8** %5
%12 = call i32 @getaddrinfo(i8* %10, i8* %11, %struct.addrinfo* %2, %struct.addrinfo** %6)
store i32 %12, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* %9
%16 = call i32 @gai_strerror(i32 %15)
%17 = call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %3
%19 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %19, %struct.addrinfo** %7
br label %20
20: ; preds = %47, %18
%21 = load %struct.addrinfo*, %struct.addrinfo** %7
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 4
%23 = load i32, i32* %22
%24 = load %struct.addrinfo*, %struct.addrinfo** %7
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i32 0, i32 3
%26 = load i32, i32* %25
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %23, i32 %26, i32 %29)
store i32 %30, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %20
br label %47
33: ; preds = %20
%34 = load i32, i32* %8
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %7
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @connect(i32 %34, i32 %37, i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %33
br label %52
44: ; preds = %33
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %8
br label %47
47: ; preds = %44, %32
%48 = load %struct.addrinfo*, %struct.addrinfo** %7
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 5
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
store %struct.addrinfo* %50, %struct.addrinfo** %7
%51 = icmp ne %struct.addrinfo* %50, null
br i1 %51, label %20, label %52
52: ; preds = %47, %43
%53 = load %struct.addrinfo*, %struct.addrinfo** %6
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
%55 = load i32, i32* %8
ret i32 %55
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [16 x i8] c"getaddrinfo: %s\00"
define i32 @remote_connect(i8* %0, i8* %1, %struct.addrinfo* byval(%struct.addrinfo) align 8 %2) {
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo** %4 to i8*
%6 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %2, %struct.addrinfo** %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = call i32 @gai_strerror(i32 %6)
%10 = call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %8, %3
%12 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %13
13: ; preds = %32, %11
%14 = phi %struct.addrinfo* [ %12, %11 ], [ %35, %32 ]
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 4
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 3
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %16, i32 %18, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %13
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @connect(i32 %21, i32 %25, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %23
%31 = call i32 @close(i32 %21)
br label %32
32: ; preds = %13, %30
%33 = phi i32 [ %21, %13 ], [ -1, %30 ]
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 5
%35 = load %struct.addrinfo*, %struct.addrinfo** %34
%36 = icmp eq %struct.addrinfo* %35, null
br i1 %36, label %37, label %13
37: ; preds = %23, %32
%38 = phi i32 [ %33, %32 ], [ %21, %23 ]
%39 = load %struct.addrinfo*, %struct.addrinfo** %4
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
ret i32 %38
}