#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*/ STRERROR(int /*<<< orphan*/, char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ memcpy(char *, char *, int);
struct addrinfo *prepare_addrinfo_tcp(char const *, char const *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_client(char const *server_addr, char const *port, char *errorstr) {
  struct addrinfo *servinfo, *p;
  int sockfd;
  if ((servinfo = prepare_addrinfo_tcp(server_addr, port, errorstr)) == NULL)
    return -1;
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
      do {
        if (errorstr != NULL) {
          memcpy(errorstr,
                 "socket"
                 ": ",
                 (sizeof("socket"
                         ": ") /
                      sizeof(char) -
                  1));
          STRERROR(errno, errorstr + (sizeof("socket"
                                             ": ") /
                                          sizeof(char) -
                                      1));
        }
      } while (0);
      continue;
    }
    if (connect(sockfd, p->ai_addr, p->ai_addrlen) < 0) {
      close(sockfd);
      do {
        if (errorstr != NULL) {
          memcpy(errorstr,
                 "connect"
                 ": ",
                 (sizeof("connect"
                         ": ") /
                      sizeof(char) -
                  1));
          STRERROR(errno, errorstr + (sizeof("connect"
                                             ": ") /
                                          sizeof(char) -
                                      1));
        }
      } while (0);
      continue;
    }
    break;
  }
  if (p == NULL)
    sockfd = -1;
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<tcp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <prepare_addrinfo_tcp@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11af <tcp_client+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <tcp_client+0x15b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12aa <tcp_client+0x13a>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    122d <tcp_client+0xbd>
jmp    11ed <tcp_client+0x7d>
cmpq   $0x0,-0x20(%rbp)
je     1223 <tcp_client+0xb3>
mov    -0x20(%rbp),%rdi
lea    0xdfd(%rip),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
add    $0x8,%rsi
call   1060 <STRERROR@plt>
jmp    1228 <tcp_client+0xb8>
jmp    1299 <tcp_client+0x129>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1294 <tcp_client+0x124>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x20(%rbp)
je     128a <tcp_client+0x11a>
mov    -0x20(%rbp),%rdi
lea    0xd9f(%rip),%rsi
mov    $0x9,%edx
call   1050 <memcpy@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
add    $0x9,%rsi
call   1060 <STRERROR@plt>
jmp    128f <tcp_client+0x11f>
jmp    1299 <tcp_client+0x129>
jmp    12aa <tcp_client+0x13a>
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11b7 <tcp_client+0x47>
cmpq   $0x0,-0x30(%rbp)
jne    12bc <tcp_client+0x14c>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
call   1030 <prepare_addrinfo_tcp@plt>
test   %rax,%rax
je     1232 <tcp_client+0xc2>
lea    0x9(%rbx),%r13
lea    0x8(%rbx),%r15
mov    0x2e45(%rip),%r14
mov    %rax,(%rsp)
mov    %rax,%r12
jmp    11de <tcp_client+0x6e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %rbx,%rbx
je     11d4 <tcp_client+0x64>
mov    %rbx,%rdi
lea    0xe41(%rip),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    %r15,%rsi
mov    (%r14),%edi
call   1060 <STRERROR@plt>
mov    0x18(%r12),%r12
test   %r12,%r12
je     1239 <tcp_client+0xc9>
mov    0x10(%r12),%edi
mov    0x8(%r12),%edx
mov    0xc(%r12),%esi
call   1090 <socket@plt>
test   %eax,%eax
js     11b0 <tcp_client+0x40>
mov    %eax,%ebp
mov    (%r12),%edx
mov    0x4(%r12),%esi
mov    %eax,%edi
call   1070 <connect@plt>
test   %rax,%rax
jns    123e <tcp_client+0xce>
mov    %ebp,%edi
call   1040 <close@plt>
test   %rbx,%rbx
je     11d4 <tcp_client+0x64>
mov    %rbx,%rdi
lea    0xde6(%rip),%rsi
mov    $0x9,%edx
call   1050 <memcpy@plt>
mov    %r13,%rsi
jmp    11cc <tcp_client+0x5c>
mov    $0xffffffff,%ebp
jmp    1247 <tcp_client+0xd7>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x8,%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* }
@.str = [9 x i8] c"socket: \00"
@errno = global i32 0
@.str.1 = [10 x i8] c"connect: \00"
define i32 @tcp_client(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = load i8*, i8** %6
%13 = load i8*, i8** %7
%14 = call %struct.addrinfo* @prepare_addrinfo_tcp(i8* %11, i8* %12, i8* %13)
store %struct.addrinfo* %14, %struct.addrinfo** %8
%15 = icmp eq %struct.addrinfo* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %85
17: ; preds = %3
%18 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %18, %struct.addrinfo** %9
br label %19
19: ; preds = %73, %17
%20 = load %struct.addrinfo*, %struct.addrinfo** %9
%21 = icmp ne %struct.addrinfo* %20, null
br i1 %21, label %22, label %77
22: ; preds = %19
%23 = load %struct.addrinfo*, %struct.addrinfo** %9
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 4
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %9
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 3
%28 = load i32, i32* %27
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %25, i32 %28, i32 %31)
store i32 %32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %22
br label %35
35: ; preds = %34
%36 = load i8*, i8** %7
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = load i8*, i8** %7
%40 = call i32 @memcpy(i8* %39, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%41 = load i32, i32* @errno
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i64 8
%44 = call i32 @STRERROR(i32 %41, i8* %43)
br label %45
45: ; preds = %38, %35
br label %46
46: ; preds = %45
br label %73
47: ; preds = %22
%48 = load i32, i32* %10
%49 = load %struct.addrinfo*, %struct.addrinfo** %9
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %9
%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 %72
57: ; preds = %47
%58 = load i32, i32* %10
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57
%61 = load i8*, i8** %7
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %70
63: ; preds = %60
%64 = load i8*, i8** %7
%65 = call i32 @memcpy(i8* %64, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 9)
%66 = load i32, i32* @errno
%67 = load i8*, i8** %7
%68 = getelementptr inbounds i8, i8* %67, i64 9
%69 = call i32 @STRERROR(i32 %66, i8* %68)
br label %70
70: ; preds = %63, %60
br label %71
71: ; preds = %70
br label %73
72: ; preds = %47
br label %77
73: ; preds = %71, %46
%74 = load %struct.addrinfo*, %struct.addrinfo** %9
%75 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %74, i32 0, i32 5
%76 = load %struct.addrinfo*, %struct.addrinfo** %75
store %struct.addrinfo* %76, %struct.addrinfo** %9
br label %19
77: ; preds = %72, %19
%78 = load %struct.addrinfo*, %struct.addrinfo** %9
%79 = icmp eq %struct.addrinfo* %78, null
br i1 %79, label %80, label %81
80: ; preds = %77
store i32 -1, i32* %10
br label %81
81: ; preds = %80, %77
%82 = load %struct.addrinfo*, %struct.addrinfo** %8
%83 = call i32 @freeaddrinfo(%struct.addrinfo* %82)
%84 = load i32, i32* %10
store i32 %84, i32* %4
br label %85
85: ; preds = %81, %16
%86 = load i32, i32* %4
ret i32 %86
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [9 x i8] c"socket: \00"
@errno = global i32 0
@.str.1 = [10 x i8] c"connect: \00"
define i32 @tcp_client(i8* %0, i8* %1, i8* %2) {
%4 = tail call %struct.addrinfo* @prepare_addrinfo_tcp(i8* %0, i8* %1, i8* %2)
%5 = icmp eq %struct.addrinfo* %4, null
br i1 %5, label %45, label %6
6: ; preds = %3
%7 = icmp eq i8* %2, null
%8 = getelementptr inbounds i8, i8* %2, i64 9
%9 = getelementptr inbounds i8, i8* %2, i64 8
br label %10
10: ; preds = %6, %38
%11 = phi %struct.addrinfo* [ %4, %6 ], [ %40, %38 ]
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 4
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 3
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 2
%17 = load i32, i32* %16
%18 = tail call i32 @socket(i32 %13, i32 %15, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %10
br i1 %7, label %38, label %21
21: ; preds = %20
%22 = tail call i32 @memcpy(i8* %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
br label %34
23: ; preds = %10
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i64 @connect(i32 %18, i32 %25, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %23
%31 = tail call i32 @close(i32 %18)
br i1 %7, label %38, label %32
32: ; preds = %30
%33 = tail call i32 @memcpy(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 9)
br label %34
34: ; preds = %21, %32
%35 = phi i8* [ %8, %32 ], [ %9, %21 ]
%36 = load i32, i32* @errno
%37 = tail call i32 @STRERROR(i32 %36, i8* %35)
br label %38
38: ; preds = %34, %30, %20
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 5
%40 = load %struct.addrinfo*, %struct.addrinfo** %39
%41 = icmp eq %struct.addrinfo* %40, null
br i1 %41, label %42, label %10
42: ; preds = %38, %23
%43 = phi i32 [ %18, %23 ], [ -1, %38 ]
%44 = tail call i32 @freeaddrinfo(%struct.addrinfo* %4)
br label %45
45: ; preds = %3, %42
%46 = phi i32 [ %43, %42 ], [ -1, %3 ]
ret i32 %46
}