#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 sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, char, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_remote(char *host, int port) {
  struct sockaddr_in6 addr;
  struct hostent *he;
  int len, s, x, on = 1;
  he = gethostbyname(host);
  if (!he)
    return (-2);
  len = sizeof(addr);
  s = socket(AF_INET6, SOCK_STREAM, 0);
  if (s < 0)
    return s;
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, 4);
  len = sizeof(addr);
  memset(&addr, '\0', len);
  addr.sin6_family = AF_INET6;
  memcpy(&addr.sin6_addr, he->h_addr, he->h_length);
  addr.sin6_port = htons(port);
  x = connect(s, (struct sockaddr *)&addr, len);
  if (x < 0) {
    close(s);
    return x;
  }
  return s;
}


x86 assembly (clang, -O0):
<connect_remote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ba <connect_remote+0x3a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1291 <connect_remote+0x111>
movl   $0xc,-0x2c(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11f2 <connect_remote+0x72>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1291 <connect_remote+0x111>
mov    -0x30(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x2c(%rbp),%edx
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    128b <connect_remote+0x10b>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1291 <connect_remote+0x111>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_remote>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
movl   $0x1,0xc(%rsp)
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1249 <connect_remote+0xc9>
mov    %rax,%rbp
mov    0x2e31(%rip),%r12
mov    (%r12),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     124e <connect_remote+0xce>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
lea    0x14(%rsp),%rdi
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
call   1080 <memcpy@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %eax,%eax
jns    124e <connect_remote+0xce>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
jmp    124e <connect_remote+0xce>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @connect_remote(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in6
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %11
%12 = load i8*, i8** %4
%13 = call %struct.hostent* @gethostbyname(i8* %12)
store %struct.hostent* %13, %struct.hostent** %7
%14 = load %struct.hostent*, %struct.hostent** %7
%15 = icmp ne %struct.hostent* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 -2, i32* %3
br label %57
17: ; preds = %2
store i32 12, i32* %8
%18 = load i32, i32* @AF_INET6
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* %9
store i32 %24, i32* %3
br label %57
25: ; preds = %17
%26 = load i32, i32* %9
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i32 @setsockopt(i32 %26, i32 %27, i32 %28, i32* %11, i32 4)
store i32 12, i32* %8
%30 = load i32, i32* %8
%31 = call i32 @memset(%struct.sockaddr_in6* %6, i8 signext 0, i32 %30)
%32 = load i32, i32* @AF_INET6
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 2
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 1
%35 = load %struct.hostent*, %struct.hostent** %7
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.hostent*, %struct.hostent** %7
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @memcpy(i32* %34, i32 %37, i32 %40)
%42 = load i32, i32* %5
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* %9
%46 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%47 = load i32, i32* %8
%48 = call i32 @connect(i32 %45, %struct.sockaddr* %46, i32 %47)
store i32 %48, i32* %10
%49 = load i32, i32* %10
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %25
%52 = load i32, i32* %9
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %10
store i32 %54, i32* %3
br label %57
55: ; preds = %25
%56 = load i32, i32* %9
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %51, %23, %16
%58 = load i32, i32* %3
ret i32 %58
}

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