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


x86 assembly (clang, -O0):
<dial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0x2ed9(%rip),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2ece(%rip)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2ebb(%rip)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    0x2ea0(%rip),%rdx
lea    -0x20(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11e3 <dial+0x73>
mov    -0x8(%rbp),%rsi
lea    0xe22(%rip),%rdi
call   1090 <eprint@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125e <dial+0xee>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1221 <dial+0xb1>
jmp    124d <dial+0xdd>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jne    1245 <dial+0xd5>
jmp    125e <dial+0xee>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11eb <dial+0x7b>
mov    -0x20(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1282 <dial+0x112>
mov    -0x8(%rbp),%rsi
lea    0xda8(%rip),%rdi
call   1090 <eprint@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0x2edb(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2ecd(%rip)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2eba(%rip)
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <getaddrinfo@plt>
test   %rax,%rax
je     11d9 <dial+0x69>
lea    0xe2f(%rip),%rdi
mov    %r14,%rsi
call   1090 <eprint@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11f9 <dial+0x89>
xor    %edi,%edi
jmp    122d <dial+0xbd>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1228 <dial+0xb8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11f0 <dial+0x80>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1050 <connect@plt>
test   %rax,%rax
je     1243 <dial+0xd3>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11f0 <dial+0x80>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
lea    0xdec(%rip),%rdi
mov    %r14,%rsi
call   1090 <eprint@plt>
jmp    124d <dial+0xdd>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@dial.hints = internal global %struct.addrinfo zeroinitializer
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [37 x i8] c"error: cannot resolve hostname '%s':\00"
@.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 = bitcast %struct.addrinfo** %3 to i8*
%5 = tail call i32 @memset(%struct.addrinfo* @dial.hints, i32 0, i32 32)
%6 = load i32, i32* @AF_UNSPEC
store i32 %6, i32* getelementptr inbounds (%struct.addrinfo, %struct.addrinfo* @dial.hints, i64 0, i32 4)
%7 = load i32, i32* @SOCK_STREAM
store i32 %7, i32* getelementptr inbounds (%struct.addrinfo, %struct.addrinfo* @dial.hints, i64 0, i32 3)
%8 = call i64 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* @dial.hints, %struct.addrinfo** %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @eprint(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %2
%13 = load %struct.addrinfo*, %struct.addrinfo** %3
%14 = icmp eq %struct.addrinfo* %13, null
br i1 %14, label %43, label %15
15: ; preds = %12, %34
%16 = phi %struct.addrinfo* [ %36, %34 ], [ %13, %12 ]
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 4
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 3
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 2
%22 = load i32, i32* %21
%23 = call i32 @socket(i32 %18, i32 %20, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %34, label %25
25: ; preds = %15
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @connect(i32 %23, i32 %27, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %25
%33 = call i32 @close(i32 %23)
br label %34
34: ; preds = %15, %32
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 5
%36 = load %struct.addrinfo*, %struct.addrinfo** %35
%37 = icmp eq %struct.addrinfo* %36, null
br i1 %37, label %41, label %15
38: ; preds = %25
%39 = load %struct.addrinfo*, %struct.addrinfo** %3
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
br label %48
41: ; preds = %34
%42 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %43
43: ; preds = %41, %12
%44 = phi %struct.addrinfo* [ null, %12 ], [ %42, %41 ]
%45 = phi i32 [ undef, %12 ], [ %23, %41 ]
%46 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
%47 = call i32 @eprint(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %48
48: ; preds = %38, %43
%49 = phi i32 [ %45, %43 ], [ %23, %38 ]
ret i32 %49
}