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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int AI_ALL;
int AI_V4MAPPED;
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, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int j_socket_connect(int type, char *server, char *port) {
  struct addrinfo hints, *res, *ressave;
  int n, sockfd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = type;
  hints.ai_flags = AI_V4MAPPED | AI_ALL;
  n = getaddrinfo(server, port, &hints, &res);
  if (n < 0) {
    if (0)
      fprintf(stderr, "getaddrinfo error:: [%s]\n", gai_strerror(n));
    return -1;
  }
  ressave = res;
  sockfd = -1;
  while (res) {
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (!(sockfd < 0)) {
      if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
        break;
      close(sockfd);
      sockfd = -1;
    }
    res = res->ai_next;
  }
  freeaddrinfo(ressave);
  return sockfd;
}


x86 assembly (clang, -O0):
<j_socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    0x2e2b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%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)
cmpl   $0x0,-0x4c(%rbp)
jge    11d8 <j_socket_connect+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <j_socket_connect+0x10c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0xffffffff,-0x50(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     125d <j_socket_connect+0xfd>
mov    -0x40(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jl     124c <j_socket_connect+0xec>
mov    -0x50(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jne    123d <j_socket_connect+0xdd>
jmp    125d <j_socket_connect+0xfd>
mov    -0x50(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x50(%rbp)
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11e7 <j_socket_connect+0x87>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<j_socket_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    %ebx,0x8(%rsp)
mov    0x2e4a(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
js     1218 <j_socket_connect+0xb8>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     121f <j_socket_connect+0xbf>
mov    %rbx,%rax
jmp    11e1 <j_socket_connect+0x81>
mov    (%rsp),%rax
mov    0x8(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     1224 <j_socket_connect+0xc4>
mov    0x1c(%rax),%edi
mov    (%rax),%esi
mov    0x18(%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11d0 <j_socket_connect+0x70>
mov    (%rsp),%rax
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
mov    %ebp,%edi
call   1050 <connect@plt>
test   %rax,%rax
je     1224 <j_socket_connect+0xc4>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
jmp    11d0 <j_socket_connect+0x70>
mov    $0xffffffff,%ebp
jmp    122c <j_socket_connect+0xcc>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1070 <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, %struct.addrinfo*, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@AI_V4MAPPED = global i32 0
@AI_ALL = global i32 0
@stderr = global i32 0
define i32 @j_socket_connect(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%14 = load i32, i32* @AF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 6
store i32 %14, i32* %15
%16 = load i32, i32* %5
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AI_V4MAPPED
%19 = load i32, i32* @AI_ALL
%20 = or i32 %18, %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i8*, i8** %6
%23 = load i8*, i8** %7
%24 = call i32 @getaddrinfo(i8* %22, i8* %23, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %3
store i32 -1, i32* %4
br label %68
28: ; preds = %3
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %29, %struct.addrinfo** %10
store i32 -1, i32* %12
br label %30
30: ; preds = %60, %28
%31 = load %struct.addrinfo*, %struct.addrinfo** %9
%32 = icmp ne %struct.addrinfo* %31, null
br i1 %32, label %33, label %64
33: ; preds = %30
%34 = load %struct.addrinfo*, %struct.addrinfo** %9
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 6
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %9
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 5
%42 = load i32, i32* %41
%43 = call i32 @socket(i32 %36, i32 %39, i32 %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp slt i32 %44, 0
br i1 %45, label %60, label %46
46: ; preds = %33
%47 = load i32, i32* %12
%48 = load %struct.addrinfo*, %struct.addrinfo** %9
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 4
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %9
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 3
%53 = load i32, i32* %52
%54 = call i64 @connect(i32 %47, i32 %50, i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %46
br label %64
57: ; preds = %46
%58 = load i32, i32* %12
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %12
br label %60
60: ; preds = %57, %33
%61 = load %struct.addrinfo*, %struct.addrinfo** %9
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 2
%63 = load %struct.addrinfo*, %struct.addrinfo** %62
store %struct.addrinfo* %63, %struct.addrinfo** %9
br label %30
64: ; preds = %56, %30
%65 = load %struct.addrinfo*, %struct.addrinfo** %10
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
%67 = load i32, i32* %12
store i32 %67, i32* %4
br label %68
68: ; preds = %64, %27
%69 = load i32, i32* %4
ret i32 %69
}

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