#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*/ freeaddrinfo(struct addrinfo *);
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 tcp_connect(char *address, char *port) {
  struct addrinfo hints, *ai, *p;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(address, port, &hints, &ai)) != 0) {
    return 0;
  }
  for (p = ai; p != NULL; p = p->ai_next) {
    rv = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (rv < 0)
      continue;
    if (connect(rv, p->ai_addr, p->ai_addrlen) < 0) {
      close(rv);
      continue;
    }
    break;
  }
  freeaddrinfo(ai);
  if (p == NULL) {
    return 0;
  }
  return rv;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     11c5 <tcp_connect+0x65>
movl   $0x0,-0x4(%rbp)
jmp    126c <tcp_connect+0x10c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1246 <tcp_connect+0xe6>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1204 <tcp_connect+0xa4>
jmp    1235 <tcp_connect+0xd5>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    1230 <tcp_connect+0xd0>
mov    -0x4c(%rbp),%edi
call   1040 <close@plt>
jmp    1235 <tcp_connect+0xd5>
jmp    1246 <tcp_connect+0xe6>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    11cd <tcp_connect+0x6d>
mov    -0x40(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
cmpq   $0x0,-0x48(%rbp)
jne    1266 <tcp_connect+0x106>
movl   $0x0,-0x4(%rbp)
jmp    126c <tcp_connect+0x10c>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x10(%rsp),%r12
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
jne    1213 <tcp_connect+0xb3>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11d9 <tcp_connect+0x79>
xor    %edi,%edi
xor    %ebp,%ebp
jmp    120e <tcp_connect+0xae>
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1207 <tcp_connect+0xa7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1080 <socket@plt>
test   %eax,%eax
js     11d0 <tcp_connect+0x70>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <connect@plt>
test   %rax,%rax
jns    1209 <tcp_connect+0xa9>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11d0 <tcp_connect+0x70>
xor    %ebp,%ebp
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
define i32 @tcp_connect(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 32)
%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 i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %47
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %43, label %17
17: ; preds = %14, %36
%18 = phi %struct.addrinfo* [ %38, %36 ], [ %15, %14 ]
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 4
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %20, i32 %22, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %17
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i64 @connect(i32 %25, i32 %29, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %27
%35 = call i32 @close(i32 %25)
br label %36
36: ; preds = %17, %34
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 5
%38 = load %struct.addrinfo*, %struct.addrinfo** %37
%39 = icmp eq %struct.addrinfo* %38, null
br i1 %39, label %40, label %17
40: ; preds = %27, %36
%41 = phi i32 [ 0, %36 ], [ %25, %27 ]
%42 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %43
43: ; preds = %40, %14
%44 = phi %struct.addrinfo* [ null, %14 ], [ %42, %40 ]
%45 = phi i32 [ 0, %14 ], [ %41, %40 ]
%46 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
br label %47
47: ; preds = %43, %2
%48 = phi i32 [ 0, %2 ], [ %45, %43 ]
ret i32 %48
}