#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*/ client;

/* 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 *);
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 *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int peer_connect(int sockfd, const char *host, const char *port) {
  struct addrinfo client, *servinfo, *p;
  memset(&client, 0, sizeof(client));
  client.ai_family = AF_UNSPEC;
  client.ai_socktype = SOCK_STREAM;
  if ((getaddrinfo(host, port, &client, &servinfo)) != 0) {
    fprintf(stderr, "Unable to assemble address info\n");
    return 0;
  }
  while (1) {
    for (p = servinfo; p != NULL; p = p->ai_next) {
      if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
        fprintf(stderr, "Error unable to create socket\n");
        continue;
      }
      printf("Connecting to peer %s:%s\n", host, port);
      if (connect(sockfd, p->ai_addr, p->ai_addrlen) < 0) {
        fprintf(stderr, "Error: Connect\n");
        close(sockfd);
        continue;
      }
      break;
    }
    if (p)
      break;
  }
  printf("Connected to peer %s:%s\n", host, port);
  freeaddrinfo(servinfo);
  return 1;
}


x86 assembly (clang, -O0):
<peer_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e1a(%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   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11fb <peer_connect+0x7b>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f7 <peer_connect+0x177>
jmp    1200 <peer_connect+0x80>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12be <peer_connect+0x13e>
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   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1253 <peer_connect+0xd3>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12ad <peer_connect+0x12d>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdde(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    12a8 <peer_connect+0x128>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    12ad <peer_connect+0x12d>
jmp    12be <peer_connect+0x13e>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1208 <peer_connect+0x88>
cmpq   $0x0,-0x48(%rbp)
je     12ce <peer_connect+0x14e>
jmp    12d3 <peer_connect+0x153>
jmp    1200 <peer_connect+0x80>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd88(%rip),%rdi
call   1030 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peer_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x8(%rsp),%r15
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
je     11f4 <peer_connect+0x74>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12a5 <peer_connect+0x125>
mov    0x2ddd(%rip),%r15
lea    0xe3e(%rip),%r12
mov    (%rsp),%r13
test   %r13,%r13
jne    1228 <peer_connect+0xa8>
jmp    1202 <peer_connect+0x82>
nopl   (%rax)
mov    (%r15),%edi
lea    0xe07(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x18(%r13),%r13
test   %r13,%r13
je     1202 <peer_connect+0x82>
mov    0x10(%r13),%edi
mov    0x8(%r13),%edx
mov    0xc(%r13),%esi
call   10a0 <socket@plt>
test   %eax,%eax
js     1210 <peer_connect+0x90>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <printf@plt>
mov    0x0(%r13),%edx
mov    0x4(%r13),%esi
mov    %ebp,%edi
call   1070 <connect@plt>
test   %rax,%rax
jns    1285 <peer_connect+0x105>
mov    (%r15),%edi
lea    0xdef(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x18(%r13),%r13
test   %r13,%r13
jne    1228 <peer_connect+0xa8>
jmp    1202 <peer_connect+0x82>
lea    0xdde(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <printf@plt>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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 = [33 x i8] c"Unable to assemble address info\0A\00"
@.str.1 = [31 x i8] c"Error unable to create socket\0A\00"
@.str.2 = [26 x i8] c"Connecting to peer %s:%s\0A\00"
@.str.3 = [16 x i8] c"Error: Connect\0A\00"
@.str.4 = [25 x i8] c"Connected to peer %s:%s\0A\00"
define i32 @peer_connect(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i64 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %4, %struct.addrinfo** %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %55
18: ; preds = %3, %21
%19 = load %struct.addrinfo*, %struct.addrinfo** %5
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %21, label %22
21: ; preds = %47, %18
br label %18
22: ; preds = %18, %47
%23 = phi %struct.addrinfo* [ %49, %47 ], [ %19, %18 ]
%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 %32, label %35
32: ; preds = %22
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %47
35: ; preds = %22
%36 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2)
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @connect(i32 %30, i32 %38, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @close(i32 %30)
br label %47
47: ; preds = %43, %32
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %21, label %22
51: ; preds = %35
%52 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %2)
%53 = load %struct.addrinfo*, %struct.addrinfo** %5
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
br label %55
55: ; preds = %51, %15
%56 = phi i32 [ 0, %15 ], [ 1, %51 ]
ret i32 %56
}