#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 msghdr {
  int dummy;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  int /*<<< orphan*/ cmsg_len;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
void *CMSG_DATA(struct cmsghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ IPV6_RTHDR;
int /*<<< orphan*/ PF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet6_rth_add2(void *, int /*<<< orphan*/ *);
void *inet6_rth_init2(void *, int, int, int);
int inet6_rth_space2(int, int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
struct cmsghdr *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in6 addr;
  void *extptr;
  socklen_t extlen;
  struct msghdr msg;
  struct cmsghdr *cmsgptr;
  int cmsglen;
  struct sockaddr_in6 server;
  memset((char *)&server, 0, sizeof(server));
  inet_pton(AF_INET6, "fda0:c365:c175:7e1::23", &server.sin6_addr);
  extlen = inet6_rth_space2(2, 1);
  cmsglen = CMSG_SPACE(extlen);
  cmsgptr = malloc(cmsglen);
  cmsgptr->cmsg_len = CMSG_LEN(extlen);
  cmsgptr->cmsg_level = IPPROTO_IPV6;
  cmsgptr->cmsg_type = IPV6_RTHDR;
  extptr = CMSG_DATA(cmsgptr);
  printf("inet6_rth_init\n");
  extptr = inet6_rth_init2(extptr, extlen, 2, 1);
  printf("inet6_rth_add\n");
  inet6_rth_add2(extptr, &server.sin6_addr);
  printf("setsockopt extlen:%d %x\n", extlen, extlen);
  if (setsockopt(sock, IPPROTO_IPV6, IPV6_RTHDR, extptr, extlen) == -1) {
    perror("setsockopt");
    exit(1);
  }
  addr.sin6_family = AF_INET6;
  printf("inet_pton\n");
  inet_pton(AF_INET6, "2620:52:0:42c0:5eff:35ff:fe0a:29f8", &addr.sin6_addr);
  addr.sin6_port = htons(7378);
  printf("connect\n");
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    perror("connect");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
call   1120 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
add    $0x4,%rdx
lea    0xda6(%rip),%rsi
call   10b0 <inet_pton@plt>
mov    $0x2,%edi
mov    $0x1,%esi
call   10f0 <inet6_rth_space2@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   10a0 <CMSG_SPACE@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2d18(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2d38(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x20(%rbp)
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    $0x2,%edx
mov    $0x1,%ecx
call   10d0 <inet6_rth_init2@plt>
mov    %rax,-0x20(%rbp)
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rsi
add    $0x4,%rsi
call   1050 <inet6_rth_add2@plt>
mov    -0x24(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2c8e(%rip),%rax
mov    (%rax),%esi
mov    0x2cb5(%rip),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1361 <main+0x161>
lea    0xcfd(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xce6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
add    $0x4,%rdx
lea    0xcd2(%rip),%rsi
call   10b0 <inet_pton@plt>
mov    $0x1cd2,%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
lea    0xcdc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0xffffffff,%eax
jne    13e3 <main+0x1e3>
lea    0xcbd(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
call   1130 <socket@plt>
mov    %eax,%r15d
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0x1c(%rsp),%r14
lea    0xd99(%rip),%rsi
mov    %r14,%rdx
call   10c0 <inet_pton@plt>
mov    $0x2,%edi
mov    $0x1,%esi
call   1100 <inet6_rth_space2@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   10b0 <CMSG_SPACE@plt>
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   10a0 <CMSG_LEN@plt>
mov    %eax,0x8(%rbx)
mov    0x2d16(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x4(%rbx)
mov    0x2d38(%rip),%r12
mov    (%r12),%eax
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,%rbx
lea    0xd96(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x2,%edx
mov    $0x1,%ecx
call   10e0 <inet6_rth_init2@plt>
mov    %rax,%rbx
lea    0xd82(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <inet6_rth_add2@plt>
lea    0xd15(%rip),%rdi
mov    %ebp,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x0(%r13),%esi
mov    (%r12),%edx
mov    %r15d,%edi
mov    %rbx,%rcx
mov    %ebp,%r8d
call   1050 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1396 <main+0x186>
mov    0x2ca1(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
lea    0xd3f(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%edi
lea    0xc(%rsp),%rdx
lea    0xcec(%rip),%rsi
call   10c0 <inet_pton@plt>
mov    $0x1cd2,%edi
call   1070 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xd1c(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1120 <connect@plt>
cmp    $0xffffffff,%eax
je     139f <main+0x18f>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc93(%rip),%rdi
jmp    13a6 <main+0x196>
lea    0xcdf(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.msghdr = type { i32 }
%struct.cmsghdr = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET6 = global i32 0
@.str = [23 x i8] c"fda0:c365:c175:7e1::23\00"
@IPPROTO_IPV6 = global i32 0
@IPV6_RTHDR = global i32 0
@.str.1 = [16 x i8] c"inet6_rth_init\0A\00"
@.str.2 = [15 x i8] c"inet6_rth_add\0A\00"
@.str.3 = [25 x i8] c"setsockopt extlen:%d %x\0A\00"
@.str.4 = [11 x i8] c"setsockopt\00"
@.str.5 = [11 x i8] c"inet_pton\0A\00"
@.str.6 = [35 x i8] c"2620:52:0:42c0:5eff:35ff:fe0a:29f8\00"
@.str.7 = [9 x i8] c"connect\0A\00"
@.str.8 = [8 x i8] c"connect\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in6
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.msghdr
%7 = alloca %struct.cmsghdr*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in6
store i32 0, i32* %1
%10 = load i32, i32* @PF_INET6
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %2
%14 = bitcast %struct.sockaddr_in6* %9 to i8*
%15 = call i32 @memset(i8* %14, i32 0, i32 12)
%16 = load i32, i32* @AF_INET6
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 1
%18 = call i32 @inet_pton(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32* %17)
%19 = call i32 @inet6_rth_space2(i32 2, i32 1)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = call i32 @CMSG_SPACE(i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = call %struct.cmsghdr* @malloc(i32 %22)
store %struct.cmsghdr* %23, %struct.cmsghdr** %7
%24 = load i32, i32* %5
%25 = call i32 @CMSG_LEN(i32 %24)
%26 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i32 0, i32 2
store i32 %25, i32* %27
%28 = load i32, i32* @IPPROTO_IPV6
%29 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%30 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %29, i32 0, i32 1
store i32 %28, i32* %30
%31 = load i32, i32* @IPV6_RTHDR
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%35 = call i8* @CMSG_DATA(%struct.cmsghdr* %34)
store i8* %35, i8** %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = call i8* @inet6_rth_init2(i8* %37, i32 %38, i32 2, i32 1)
store i8* %39, i8** %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%41 = load i8*, i8** %4
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 1
%43 = call i32 @inet6_rth_add2(i8* %41, i32* %42)
%44 = load i32, i32* %5
%45 = load i32, i32* %5
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %45)
%47 = load i32, i32* %2
%48 = load i32, i32* @IPPROTO_IPV6
%49 = load i32, i32* @IPV6_RTHDR
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = call i32 @setsockopt(i32 %47, i32 %48, i32 %49, i8* %50, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %0
%55 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %0
%58 = load i32, i32* @AF_INET6
%59 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i32 0, i32 2
store i32 %58, i32* %59
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%61 = load i32, i32* @AF_INET6
%62 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i32 0, i32 1
%63 = call i32 @inet_pton(i32 %61, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32* %62)
%64 = call i32 @htons(i32 7378)
%65 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i32 0, i32 0
store i32 %64, i32* %65
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%67 = load i32, i32* %2
%68 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%69 = call i32 @connect(i32 %67, %struct.sockaddr* %68, i32 12)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %57
%72 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %57
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET6 = global i32 0
@.str = [23 x i8] c"fda0:c365:c175:7e1::23\00"
@IPPROTO_IPV6 = global i32 0
@IPV6_RTHDR = global i32 0
@.str.3 = [25 x i8] c"setsockopt extlen:%d %x\0A\00"
@.str.4 = [11 x i8] c"setsockopt\00"
@.str.6 = [35 x i8] c"2620:52:0:42c0:5eff:35ff:fe0a:29f8\00"
@str = [15 x i8] c"inet6_rth_init\00"
@str.9 = [14 x i8] c"inet6_rth_add\00"
@str.10 = [10 x i8] c"inet_pton\00"
@str.11 = [8 x i8] c"connect\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in6
%2 = alloca %struct.sockaddr_in6
%3 = load i32, i32* @PF_INET6
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = bitcast %struct.sockaddr_in6* %1 to i8*
%8 = bitcast %struct.sockaddr_in6* %2 to i8*
%9 = call i32 @memset(i8* %8, i32 0, i32 12)
%10 = load i32, i32* @AF_INET6
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
%12 = call i32 @inet_pton(i32 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = call i32 @inet6_rth_space2(i32 2, i32 1)
%14 = call i32 @CMSG_SPACE(i32 %13)
%15 = call %struct.cmsghdr* @malloc(i32 %14)
%16 = call i32 @CMSG_LEN(i32 %13)
%17 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @IPPROTO_IPV6
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @IPV6_RTHDR
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i64 0, i32 0
store i32 %20, i32* %21
%22 = call i8* @CMSG_DATA(%struct.cmsghdr* %15)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%24 = call i8* @inet6_rth_init2(i8* %22, i32 %13, i32 2, i32 1)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%26 = call i32 @inet6_rth_add2(i8* %24, i32* %11)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %13, i32 %13)
%28 = load i32, i32* @IPPROTO_IPV6
%29 = load i32, i32* @IPV6_RTHDR
%30 = call i32 @setsockopt(i32 %6, i32 %28, i32 %29, i8* %24, i32 %13)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %0
%33 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %0
%36 = load i32, i32* @AF_INET6
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 0, i32 2
store i32 %36, i32* %37
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%39 = load i32, i32* @AF_INET6
%40 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 0, i32 1
%41 = call i32 @inet_pton(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32* %40)
%42 = call i32 @htons(i32 7378)
%43 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 0, i32 0
store i32 %42, i32* %43
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.11, i64 0, i64 0))
%45 = bitcast %struct.sockaddr_in6* %1 to %struct.sockaddr*
%46 = call i32 @connect(i32 %6, %struct.sockaddr* %45, i32 12)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %35
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str.11, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %35
ret i32 0
}