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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int dial(const char *host, const char *port) {
  struct addrinfo hints;
  struct addrinfo *res, *r;
  int srv;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(host, port, &hints, &res) != 0) {
    fprintf(stderr, "error: cannot resolve hostname '%s': %s\n", host,
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  for (r = res; r; r = r->ai_next) {
    srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
    if (srv == -1)
      continue;
    if (connect(srv, r->ai_addr, r->ai_addrlen) == 0)
      break;
    close(srv);
  }
  freeaddrinfo(res);
  if (!r) {
    fprintf(stderr, "error: cannot connect to host '%s'\n", host);
    exit(EXIT_FAILURE);
  }
  return srv;
}


x86 assembly (clang, -O0):
<dial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     122f <dial+0x9f>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12ab <dial+0x11b>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    126e <dial+0xde>
jmp    129a <dial+0x10a>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    1292 <dial+0x102>
jmp    12ab <dial+0x11b>
mov    -0x44(%rbp),%edi
call   1040 <close@plt>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1237 <dial+0xa7>
mov    -0x38(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x40(%rbp)
jne    12e8 <dial+0x158>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd56(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x44(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dial>:
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   1030 <memset@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
jne    127d <dial+0xed>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    1210 <dial+0x80>
xor    %edi,%edi
jmp    1250 <dial+0xc0>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     124b <dial+0xbb>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1207 <dial+0x77>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
test   %rax,%rax
jne    1200 <dial+0x70>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd65(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    126f <dial+0xdf>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [41 x i8] c"error: cannot resolve hostname '%s': %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"error: cannot connect to host '%s'\0A\00"
define i32 @dial(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%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 %49, label %24
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14, %43
%25 = phi %struct.addrinfo* [ %45, %43 ], [ %15, %14 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %43, label %34
34: ; preds = %24
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i64 @connect(i32 %32, i32 %36, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %56, label %41
41: ; preds = %34
%42 = call i32 @close(i32 %32)
br label %43
43: ; preds = %24, %41
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %47, label %24
47: ; preds = %43
%48 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %49
49: ; preds = %47, %14
%50 = phi %struct.addrinfo* [ %48, %47 ], [ null, %14 ]
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %34
%57 = load %struct.addrinfo*, %struct.addrinfo** %4
%58 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
ret i32 %32
}