#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 *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int _open_tcp(int argc, char **argv) {
  struct addrinfo hints, *result, *rp;
  char *host = &argv[0][sizeof("tcp:") - 1];
  char *port = "20000";
  int res;
  if (argc > 1) {
    port = argv[1];
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  res = getaddrinfo(host, port, &hints, &result);
  if (res) {
    fprintf(stderr, "%s. ", gai_strerror(res));
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    res = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (res < 0) {
      continue;
    }
    if (connect(res, rp->ai_addr, rp->ai_addrlen) == 0) {
      break;
    }
    close(res);
  }
  freeaddrinfo(result);
  if (rp == NULL) {
    return -1;
  }
  return res;
}


x86 assembly (clang, -O0):
<_open_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
lea    0xe5b(%rip),%rax
mov    %rax,-0x50(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11bf <_open_tcp+0x3f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     123b <_open_tcp+0xbb>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xddc(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dd <_open_tcp+0x15d>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12b7 <_open_tcp+0x137>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    127a <_open_tcp+0xfa>
jmp    12a6 <_open_tcp+0x126>
mov    -0x54(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    129e <_open_tcp+0x11e>
jmp    12b7 <_open_tcp+0x137>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1243 <_open_tcp+0xc3>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x40(%rbp)
jne    12d7 <_open_tcp+0x157>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dd <_open_tcp+0x15d>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_open_tcp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    (%rsi),%rbx
add    $0x4,%rbx
cmp    $0x2,%edi
jl     119c <_open_tcp+0x1c>
mov    0x8(%rsi),%r14
jmp    11a3 <_open_tcp+0x23>
lea    0xe5d(%rip),%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e05(%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   %eax,%eax
je     120e <_open_tcp+0x8e>
mov    0x2deb(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe09(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1265 <_open_tcp+0xe5>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1229 <_open_tcp+0xa9>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1260 <_open_tcp+0xe0>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1257 <_open_tcp+0xd7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
test   %eax,%eax
js     1220 <_open_tcp+0xa0>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
test   %rax,%rax
je     125c <_open_tcp+0xdc>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1220 <_open_tcp+0xa0>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%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 = [6 x i8] c"20000\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [5 x i8] c"%s. \00"
define i32 @_open_tcp(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8, i8* %14, i64 4
store i8* %15, i8** %9
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %10
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %10
br label %22
22: ; preds = %18, %2
%23 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%24 = load i32, i32* @AF_UNSPEC
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %24, i32* %25
%26 = load i32, i32* @SOCK_STREAM
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %26, i32* %27
%28 = load i8*, i8** %9
%29 = load i8*, i8** %10
%30 = call i32 @getaddrinfo(i8* %28, i8* %29, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %22
%34 = load i32, i32* @stderr
%35 = load i32, i32* %11
%36 = call i8* @gai_strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %36)
store i32 -1, i32* %3
br label %83
38: ; preds = %22
%39 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %39, %struct.addrinfo** %8
br label %40
40: ; preds = %71, %38
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = icmp ne %struct.addrinfo* %41, null
br i1 %42, label %43, label %75
43: ; preds = %40
%44 = load %struct.addrinfo*, %struct.addrinfo** %8
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 4
%46 = load i32, i32* %45
%47 = load %struct.addrinfo*, %struct.addrinfo** %8
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 3
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %8
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 2
%52 = load i32, i32* %51
%53 = call i32 @socket(i32 %46, i32 %49, i32 %52)
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %43
br label %71
57: ; preds = %43
%58 = load i32, i32* %11
%59 = load %struct.addrinfo*, %struct.addrinfo** %8
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 1
%61 = load i32, i32* %60
%62 = load %struct.addrinfo*, %struct.addrinfo** %8
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call i64 @connect(i32 %58, i32 %61, i32 %64)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %57
br label %75
68: ; preds = %57
%69 = load i32, i32* %11
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %56
%72 = load %struct.addrinfo*, %struct.addrinfo** %8
%73 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %72, i32 0, i32 5
%74 = load %struct.addrinfo*, %struct.addrinfo** %73
store %struct.addrinfo* %74, %struct.addrinfo** %8
br label %40
75: ; preds = %67, %40
%76 = load %struct.addrinfo*, %struct.addrinfo** %7
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
%78 = load %struct.addrinfo*, %struct.addrinfo** %8
%79 = icmp eq %struct.addrinfo* %78, null
br i1 %79, label %80, label %81
80: ; preds = %75
store i32 -1, i32* %3
br label %83
81: ; preds = %75
%82 = load i32, i32* %11
store i32 %82, i32* %3
br label %83
83: ; preds = %81, %80, %33
%84 = load i32, i32* %3
ret i32 %84
}

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