#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*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
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*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int dial(const char *host, int port) {
  char portstr[32];
  int sockfd;
  struct addrinfo *result, *rp, hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  snprintf(portstr, 32, "%d", port);
  if (getaddrinfo(host, portstr, &hints, &result)) {
    perror("error: getaddrinfo");
    return -1;
  }
  for (rp = result; rp; rp = rp->ai_next) {
    sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sockfd < 0)
      continue;
    if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) >= 0)
      goto win;
    close(sockfd);
  }
  freeaddrinfo(result);
  perror("error");
  return -1;
win:
  freeaddrinfo(result);
  return sockfd;
}


x86 assembly (clang, -O0):
<dial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x20,%esi
lea    0xe33(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x78(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1209 <dial+0x89>
lea    0xe0b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <dial+0x135>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1285 <dial+0x105>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1248 <dial+0xc8>
jmp    1274 <dial+0xf4>
mov    -0x44(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jl     126c <dial+0xec>
jmp    12a6 <dial+0x126>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1211 <dial+0x91>
mov    -0x50(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
lea    0xd81(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <dial+0x135>
mov    -0x50(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0xe3c(%rip),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
jne    1260 <dial+0xe0>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
jne    1210 <dial+0x90>
xor    %edi,%edi
jmp    1252 <dial+0xd2>
xchg   %ax,%ax
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     124d <dial+0xcd>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10a0 <socket@plt>
test   %eax,%eax
js     1207 <dial+0x87>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %eax,%edi
call   1070 <connect@plt>
test   %rax,%rax
js     1200 <dial+0x80>
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
lea    0xdb8(%rip),%rdi
jmp    1267 <dial+0xe7>
lea    0xd9c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1240 <dial+0xc0>

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