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


x86 assembly (clang, -O0):
<pb_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <pb_connect+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <pb_connect+0x187>
cmpq   $0x0,-0x18(%rbp)
jne    11ce <pb_connect+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <pb_connect+0x187>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x40(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x50(%rbp)
cmp    $0x0,%eax
je     125d <pb_connect+0xcd>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xdcb(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <pb_connect+0x187>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12d8 <pb_connect+0x148>
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   10b0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jge    129b <pb_connect+0x10b>
jmp    12c7 <pb_connect+0x137>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jne    12bf <pb_connect+0x12f>
jmp    12d8 <pb_connect+0x148>
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1265 <pb_connect+0xd5>
cmpq   $0x0,-0x48(%rbp)
jne    1308 <pb_connect+0x178>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd31(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <pb_connect+0x187>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pb_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     129d <pb_connect+0x10d>
mov    %rsi,%rbp
test   %rsi,%rsi
je     129d <pb_connect+0x10d>
mov    %rdi,%r15
lea    0xe41(%rip),%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1030 <printf@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     122c <pb_connect+0x9c>
mov    0x2dc8(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdf7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    129d <pb_connect+0x10d>
mov    (%rsp),%rbx
jmp    1244 <pb_connect+0xb4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1277 <pb_connect+0xe7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     1240 <pb_connect+0xb0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
test   %rax,%rax
je     1291 <pb_connect+0x101>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1240 <pb_connect+0xb0>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
jmp    129d <pb_connect+0x10d>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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