#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 {
  int dummy;
};
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;
};
typedef int /*<<< orphan*/ s;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, scalar_t__, int /*<<< orphan*/);
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 **);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int setup_talk_con(char *node, char *port, int *sock_fds) {
  struct addrinfo hints, *servinfo, *p;
  int rv;
  char s[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(node, port, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((*sock_fds = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) ==
        -1) {
      perror("client: socket");
      continue;
    }
    if (connect(*sock_fds, p->ai_addr, p->ai_addrlen) == -1) {
      perror("client: connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "client: failed to connect\n");
    return -1;
  }
  inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s,
            sizeof(s));
  freeaddrinfo(servinfo);
  return 0;
}


x86 assembly (clang, -O0):
<setup_talk_con>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x80(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x68(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     1271 <setup_talk_con+0xd1>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    -0x54(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x84(%rbp),%edi
mov    %rax,%rdx
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1383 <setup_talk_con+0x1e3>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1307 <setup_talk_con+0x167>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   10c0 <socket@plt>
mov    -0x20(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    12be <setup_talk_con+0x11e>
lea    0xd5d(%rip),%rdi
call   1080 <perror@plt>
jmp    12f6 <setup_talk_con+0x156>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f1 <setup_talk_con+0x151>
lea    0xd39(%rip),%rdi
call   1080 <perror@plt>
jmp    12f6 <setup_talk_con+0x156>
jmp    1307 <setup_talk_con+0x167>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1279 <setup_talk_con+0xd9>
cmpq   $0x0,-0x50(%rbp)
jne    133c <setup_talk_con+0x19c>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1383 <setup_talk_con+0x1e3>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x88(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%rdi
call   1040 <get_in_addr@plt>
mov    -0x88(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %eax,%esi
mov    -0x80(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x48(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_talk_con>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,-0x38(%rbp)
mov    %rsi,%r12
mov    %rdi,%r13
mov    %rsp,-0x40(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r15
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x60(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x30(%rbp),%rcx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     1247 <setup_talk_con+0xa7>
mov    0x2daf(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdcf(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12e4 <setup_talk_con+0x144>
mov    -0x38(%rbp),%r12
mov    %rbx,%r13
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     129f <setup_talk_con+0xff>
lea    0xdb3(%rip),%r14
mov    0x14(%rbx),%esi
mov    0x8(%rbx),%edi
mov    0x10(%rbx),%edx
call   10c0 <socket@plt>
mov    %eax,(%r12)
mov    %r14,%rdi
cmp    $0xffffffff,%eax
je     1291 <setup_talk_con+0xf1>
mov    (%rbx),%rsi
mov    0xc(%rbx),%edx
mov    %eax,%edi
call   1090 <connect@plt>
lea    0xd94(%rip),%rdi
cmp    $0xffffffff,%eax
jne    12bd <setup_talk_con+0x11d>
call   1080 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    125e <setup_talk_con+0xbe>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12e4 <setup_talk_con+0x144>
mov    0x8(%rbx),%r14d
mov    (%rbx),%rdi
call   1040 <get_in_addr@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    %r13d,%ecx
call   1030 <inet_ntop@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
xor    %eax,%eax
mov    -0x40(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [15 x i8] c"client: socket\00"
@.str.2 = [16 x i8] c"client: connect\00"
@.str.3 = [27 x i8] c"client: failed to connect\0A\00"
define i32 @setup_talk_con(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = load i32, i32* @INET6_ADDRSTRLEN
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %15, i32* %16
%17 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load %struct.addrinfo*, %struct.addrinfo** %5
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %49, label %26
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = call i8* @gai_strerror(i32 %17)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %62
26: ; preds = %19, %43
%27 = phi %struct.addrinfo* [ %47, %43 ], [ %20, %19 ]
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 4
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 3
%33 = load i32, i32* %32
%34 = call i32 @socket(i32 %29, i32 %31, i32 %33)
store i32 %34, i32* %2
%35 = icmp eq i32 %34, -1
br i1 %35, label %43, label %36
36: ; preds = %26
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 @connect(i32 %34, i64 %38, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %52
43: ; preds = %36, %26
%44 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %26 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %36 ]
%45 = call i32 @perror(i8* %44)
%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 = %43, %19
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %62
52: ; preds = %36
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%55 = load i32, i32* %53
%56 = load i64, i64* %54
%57 = inttoptr i64 %56 to %struct.sockaddr*
%58 = call i32 @get_in_addr(%struct.sockaddr* %57)
%59 = call i32 @inet_ntop(i32 %55, i32 %58, i8* %11, i32 %8)
%60 = load %struct.addrinfo*, %struct.addrinfo** %5
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %60)
br label %62
62: ; preds = %52, %49, %22
%63 = phi i32 [ -1, %22 ], [ -1, %49 ], [ 0, %52 ]
ret i32 %63
}