#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 sockaddr_storage {
  int /*<<< orphan*/ ss_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
scalar_t__ gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int establish_connection(char *ip, char *port, int flag) {
  int sockfd, new_fd;
  struct addrinfo hints, *servinfo, *p;
  struct sockaddr_storage their_addr;
  socklen_t sin_size;
  int yes = 1;
  char s[INET6_ADDRSTRLEN];
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (flag) {
    hints.ai_flags = AI_PASSIVE;
  }
  if ((rv = getaddrinfo(ip, port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("socket");
      continue;
    }
    if (flag) {
      if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) ==
          -1) {
        perror("setsockopt");
        exit(1);
      }
      if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
        close(sockfd);
        perror("server: bind");
        continue;
      }
    } else {
      if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
        close(sockfd);
        perror("client: connect");
        continue;
      }
    }
    break;
  }
  if (p == NULL) {
    if (flag) {
      fprintf(stderr, "server: failed to bind\n");
    } else {
      fprintf(stderr, "client: failed to connect\n");
    }
    return -1;
  }
  if (flag) {
    if (listen(sockfd, 1) == -1) {
      perror("listen");
      return -1;
    }
    char *ipstr;
    char hostname[1024];
    gethostname(hostname, 1024);
    struct hostent *info;
    info = (struct hostent *)gethostbyname(hostname);
    ipstr = inet_ntoa(*((struct in_addr *)info->h_addr));
    printf("Waiting for a connection on %s port %s\n", ipstr, "44044");
    sin_size = sizeof their_addr;
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    if (new_fd == -1) {
      perror("accept");
    }
    inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr),
              s, sizeof s);
    printf("Found a friend! You receive first.\n");
    close(sockfd);
    sockfd = new_fd;
  } else {
    inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s,
              sizeof s);
    printf("Connecting to server... Connected!\n");
    printf("Connected to a friend! You send first.\n");
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<establish_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x1,-0x70(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x4a8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x4a0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x80(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1090 <memset@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12d1 <establish_connection+0x91>
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1140 <getaddrinfo@plt>
mov    %eax,-0x84(%rbp)
cmp    $0x0,%eax
je     133c <establish_connection+0xfc>
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4ac(%rbp)
mov    -0x84(%rbp),%edi
call   1080 <gai_strerror@plt>
mov    -0x4ac(%rbp),%edi
mov    %rax,%rdx
lea    0xce1(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x88(%rbp)
jmp    1646 <establish_connection+0x406>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1463 <establish_connection+0x223>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edx
call   1160 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1386 <establish_connection+0x146>
lea    0xc95(%rip),%rdi
call   10f0 <perror@plt>
jmp    1452 <establish_connection+0x212>
cmpl   $0x0,-0x1c(%rbp)
je     1410 <establish_connection+0x1d0>
mov    -0x20(%rbp),%edi
mov    0x2c4e(%rip),%rax
mov    (%rax),%esi
mov    0x2c0d(%rip),%rax
mov    (%rax),%edx
lea    -0x70(%rbp),%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    13d3 <establish_connection+0x193>
lea    0xc54(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x20(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    (%rax),%rsi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    140b <establish_connection+0x1cb>
mov    -0x20(%rbp),%edi
call   10a0 <close@plt>
lea    0xc22(%rip),%rdi
call   10f0 <perror@plt>
jmp    1452 <establish_connection+0x212>
jmp    144d <establish_connection+0x20d>
mov    -0x20(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    (%rax),%rsi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%edx
call   1130 <connect@plt>
cmp    $0xffffffff,%eax
jne    1448 <establish_connection+0x208>
mov    -0x20(%rbp),%edi
call   10a0 <close@plt>
lea    0xbf2(%rip),%rdi
call   10f0 <perror@plt>
jmp    1452 <establish_connection+0x212>
jmp    144d <establish_connection+0x20d>
jmp    1463 <establish_connection+0x223>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1344 <establish_connection+0x104>
cmpq   $0x0,-0x60(%rbp)
jne    14c1 <establish_connection+0x281>
cmpl   $0x0,-0x1c(%rbp)
je     1494 <establish_connection+0x254>
mov    0x2b41(%rip),%rax
mov    (%rax),%edi
lea    0xbb8(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
jmp    14ab <establish_connection+0x26b>
mov    0x2b25(%rip),%rax
mov    (%rax),%edi
lea    0xbb4(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x88(%rbp)
jmp    1646 <establish_connection+0x406>
cmpl   $0x0,-0x1c(%rbp)
je     15db <establish_connection+0x39b>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   10d0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1503 <establish_connection+0x2c3>
lea    0xb8b(%rip),%rdi
call   10f0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x88(%rbp)
jmp    1646 <establish_connection+0x406>
lea    -0x490(%rbp),%rdi
mov    $0x400,%esi
call   1110 <gethostname@plt>
lea    -0x490(%rbp),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x498(%rbp)
mov    -0x498(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rsi
lea    0xb2d(%rip),%rdi
lea    0xb4e(%rip),%rdx
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x4,-0x6c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x68(%rbp),%rsi
lea    -0x6c(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    158b <establish_connection+0x34b>
lea    0xb22(%rip),%rdi
call   10f0 <perror@plt>
mov    -0x68(%rbp),%eax
mov    %eax,-0x4b0(%rbp)
lea    -0x68(%rbp),%rdi
call   1040 <get_in_addr@plt>
mov    -0x4b0(%rbp),%edi
mov    -0x4a0(%rbp),%rdx
mov    %eax,%esi
mov    -0x4a8(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
lea    0xaee(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%edi
call   10a0 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    162d <establish_connection+0x3ed>
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x4b4(%rbp)
mov    -0x60(%rbp),%rax
mov    (%rax),%rdi
call   1040 <get_in_addr@plt>
mov    -0x4b4(%rbp),%edi
mov    -0x4a0(%rbp),%rdx
mov    %eax,%esi
mov    -0x4a8(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
lea    0xabb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xad1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x58(%rbp),%rdi
call   1150 <freeaddrinfo@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x88(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establish_connection>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x458,%rsp
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%rbx
mov    %rsp,%r14
movl   $0x1,-0x3c(%rbp)
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
mov    %rax,-0x48(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   10a0 <memset@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
test   %r12d,%r12d
je     12ce <establish_connection+0x7e>
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
lea    -0x78(%rbp),%rdx
lea    -0x50(%rbp),%rcx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1150 <getaddrinfo@plt>
test   %eax,%eax
je     1312 <establish_connection+0xc2>
mov    0x2cd4(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1090 <gai_strerror@plt>
lea    0xd04(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    14dd <establish_connection+0x28d>
mov    %r14,-0x38(%rbp)
mov    -0x50(%rbp),%r14
test   %r14,%r14
je     13ca <establish_connection+0x17a>
mov    0x14(%r14),%esi
mov    0x8(%r14),%edi
mov    0x10(%r14),%edx
call   1170 <socket@plt>
lea    0xcd6(%rip),%r13
cmp    $0xffffffff,%eax
je     13b5 <establish_connection+0x165>
mov    %eax,%ebx
test   %r12d,%r12d
je     1390 <establish_connection+0x140>
mov    0x2c9a(%rip),%rax
mov    (%rax),%esi
mov    0x2c59(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1526 <establish_connection+0x2d6>
mov    (%r14),%rsi
mov    0xc(%r14),%edx
mov    %ebx,%edi
call   10f0 <bind@plt>
lea    0xc9b(%rip),%r13
cmp    $0xffffffff,%eax
je     13ae <establish_connection+0x15e>
jmp    13ec <establish_connection+0x19c>
nop
mov    (%r14),%rsi
mov    0xc(%r14),%edx
mov    %ebx,%edi
call   1140 <connect@plt>
lea    0xc8b(%rip),%r13
cmp    $0xffffffff,%eax
jne    149b <establish_connection+0x24b>
mov    %ebx,%edi
call   10b0 <close@plt>
mov    %r13,%rdi
call   1100 <perror@plt>
mov    0x18(%r14),%r14
test   %r14,%r14
jne    1323 <establish_connection+0xd3>
mov    0x2bef(%rip),%rax
mov    (%rax),%edi
test   %r12d,%r12d
mov    -0x38(%rbp),%r14
je     14f1 <establish_connection+0x2a1>
lea    0xc59(%rip),%rsi
jmp    14f8 <establish_connection+0x2a8>
mov    %ebx,%edi
mov    $0x1,%esi
call   10e0 <listen@plt>
cmp    $0xffffffff,%eax
mov    -0x38(%rbp),%r14
je     1504 <establish_connection+0x2b4>
lea    -0x480(%rbp),%r12
mov    %r12,%rdi
mov    $0x400,%esi
call   1120 <gethostname@plt>
mov    %r12,%rdi
call   10c0 <gethostbyname@plt>
mov    (%rax),%rax
mov    (%rax),%edi
call   1070 <inet_ntoa@plt>
lea    0xc48(%rip),%rdi
lea    0xc69(%rip),%rdx
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
movl   $0x4,-0x40(%rbp)
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
mov    %ebx,%edi
call   1110 <accept@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     1515 <establish_connection+0x2c5>
mov    -0x30(%rbp),%r13d
lea    -0x30(%rbp),%rdi
call   1040 <get_in_addr@plt>
mov    %r13d,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    -0x48(%rbp),%rcx
call   1030 <inet_ntop@plt>
lea    0xc6f(%rip),%rdi
call   1050 <puts@plt>
mov    %ebx,%edi
call   10b0 <close@plt>
mov    %r12d,%ebx
jmp    14d4 <establish_connection+0x284>
mov    0x8(%r14),%r12d
mov    (%r14),%rdi
call   1040 <get_in_addr@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    -0x48(%rbp),%rcx
call   1030 <inet_ntop@plt>
lea    0xbf0(%rip),%rdi
call   1050 <puts@plt>
lea    0xc07(%rip),%rdi
call   1050 <puts@plt>
mov    -0x38(%rbp),%r14
mov    -0x50(%rbp),%rdi
call   1160 <freeaddrinfo@plt>
mov    %r14,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb60(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
jmp    1308 <establish_connection+0xb8>
lea    0xb68(%rip),%rdi
call   1100 <perror@plt>
jmp    1308 <establish_connection+0xb8>
lea    0xb8c(%rip),%rdi
call   1100 <perror@plt>
jmp    1465 <establish_connection+0x215>
lea    0xaeb(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
@.str.3 = [13 x i8] c"server: bind\00"
@.str.4 = [16 x i8] c"client: connect\00"
@.str.5 = [24 x i8] c"server: failed to bind\0A\00"
@.str.6 = [27 x i8] c"client: failed to connect\0A\00"
@.str.7 = [7 x i8] c"listen\00"
@.str.8 = [40 x i8] c"Waiting for a connection on %s port %s\0A\00"
@.str.9 = [6 x i8] c"44044\00"
@.str.10 = [7 x i8] c"accept\00"
@.str.11 = [36 x i8] c"Found a friend! You receive first.\0A\00"
@.str.12 = [36 x i8] c"Connecting to server... Connected!\0A\00"
@.str.13 = [40 x i8] c"Connected to a friend! You send first.\0A\00"
define i32 @establish_connection(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.addrinfo
%11 = alloca %struct.addrinfo*
%12 = alloca %struct.addrinfo*
%13 = alloca %struct.sockaddr_storage
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i32
%19 = alloca i32
%20 = alloca i8*
%21 = alloca [1024 x i8]
%22 = alloca %struct.hostent*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 1, i32* %15
%23 = load i32, i32* @INET6_ADDRSTRLEN
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %16
%26 = alloca i8, i64 %24
store i64 %24, i64* %17
%27 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 40)
%28 = load i32, i32* @AF_UNSPEC
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @SOCK_STREAM
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 4
store i32 %30, i32* %31
%32 = load i32, i32* %7
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %3
%35 = load i32, i32* @AI_PASSIVE
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 6
store i32 %35, i32* %36
br label %37
37: ; preds = %34, %3
%38 = load i8*, i8** %5
%39 = load i8*, i8** %6
%40 = call i32 @getaddrinfo(i8* %38, i8* %39, %struct.addrinfo* %10, %struct.addrinfo** %11)
store i32 %40, i32* %18
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = load i32, i32* %18
%45 = call i8* @gai_strerror(i32 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %45)
store i32 -1, i32* %4
store i32 1, i32* %19
br label %185
47: ; preds = %37
%48 = load %struct.addrinfo*, %struct.addrinfo** %11
store %struct.addrinfo* %48, %struct.addrinfo** %12
br label %49
49: ; preds = %109, %47
%50 = load %struct.addrinfo*, %struct.addrinfo** %12
%51 = icmp ne %struct.addrinfo* %50, null
br i1 %51, label %52, label %113
52: ; preds = %49
%53 = load %struct.addrinfo*, %struct.addrinfo** %12
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %12
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 4
%58 = load i32, i32* %57
%59 = load %struct.addrinfo*, %struct.addrinfo** %12
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 3
%61 = load i32, i32* %60
%62 = call i32 @socket(i32 %55, i32 %58, i32 %61)
store i32 %62, i32* %8
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %52
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %109
66: ; preds = %52
%67 = load i32, i32* %7
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %93
69: ; preds = %66
%70 = load i32, i32* %8
%71 = load i32, i32* @SOL_SOCKET
%72 = load i32, i32* @SO_REUSEADDR
%73 = call i32 @setsockopt(i32 %70, i32 %71, i32 %72, i32* %15, i32 4)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %78
75: ; preds = %69
%76 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %69
%79 = load i32, i32* %8
%80 = load %struct.addrinfo*, %struct.addrinfo** %12
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 0
%82 = load i64, i64* %81
%83 = load %struct.addrinfo*, %struct.addrinfo** %12
%84 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %83, i32 0, i32 2
%85 = load i32, i32* %84
%86 = call i32 @bind(i32 %79, i64 %82, i32 %85)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %92
88: ; preds = %78
%89 = load i32, i32* %8
%90 = call i32 @close(i32 %89)
%91 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %109
92: ; preds = %78
br label %108
93: ; preds = %66
%94 = load i32, i32* %8
%95 = load %struct.addrinfo*, %struct.addrinfo** %12
%96 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %95, i32 0, i32 0
%97 = load i64, i64* %96
%98 = load %struct.addrinfo*, %struct.addrinfo** %12
%99 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %98, i32 0, i32 2
%100 = load i32, i32* %99
%101 = call i32 @connect(i32 %94, i64 %97, i32 %100)
%102 = icmp eq i32 %101, -1
br i1 %102, label %103, label %107
103: ; preds = %93
%104 = load i32, i32* %8
%105 = call i32 @close(i32 %104)
%106 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %109
107: ; preds = %93
br label %108
108: ; preds = %107, %92
br label %113
109: ; preds = %103, %88, %64
%110 = load %struct.addrinfo*, %struct.addrinfo** %12
%111 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %110, i32 0, i32 5
%112 = load %struct.addrinfo*, %struct.addrinfo** %111
store %struct.addrinfo* %112, %struct.addrinfo** %12
br label %49
113: ; preds = %108, %49
%114 = load %struct.addrinfo*, %struct.addrinfo** %12
%115 = icmp eq %struct.addrinfo* %114, null
br i1 %115, label %116, label %126
116: ; preds = %113
%117 = load i32, i32* %7
%118 = icmp ne i32 %117, 0
br i1 %118, label %119, label %122
119: ; preds = %116
%120 = load i32, i32* @stderr
%121 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %125
122: ; preds = %116
%123 = load i32, i32* @stderr
%124 = call i32 (i32, i8*, ...) @fprintf(i32 %123, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %125
125: ; preds = %122, %119
store i32 -1, i32* %4
store i32 1, i32* %19
br label %185
126: ; preds = %113
%127 = load i32, i32* %7
%128 = icmp ne i32 %127, 0
br i1 %128, label %129, label %168
129: ; preds = %126
%130 = load i32, i32* %8
%131 = call i32 @listen(i32 %130, i32 1)
%132 = icmp eq i32 %131, -1
br i1 %132, label %133, label %135
133: ; preds = %129
%134 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %19
br label %185
135: ; preds = %129
%136 = getelementptr inbounds [1024 x i8], [1024 x i8]* %21, i64 0, i64 0
%137 = call i32 @gethostname(i8* %136, i32 1024)
%138 = getelementptr inbounds [1024 x i8], [1024 x i8]* %21, i64 0, i64 0
%139 = call i64 @gethostbyname(i8* %138)
%140 = inttoptr i64 %139 to %struct.hostent*
store %struct.hostent* %140, %struct.hostent** %22
%141 = load %struct.hostent*, %struct.hostent** %22
%142 = getelementptr inbounds %struct.hostent, %struct.hostent* %141, i32 0, i32 0
%143 = load i64, i64* %142
%144 = inttoptr i64 %143 to %struct.in_addr*
%145 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %144, i32 0, i32 0
%146 = load i32, i32* %145
%147 = call i8* @inet_ntoa(i32 %146)
store i8* %147, i8** %20
%148 = load i8*, i8** %20
%149 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i8* %148, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
store i32 4, i32* %14
%150 = load i32, i32* %8
%151 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr*
%152 = call i32 @accept(i32 %150, %struct.sockaddr* %151, i32* %14)
store i32 %152, i32* %9
%153 = load i32, i32* %9
%154 = icmp eq i32 %153, -1
br i1 %154, label %155, label %157
155: ; preds = %135
%156 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %157
157: ; preds = %155, %135
%158 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %13, i32 0, i32 0
%159 = load i32, i32* %158
%160 = bitcast %struct.sockaddr_storage* %13 to %struct.sockaddr*
%161 = call i32 @get_in_addr(%struct.sockaddr* %160)
%162 = trunc i64 %24 to i32
%163 = call i32 @inet_ntop(i32 %159, i32 %161, i8* %26, i32 %162)
%164 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i64 0, i64 0))
%165 = load i32, i32* %8
%166 = call i32 @close(i32 %165)
%167 = load i32, i32* %9
store i32 %167, i32* %8
br label %181
168: ; preds = %126
%169 = load %struct.addrinfo*, %struct.addrinfo** %12
%170 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %169, i32 0, i32 1
%171 = load i32, i32* %170
%172 = load %struct.addrinfo*, %struct.addrinfo** %12
%173 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %172, i32 0, i32 0
%174 = load i64, i64* %173
%175 = inttoptr i64 %174 to %struct.sockaddr*
%176 = call i32 @get_in_addr(%struct.sockaddr* %175)
%177 = trunc i64 %24 to i32
%178 = call i32 @inet_ntop(i32 %171, i32 %176, i8* %26, i32 %177)
%179 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.12, i64 0, i64 0))
%180 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.13, i64 0, i64 0))
br label %181
181: ; preds = %168, %157
%182 = load %struct.addrinfo*, %struct.addrinfo** %11
%183 = call i32 @freeaddrinfo(%struct.addrinfo* %182)
%184 = load i32, i32* %8
store i32 %184, i32* %4
store i32 1, i32* %19
br label %185
185: ; preds = %181, %133, %125, %42
%186 = load i8*, i8** %16
%187 = load i32, i32* %4
ret i32 %187
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
@.str.3 = [13 x i8] c"server: bind\00"
@.str.4 = [16 x i8] c"client: connect\00"
@.str.5 = [24 x i8] c"server: failed to bind\0A\00"
@.str.6 = [27 x i8] c"client: failed to connect\0A\00"
@.str.7 = [7 x i8] c"listen\00"
@.str.8 = [40 x i8] c"Waiting for a connection on %s port %s\0A\00"
@.str.9 = [6 x i8] c"44044\00"
@.str.10 = [7 x i8] c"accept\00"
@str = [35 x i8] c"Connecting to server... Connected!\00"
@str.14 = [39 x i8] c"Connected to a friend! You send first.\00"
@str.15 = [35 x i8] c"Found a friend! You receive first.\00"
define i32 @establish_connection(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = bitcast %struct.addrinfo* %4 to i8*
%11 = bitcast %struct.addrinfo** %5 to i8*
%12 = bitcast %struct.sockaddr_storage* %6 to i8*
%13 = bitcast i32* %7 to i8*
%14 = bitcast i32* %8 to i8*
store i32 1, i32* %8
%15 = load i32, i32* @INET6_ADDRSTRLEN
%16 = zext i32 %15 to i64
%17 = tail call i8* @llvm.stacksave()
%18 = alloca i8, i64 %16
%19 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%20 = load i32, i32* @AF_UNSPEC
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @SOCK_STREAM
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %22, i32* %23
%24 = icmp eq i32 %2, 0
br i1 %24, label %28, label %25
25: ; preds = %3
%26 = load i32, i32* @AI_PASSIVE
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %26, i32* %27
br label %28
28: ; preds = %25, %3
%29 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %5
%33 = icmp eq %struct.addrinfo* %32, null
br i1 %33, label %80, label %38
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = call i8* @gai_strerror(i32 %29)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %36)
br label %129
38: ; preds = %31, %74
%39 = phi %struct.addrinfo* [ %78, %74 ], [ %32, %31 ]
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 4
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 3
%45 = load i32, i32* %44
%46 = call i32 @socket(i32 %41, i32 %43, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %74, label %48
48: ; preds = %38
br i1 %24, label %64, label %49
49: ; preds = %48
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_REUSEADDR
%52 = call i32 @setsockopt(i32 %46, i32 %50, i32 %51, i32* %8, i32 4)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %49
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 0
%59 = load i64, i64* %58
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 2
%61 = load i32, i32* %60
%62 = call i32 @bind(i32 %46, i64 %59, i32 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %71, label %86
64: ; preds = %48
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 0
%66 = load i64, i64* %65
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 2
%68 = load i32, i32* %67
%69 = call i32 @connect(i32 %46, i64 %66, i32 %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %115
71: ; preds = %64, %57
%72 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %57 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), %64 ]
%73 = call i32 @close(i32 %46)
br label %74
74: ; preds = %71, %38
%75 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %38 ], [ %72, %71 ]
%76 = call i32 @perror(i8* %75)
%77 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 5
%78 = load %struct.addrinfo*, %struct.addrinfo** %77
%79 = icmp eq %struct.addrinfo* %78, null
br i1 %79, label %80, label %38
80: ; preds = %74, %31
%81 = load i32, i32* @stderr
br i1 %24, label %84, label %82
82: ; preds = %80
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %129
84: ; preds = %80
%85 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %129
86: ; preds = %57
%87 = call i32 @listen(i32 %46, i32 1)
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %91
89: ; preds = %86
%90 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %129
91: ; preds = %86
%92 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%93 = call i32 @gethostname(i8* %92, i32 1024)
%94 = call i64 @gethostbyname(i8* %92)
%95 = inttoptr i64 %94 to %struct.hostent*
%96 = getelementptr inbounds %struct.hostent, %struct.hostent* %95, i64 0, i32 0
%97 = load i64, i64* %96
%98 = inttoptr i64 %97 to %struct.in_addr*
%99 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %98, i64 0, i32 0
%100 = load i32, i32* %99
%101 = call i8* @inet_ntoa(i32 %100)
%102 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i8* %101, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
store i32 4, i32* %7
%103 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%104 = call i32 @accept(i32 %46, %struct.sockaddr* %103, i32* %7)
%105 = icmp eq i32 %104, -1
br i1 %105, label %106, label %108
106: ; preds = %91
%107 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %108
108: ; preds = %106, %91
%109 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %6, i64 0, i32 0
%110 = load i32, i32* %109
%111 = call i32 @get_in_addr(%struct.sockaddr* %103)
%112 = call i32 @inet_ntop(i32 %110, i32 %111, i8* %18, i32 %15)
%113 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.15, i64 0, i64 0))
%114 = call i32 @close(i32 %46)
br label %125
115: ; preds = %64
%116 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 1
%117 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 0
%118 = load i32, i32* %116
%119 = load i64, i64* %117
%120 = inttoptr i64 %119 to %struct.sockaddr*
%121 = call i32 @get_in_addr(%struct.sockaddr* %120)
%122 = call i32 @inet_ntop(i32 %118, i32 %121, i8* %18, i32 %15)
%123 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%124 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.14, i64 0, i64 0))
br label %125
125: ; preds = %115, %108
%126 = phi i32 [ %104, %108 ], [ %46, %115 ]
%127 = load %struct.addrinfo*, %struct.addrinfo** %5
%128 = call i32 @freeaddrinfo(%struct.addrinfo* %127)
br label %129
129: ; preds = %82, %84, %125, %89, %34
%130 = phi i32 [ -1, %34 ], [ -1, %89 ], [ %126, %125 ], [ -1, %84 ], [ -1, %82 ]
ret i32 %130
}