#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 protoent {
  int /*<<< orphan*/ p_proto;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_UNSPEC;
#define EAI_AGAIN 132
#define EAI_FAIL 131
#define EAI_MEMORY 130
#define EAI_NONAME 129
#define EAI_SERVICE 128
int EINVAL;
int ENETDOWN;
int ENETUNREACH;
int ENOMEM;
int ENOPROTOOPT;
int ENOTCONN;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcpdial(int *conn, char host[], char port[]) {
  struct protoent *tcpproto = getprotobyname("tcp");
  if (tcpproto == NULL) {
    errno = ENOPROTOOPT;
    return errno;
  }
  *conn = socket(AF_INET, SOCK_STREAM, tcpproto->p_proto);
  if (*conn == -1) {
    return errno;
  }
  struct addrinfo tcphints, *tcpsockaddr;
  memset(&tcphints, 0, sizeof tcphints);
  tcphints.ai_family = AF_UNSPEC;
  tcphints.ai_socktype = SOCK_STREAM;
  tcphints.ai_protocol = tcpproto->p_proto;
  errno = getaddrinfo(host, port, &tcphints, &tcpsockaddr);
  if (errno != 0) {
    switch (errno) {
    case EAI_AGAIN:
      errno = ENETUNREACH;
      break;
    case EAI_FAIL:
      errno = ENETDOWN;
      break;
    case EAI_MEMORY:
      errno = ENOMEM;
      break;
    case EAI_NONAME:
    case EAI_SERVICE:
      errno = EINVAL;
      break;
    default:
      break;
    }
    return errno;
  }
  struct addrinfo *addri;
  for (addri = tcpsockaddr; addri != NULL; addri = addri->ai_next) {
    int retcon = connect(*conn, addri->ai_addr, addri->ai_addrlen);
    if (retcon != 0) {
      continue;
    }
    break;
  }
  if (addri == NULL) {
    errno = ENOTCONN;
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tcpdial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xea9(%rip),%rdi
call   1040 <getprotobyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a2 <tcpdial+0x52>
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e11(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <tcpdial+0x21f>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    11e5 <tcpdial+0x95>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <tcpdial+0x21f>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,%ecx
mov    0x2d6c(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d63(%rip),%rax
cmpl   $0x0,(%rax)
je     12e7 <tcpdial+0x197>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x68(%rbp)
sub    $0x4,%eax
ja     12d1 <tcpdial+0x181>
mov    -0x68(%rbp),%rax
lea    0xd94(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2d54(%rip),%rax
mov    (%rax),%ecx
mov    0x2d1b(%rip),%rax
mov    %ecx,(%rax)
jmp    12d6 <tcpdial+0x186>
mov    0x2d35(%rip),%rax
mov    (%rax),%ecx
mov    0x2d04(%rip),%rax
mov    %ecx,(%rax)
jmp    12d6 <tcpdial+0x186>
mov    0x2d06(%rip),%rax
mov    (%rax),%ecx
mov    0x2ced(%rip),%rax
mov    %ecx,(%rax)
jmp    12d6 <tcpdial+0x186>
mov    0x2cd7(%rip),%rax
mov    (%rax),%ecx
mov    0x2cd6(%rip),%rax
mov    %ecx,(%rax)
jmp    12d6 <tcpdial+0x186>
jmp    12d6 <tcpdial+0x186>
mov    0x2cc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <tcpdial+0x21f>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     133a <tcpdial+0x1ea>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     1324 <tcpdial+0x1d4>
jmp    1329 <tcpdial+0x1d9>
jmp    133a <tcpdial+0x1ea>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    12ef <tcpdial+0x19f>
cmpq   $0x0,-0x58(%rbp)
jne    1368 <tcpdial+0x218>
mov    0x2c74(%rip),%rax
mov    (%rax),%ecx
mov    0x2c4b(%rip),%rax
mov    %ecx,(%rax)
mov    0x2c42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    136f <tcpdial+0x21f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpdial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xea6(%rip),%rdi
call   1040 <getprotobyname@plt>
test   %rax,%rax
je     1216 <tcpdial+0xc6>
mov    %rax,%rbx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rbp
mov    0x0(%rbp),%esi
mov    (%rbx),%edx
call   1070 <socket@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
je     122a <tcpdial+0xda>
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x1c(%rsp)
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1060 <getaddrinfo@plt>
mov    0x2db5(%rip),%r15
mov    %eax,(%r15)
lea    -0x80(%rax),%ecx
cmp    $0x4,%ecx
ja     1235 <tcpdial+0xe5>
mov    0x2dd3(%rip),%rbx
lea    0xdfc(%rip),%rax
movslq (%rax,%rcx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    0x2d84(%rip),%rbx
jmp    127d <tcpdial+0x12d>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    0x2d7a(%rip),%rcx
mov    %eax,(%rcx)
jmp    1282 <tcpdial+0x132>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
jmp    1282 <tcpdial+0x132>
test   %eax,%eax
jne    1282 <tcpdial+0x132>
mov    (%rsp),%rbp
mov    0x2d7c(%rip),%rbx
test   %rbp,%rbp
je     127d <tcpdial+0x12d>
nopl   0x0(%rax)
mov    (%r14),%edi
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
call   1050 <connect@plt>
test   %eax,%eax
je     1291 <tcpdial+0x141>
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
jne    1250 <tcpdial+0x100>
jmp    127d <tcpdial+0x12d>
mov    0x2d3c(%rip),%rbx
jmp    127d <tcpdial+0x12d>
mov    0x2d4b(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,(%r15)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1282 <tcpdial+0x132>

LLVM IR (-O0):
%struct.protoent = type { i32 }
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32 }
@.str = [4 x i8] c"tcp\00"
@ENOPROTOOPT = global i32 0
@errno = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@ENETUNREACH = global i32 0
@ENETDOWN = global i32 0
@ENOMEM = global i32 0
@EINVAL = global i32 0
@ENOTCONN = global i32 0
define i32 @tcpdial(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.protoent*
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
%11 = alloca %struct.addrinfo*
%12 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.protoent* %13, %struct.protoent** %8
%14 = load %struct.protoent*, %struct.protoent** %8
%15 = icmp eq %struct.protoent* %14, null
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @ENOPROTOOPT
store i32 %17, i32* @errno
%18 = load i32, i32* @errno
store i32 %18, i32* %4
br label %90
19: ; preds = %3
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = load %struct.protoent*, %struct.protoent** %8
%23 = getelementptr inbounds %struct.protoent, %struct.protoent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %20, i32 %21, i32 %24)
%26 = load i32*, i32** %5
store i32 %25, i32* %26
%27 = load i32*, i32** %5
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %19
%31 = load i32, i32* @errno
store i32 %31, i32* %4
br label %90
32: ; preds = %19
%33 = call i32 @memset(%struct.addrinfo* %9, i32 0, i32 32)
%34 = load i32, i32* @AF_UNSPEC
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 5
store i32 %34, i32* %35
%36 = load i32, i32* @SOCK_STREAM
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 4
store i32 %36, i32* %37
%38 = load %struct.protoent*, %struct.protoent** %8
%39 = getelementptr inbounds %struct.protoent, %struct.protoent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
store i32 %40, i32* %41
%42 = load i8*, i8** %6
%43 = load i8*, i8** %7
%44 = call i32 @getaddrinfo(i8* %42, i8* %43, %struct.addrinfo* %9, %struct.addrinfo** %10)
store i32 %44, i32* @errno
%45 = load i32, i32* @errno
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %60
47: ; preds = %32
%48 = load i32, i32* @errno
switch i32 %48, label %57 [
i32 132, label %49
i32 131, label %51
i32 130, label %53
i32 129, label %55
i32 128, label %55
]
49: ; preds = %47
%50 = load i32, i32* @ENETUNREACH
store i32 %50, i32* @errno
br label %58
51: ; preds = %47
%52 = load i32, i32* @ENETDOWN
store i32 %52, i32* @errno
br label %58
53: ; preds = %47
%54 = load i32, i32* @ENOMEM
store i32 %54, i32* @errno
br label %58
55: ; preds = %47, %47
%56 = load i32, i32* @EINVAL
store i32 %56, i32* @errno
br label %58
57: ; preds = %47
br label %58
58: ; preds = %57, %55, %53, %51, %49
%59 = load i32, i32* @errno
store i32 %59, i32* %4
br label %90
60: ; preds = %32
%61 = load %struct.addrinfo*, %struct.addrinfo** %10
store %struct.addrinfo* %61, %struct.addrinfo** %11
br label %62
62: ; preds = %79, %60
%63 = load %struct.addrinfo*, %struct.addrinfo** %11
%64 = icmp ne %struct.addrinfo* %63, null
br i1 %64, label %65, label %83
65: ; preds = %62
%66 = load i32*, i32** %5
%67 = load i32, i32* %66
%68 = load %struct.addrinfo*, %struct.addrinfo** %11
%69 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %68, i32 0, i32 1
%70 = load i32, i32* %69
%71 = load %struct.addrinfo*, %struct.addrinfo** %11
%72 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @connect(i32 %67, i32 %70, i32 %73)
store i32 %74, i32* %12
%75 = load i32, i32* %12
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %65
br label %79
78: ; preds = %65
br label %83
79: ; preds = %77
%80 = load %struct.addrinfo*, %struct.addrinfo** %11
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 2
%82 = load %struct.addrinfo*, %struct.addrinfo** %81
store %struct.addrinfo* %82, %struct.addrinfo** %11
br label %62
83: ; preds = %78, %62
%84 = load %struct.addrinfo*, %struct.addrinfo** %11
%85 = icmp eq %struct.addrinfo* %84, null
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i32, i32* @ENOTCONN
store i32 %87, i32* @errno
%88 = load i32, i32* @errno
store i32 %88, i32* %4
br label %90
89: ; preds = %83
store i32 0, i32* %4
br label %90
90: ; preds = %89, %86, %58, %30, %16
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32 }
%struct.protoent = type { i32 }
@.str = [4 x i8] c"tcp\00"
@ENOPROTOOPT = global i32 0
@errno = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@ENETUNREACH = global i32 0
@ENETDOWN = global i32 0
@ENOMEM = global i32 0
@EINVAL = global i32 0
@ENOTCONN = global i32 0
define i32 @tcpdial(i32* nocapture %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq %struct.protoent* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = load i32, i32* @ENOPROTOOPT
store i32 %9, i32* @errno
br label %54
10: ; preds = %3
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.protoent, %struct.protoent* %6, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @socket(i32 %11, i32 %12, i32 %14)
store i32 %15, i32* %0
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = load i32, i32* @errno
br label %54
19: ; preds = %10
%20 = bitcast %struct.addrinfo* %4 to i8*
%21 = bitcast %struct.addrinfo** %5 to i8*
%22 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%23 = load i32, i32* @AF_UNSPEC
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %23, i32* %24
%25 = load i32, i32* @SOCK_STREAM
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %25, i32* %26
%27 = load i32, i32* %13
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %27, i32* %28
%29 = call i32 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %29, i32* @errno
switch i32 %29, label %52 [
i32 0, label %30
i32 132, label %49
i32 131, label %33
i32 130, label %34
i32 129, label %35
i32 128, label %35
]
30: ; preds = %19
%31 = load %struct.addrinfo*, %struct.addrinfo** %5
%32 = icmp eq %struct.addrinfo* %31, null
br i1 %32, label %49, label %40
33: ; preds = %19
br label %49
34: ; preds = %19
br label %49
35: ; preds = %19, %19
br label %49
36: ; preds = %40
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i64 0, i32 2
%38 = load %struct.addrinfo*, %struct.addrinfo** %37
%39 = icmp eq %struct.addrinfo* %38, null
br i1 %39, label %49, label %40
40: ; preds = %30, %36
%41 = phi %struct.addrinfo* [ %38, %36 ], [ %31, %30 ]
%42 = load i32, i32* %0
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @connect(i32 %42, i32 %44, i32 %46)
%48 = icmp eq i32 %47, 0
br i1 %48, label %52, label %36
49: ; preds = %36, %30, %19, %35, %34, %33
%50 = phi i32* [ @EINVAL, %35 ], [ @ENOMEM, %34 ], [ @ENETDOWN, %33 ], [ @ENETUNREACH, %19 ], [ @ENOTCONN, %30 ], [ @ENOTCONN, %36 ]
%51 = load i32, i32* %50
store i32 %51, i32* @errno
br label %52
52: ; preds = %40, %49, %19
%53 = phi i32 [ %29, %19 ], [ %51, %49 ], [ 0, %40 ]
br label %54
54: ; preds = %52, %17, %8
%55 = phi i32 [ %9, %8 ], [ %18, %17 ], [ %53, %52 ]
ret i32 %55
}