#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tconnect(char *target, char *target_port) {
  int retfd;
  struct addrinfo hints, *servinfo;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(target, target_port, &hints, &servinfo) != 0) {
    return -1;
  }
  if ((retfd = socket(servinfo->ai_family, servinfo->ai_socktype,
                      servinfo->ai_protocol)) == -1) {
    return -2;
  }
  if (connect(retfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
    return -3;
  }
  freeaddrinfo(servinfo);
  return retfd;
}


x86 assembly (clang, -O0):
<tconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1050 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11b3 <tconnect+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <tconnect+0xce>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11e5 <tconnect+0x95>
movl   $0xfffffffe,-0x4(%rbp)
jmp    121e <tconnect+0xce>
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1040 <connect@plt>
cmp    $0xffffffff,%eax
jne    120f <tconnect+0xbf>
movl   $0xfffffffd,-0x4(%rbp)
jmp    121e <tconnect+0xce>
mov    -0x38(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tconnect>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11b4 <tconnect+0x64>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11ed <tconnect+0x9d>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1040 <connect@plt>
cmp    $0xffffffff,%eax
je     11f4 <tconnect+0xa4>
mov    (%rsp),%rdi
call   1060 <freeaddrinfo@plt>
jmp    11a8 <tconnect+0x58>
mov    $0xfffffffe,%ebx
jmp    11a8 <tconnect+0x58>
mov    $0xfffffffd,%ebx
jmp    11a8 <tconnect+0x58>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
define i32 @tconnect(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 20)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = call i64 @getaddrinfo(i8* %14, i8* %15, %struct.addrinfo* %7, %struct.addrinfo** %8)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %47
19: ; preds = %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %8
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 4
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %8
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 3
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %8
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %22, i32 %25, i32 %28)
store i32 %29, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %19
store i32 -2, i32* %3
br label %47
32: ; preds = %19
%33 = load i32, i32* %6
%34 = load %struct.addrinfo*, %struct.addrinfo** %8
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %8
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @connect(i32 %33, i32 %36, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %32
store i32 -3, i32* %3
br label %47
43: ; preds = %32
%44 = load %struct.addrinfo*, %struct.addrinfo** %8
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
%46 = load i32, i32* %6
store i32 %46, i32* %3
br label %47
47: ; preds = %43, %42, %31, %18
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
define i32 @tconnect(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 20)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = call i64 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %35
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 4
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 3
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 2
%21 = load i32, i32* %20
%22 = call i32 @socket(i32 %17, i32 %19, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %35, label %24
24: ; preds = %14
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @connect(i32 %22, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %35, label %32
32: ; preds = %24
%33 = load %struct.addrinfo*, %struct.addrinfo** %4
%34 = call i32 @freeaddrinfo(%struct.addrinfo* %33)
br label %35
35: ; preds = %24, %14, %2, %32
%36 = phi i32 [ %22, %32 ], [ -1, %2 ], [ -2, %14 ], [ -3, %24 ]
ret i32 %36
}