#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*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to(const char *host, const char *port) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  struct addrinfo *result, *rp;
  if (getaddrinfo(host, port, &hints, &result) != 0) {
    printf("dns failed for %s\n", host);
    return -1;
  }
  int sfd;
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1) {
      continue;
    }
    if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) {
      break;
    } else {
      close(sfd);
    }
  }
  freeaddrinfo(result);
  if (rp == NULL) {
    printf("connect failed for %s\n", host);
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11e3 <connect_to+0x73>
mov    -0x10(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <connect_to+0x129>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1263 <connect_to+0xf3>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1222 <connect_to+0xb2>
jmp    1252 <connect_to+0xe2>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     1245 <connect_to+0xd5>
jmp    1263 <connect_to+0xf3>
mov    -0x4c(%rbp),%edi
call   1050 <close@plt>
jmp    1252 <connect_to+0xe2>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    11eb <connect_to+0x7b>
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
cmpq   $0x0,-0x48(%rbp)
jne    1293 <connect_to+0x123>
mov    -0x10(%rbp),%rsi
lea    0xd91(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <connect_to+0x129>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
je     11cd <connect_to+0x5d>
lea    0xe35(%rip),%rdi
jmp    1229 <connect_to+0xb9>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11e9 <connect_to+0x79>
xor    %edi,%edi
jmp    121d <connect_to+0xad>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1218 <connect_to+0xa8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11e0 <connect_to+0x70>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    1241 <connect_to+0xd1>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11e0 <connect_to+0x70>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
lea    0xdea(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
jmp    1236 <connect_to+0xc6>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"dns failed for %s\0A\00"
@.str.1 = [23 x i8] c"connect failed for %s\0A\00"
define i32 @connect_to(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = call i64 @getaddrinfo(i8* %15, i8* %16, %struct.addrinfo* %6, %struct.addrinfo** %7)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %70
22: ; preds = %2
%23 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %23, %struct.addrinfo** %8
br label %24
24: ; preds = %56, %22
%25 = load %struct.addrinfo*, %struct.addrinfo** %8
%26 = icmp ne %struct.addrinfo* %25, null
br i1 %26, label %27, label %60
27: ; preds = %24
%28 = load %struct.addrinfo*, %struct.addrinfo** %8
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 4
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %8
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 3
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %8
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %30, i32 %33, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %27
br label %56
41: ; preds = %27
%42 = load i32, i32* %9
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %8
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @connect(i32 %42, i32 %45, i32 %48)
%50 = icmp ne i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %41
br label %60
52: ; preds = %41
%53 = load i32, i32* %9
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %52
br label %56
56: ; preds = %55, %40
%57 = load %struct.addrinfo*, %struct.addrinfo** %8
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 5
%59 = load %struct.addrinfo*, %struct.addrinfo** %58
store %struct.addrinfo* %59, %struct.addrinfo** %8
br label %24
60: ; preds = %51, %24
%61 = load %struct.addrinfo*, %struct.addrinfo** %7
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %61)
%63 = load %struct.addrinfo*, %struct.addrinfo** %8
%64 = icmp eq %struct.addrinfo* %63, null
br i1 %64, label %65, label %68
65: ; preds = %60
%66 = load i8*, i8** %4
%67 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %66)
store i32 -1, i32* %3
br label %70
68: ; preds = %60
%69 = load i32, i32* %9
store i32 %69, i32* %3
br label %70
70: ; preds = %68, %65, %19
%71 = load i32, i32* %3
ret i32 %71
}

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