#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s6_addr;
};
struct sockaddr_in6 {
  TYPE_1__ sin6_addr;
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
TYPE_1__ in6addr_any;
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int nw_connect_in6(const char *host, int port) {
  struct sockaddr_in6 serv_addr;
  serv_addr.sin6_family = AF_INET6;
  serv_addr.sin6_port = htons(10000);
  if (host && *host) {
    if (!inet_pton(AF_INET6, host, &serv_addr.sin6_addr.s6_addr))
      return -1;
  } else {
    serv_addr.sin6_addr = in6addr_any;
  }
  int sockfd = socket(AF_INET6, SOCK_STREAM, 0);
  if (sockfd < 0)
    return -1;
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    close(sockfd);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<nw_connect_in6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x2710,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c3 <nw_connect_in6+0x73>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c3 <nw_connect_in6+0x73>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%eax
jne    11be <nw_connect_in6+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <nw_connect_in6+0xe6>
jmp    11cf <nw_connect_in6+0x7f>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1201 <nw_connect_in6+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <nw_connect_in6+0xe6>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1230 <nw_connect_in6+0xe0>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <nw_connect_in6+0xe6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nw_connect_in6>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e80(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x2710,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
test   %rbx,%rbx
je     119a <nw_connect_in6+0x4a>
cmpb   $0x0,(%rbx)
je     119a <nw_connect_in6+0x4a>
mov    0x0(%rbp),%edi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <inet_pton@plt>
test   %eax,%eax
jne    11a7 <nw_connect_in6+0x57>
mov    $0xffffffff,%ebx
jmp    11e6 <nw_connect_in6+0x96>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11e6 <nw_connect_in6+0x96>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11df <nw_connect_in6+0x8f>
mov    %ebp,%ebx
jmp    11e6 <nw_connect_in6+0x96>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { %struct.TYPE_2__, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@in6addr_any = global %struct.TYPE_2__ zeroinitializer
@SOCK_STREAM = global i32 0
define i32 @nw_connect_in6(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in6
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET6
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @htons(i32 10000)
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %28
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i32, i32* @AF_INET6
%21 = load i8*, i8** %4
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = call i32 @inet_pton(i32 %20, i8* %21, i32* %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %19
store i32 -1, i32* %3
br label %48
27: ; preds = %19
br label %31
28: ; preds = %14, %2
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
%30 = bitcast %struct.TYPE_2__* %29 to i8*
br label %31
31: ; preds = %28, %27
%32 = load i32, i32* @AF_INET6
%33 = load i32, i32* @SOCK_STREAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 -1, i32* %3
br label %48
38: ; preds = %31
%39 = load i32, i32* %7
%40 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%41 = call i64 @connect(i32 %39, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
store i32 -1, i32* %3
br label %48
46: ; preds = %38
%47 = load i32, i32* %7
store i32 %47, i32* %3
br label %48
48: ; preds = %46, %43, %37, %26
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { %struct.TYPE_2__, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@in6addr_any = global %struct.TYPE_2__ zeroinitializer
@SOCK_STREAM = global i32 0
define i32 @nw_connect_in6(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in6
%4 = bitcast %struct.sockaddr_in6* %3 to i8*
%5 = load i32, i32* @AF_INET6
%6 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @htons(i32 10000)
%8 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
store i32 %7, i32* %8
%9 = icmp eq i8* %0, null
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = load i32, i32* @AF_INET6
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0, i32 0
%16 = call i32 @inet_pton(i32 %14, i8* %0, i32* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %32, label %21
18: ; preds = %10, %2
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0, i32 0
%20 = load i32, i32* getelementptr inbounds (%struct.TYPE_2__, %struct.TYPE_2__* @in6addr_any, i64 0, i32 0)
store i32 %20, i32* %19
br label %21
21: ; preds = %13, %18
%22 = load i32, i32* @AF_INET6
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %21
%27 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%28 = call i64 @connect(i32 %24, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @close(i32 %24)
br label %32
32: ; preds = %30, %21, %26, %13
%33 = phi i32 [ -1, %13 ], [ -1, %30 ], [ -1, %21 ], [ %24, %26 ]
ret i32 %33
}