#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;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_NUMERICSERV;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ warnx(char *, ...);
int basex_connect(const char *host, const char *port) {
  struct addrinfo hints;
  struct addrinfo *result = NULL, *rp;
  int sfd, rc;
  if (host == NULL || port == NULL) {
    return -1;
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_NUMERICSERV;
  rc = getaddrinfo(host, port, &hints, &result);
  if (rc != 0) {
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
      continue;
    if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
    close(sfd);
  }
  if (rp == NULL) {
    warnx("Can not connect to BaseX server.");
    warnx("Hostname '%s', port %s.", host, port);
    return -1;
  }
  freeaddrinfo(result);
  return sfd;
}


x86 assembly (clang, -O0):
<basex_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119e <basex_connect+0x2e>
cmpq   $0x0,-0x18(%rbp)
jne    11aa <basex_connect+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <basex_connect+0x161>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
je     120c <basex_connect+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <basex_connect+0x161>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1287 <basex_connect+0x117>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    124b <basex_connect+0xdb>
jmp    1276 <basex_connect+0x106>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     126e <basex_connect+0xfe>
jmp    1287 <basex_connect+0x117>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1214 <basex_connect+0xa4>
cmpq   $0x0,-0x50(%rbp)
jne    12c2 <basex_connect+0x152>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1030 <warnx@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1030 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d1 <basex_connect+0x161>
mov    -0x48(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basex_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movq   $0x0,(%rsp)
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1269 <basex_connect+0xf9>
mov    %rsi,%r15
test   %rsi,%rsi
je     1269 <basex_connect+0xf9>
mov    %rdi,%r12
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
jne    1269 <basex_connect+0xf9>
mov    (%rsp),%rbp
jmp    1204 <basex_connect+0x94>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     1239 <basex_connect+0xc9>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1200 <basex_connect+0x90>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %eax,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    125d <basex_connect+0xed>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1200 <basex_connect+0x90>
lea    0xdc0(%rip),%rdi
xor    %eax,%eax
call   1030 <warnx@plt>
lea    0xdd3(%rip),%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <warnx@plt>
jmp    1269 <basex_connect+0xf9>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebx,%r14d
mov    %r14d,%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*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_NUMERICSERV = global i32 0
@.str = [33 x i8] c"Can not connect to BaseX server.\00"
@.str.1 = [24 x i8] c"Hostname '%s', port %s.\00"
define i32 @basex_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
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.addrinfo* null, %struct.addrinfo** %7
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13, %2
store i32 -1, i32* %3
br label %80
17: ; preds = %13
%18 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%19 = load i32, i32* @AF_UNSPEC
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %21, i32* %22
%23 = load i32, i32* @AI_NUMERICSERV
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i32 %23, i32* %24
%25 = load i8*, i8** %4
%26 = load i8*, i8** %5
%27 = call i32 @getaddrinfo(i8* %25, i8* %26, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %17
store i32 -1, i32* %3
br label %80
31: ; preds = %17
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %32, %struct.addrinfo** %8
br label %33
33: ; preds = %64, %31
%34 = load %struct.addrinfo*, %struct.addrinfo** %8
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %68
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %8
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 4
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %8
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 3
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = call i32 @socket(i32 %39, i32 %42, i32 %45)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %36
br label %64
50: ; preds = %36
%51 = load i32, i32* %9
%52 = load %struct.addrinfo*, %struct.addrinfo** %8
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 1
%54 = load i32, i32* %53
%55 = load %struct.addrinfo*, %struct.addrinfo** %8
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @connect(i32 %51, i32 %54, i32 %57)
%59 = icmp ne i32 %58, -1
br i1 %59, label %60, label %61
60: ; preds = %50
br label %68
61: ; preds = %50
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %49
%65 = load %struct.addrinfo*, %struct.addrinfo** %8
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 5
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
store %struct.addrinfo* %67, %struct.addrinfo** %8
br label %33
68: ; preds = %60, %33
%69 = load %struct.addrinfo*, %struct.addrinfo** %8
%70 = icmp eq %struct.addrinfo* %69, null
br i1 %70, label %71, label %76
71: ; preds = %68
%72 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%73 = load i8*, i8** %4
%74 = load i8*, i8** %5
%75 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %73, i8* %74)
store i32 -1, i32* %3
br label %80
76: ; preds = %68
%77 = load %struct.addrinfo*, %struct.addrinfo** %7
%78 = call i32 @freeaddrinfo(%struct.addrinfo* %77)
%79 = load i32, i32* %9
store i32 %79, i32* %3
br label %80
80: ; preds = %76, %71, %30, %16
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_NUMERICSERV = global i32 0
@.str = [33 x i8] c"Can not connect to BaseX server.\00"
@.str.1 = [24 x i8] c"Hostname '%s', port %s.\00"
define i32 @basex_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*
store %struct.addrinfo* null, %struct.addrinfo** %4
%7 = icmp eq i8* %0, null
%8 = icmp eq i8* %1, null
%9 = or i1 %7, %8
br i1 %9, label %52, label %10
10: ; preds = %2
%11 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @AI_NUMERICSERV
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %16, i32* %17
%18 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %52
20: ; preds = %10
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %46, label %23
23: ; preds = %20, %42
%24 = phi %struct.addrinfo* [ %44, %42 ], [ %21, %20 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%30 = load i32, i32* %29
%31 = call i32 @socket(i32 %26, i32 %28, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %42, label %33
33: ; preds = %23
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @connect(i32 %31, i32 %35, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %49
40: ; preds = %33
%41 = call i32 @close(i32 %31)
br label %42
42: ; preds = %23, %40
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %23
46: ; preds = %42, %20
%47 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%48 = call i32 (i8*, ...) @warnx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
br label %52
49: ; preds = %33
%50 = load %struct.addrinfo*, %struct.addrinfo** %4
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
br label %52
52: ; preds = %10, %2, %49, %46
%53 = phi i32 [ -1, %46 ], [ %31, %49 ], [ -1, %2 ], [ -1, %10 ]
ret i32 %53
}