#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_addr;
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ saddr6;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_V6ONLY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ in6addr_any;
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int gen_socket6(const int port) {
  int server_socket6;
  server_socket6 = socket(AF_INET6, SOCK_STREAM, 0);
  if (server_socket6 == -1) {
    perror("socketv6");
    exit(-1);
  }
  int optval = 1;
  if (setsockopt(server_socket6, SOL_SOCKET, SO_REUSEADDR, &optval,
                 sizeof(int)) == -1) {
    perror("SO_REUSEADR option on IPv6 socket");
    exit(-1);
  }
  if (setsockopt(server_socket6, IPPROTO_IPV6, IPV6_V6ONLY, &optval,
                 sizeof(int)) == -1) {
    perror("IPV6_V6ONLY on IPv6 socket");
    exit(-1);
  }
  struct sockaddr_in6 saddr6;
  saddr6.sin6_family = AF_INET6;
  saddr6.sin6_port = htons(port);
  saddr6.sin6_addr = in6addr_any;
  if (bind(server_socket6, (struct sockaddr *)&saddr6, sizeof(saddr6)) == -1) {
    perror("bind server IPv6 socket");
    exit(-1);
  }
  if (listen(server_socket6, 10) == -1) {
    perror("listen IPv6 socket");
    exit(-1);
  }
  return server_socket6;
}


x86 assembly (clang, -O0):
<gen_socket6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b7 <gen_socket6+0x47>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1201 <gen_socket6+0x91>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1244 <gen_socket6+0xd4>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1297 <gen_socket6+0x127>
lea    0xdbe(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c3 <gen_socket6+0x153>
lea    0xdaa(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_socket6>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    0x2e57(%rip),%r14
mov    (%r14),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     124e <gen_socket6+0xde>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1257 <gen_socket6+0xe7>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1260 <gen_socket6+0xf0>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1269 <gen_socket6+0xf9>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
je     1272 <gen_socket6+0x102>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdab(%rip),%rdi
jmp    1279 <gen_socket6+0x109>
lea    0xdab(%rip),%rdi
jmp    1279 <gen_socket6+0x109>
lea    0xdc4(%rip),%rdi
jmp    1279 <gen_socket6+0x109>
lea    0xdd6(%rip),%rdi
jmp    1279 <gen_socket6+0x109>
lea    0xde5(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socketv6\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [34 x i8] c"SO_REUSEADR option on IPv6 socket\00"
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.2 = [27 x i8] c"IPV6_V6ONLY on IPv6 socket\00"
@in6addr_any = global i32 0
@.str.3 = [24 x i8] c"bind server IPv6 socket\00"
@.str.4 = [19 x i8] c"listen IPv6 socket\00"
define i32 @gen_socket6(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in6
store i32 %0, i32* %2
%6 = load i32, i32* @AF_INET6
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
store i32 1, i32* %4
%15 = load i32, i32* %3
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %4, i32 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %14
%24 = load i32, i32* %3
%25 = load i32, i32* @IPPROTO_IPV6
%26 = load i32, i32* @IPV6_V6ONLY
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %4, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %23
%33 = load i32, i32* @AF_INET6
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* %2
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* @in6addr_any
%39 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %3
%41 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %32
%45 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %32
%48 = load i32, i32* %3
%49 = call i32 @listen(i32 %48, i32 10)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %47
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socketv6\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [34 x i8] c"SO_REUSEADR option on IPv6 socket\00"
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.2 = [27 x i8] c"IPV6_V6ONLY on IPv6 socket\00"
@in6addr_any = global i32 0
@.str.3 = [24 x i8] c"bind server IPv6 socket\00"
@.str.4 = [19 x i8] c"listen IPv6 socket\00"
define i32 @gen_socket6(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in6
%4 = load i32, i32* @AF_INET6
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %1
%12 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %6, i32 %13, i32 %14, i32* %2, i32 4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %11
%21 = load i32, i32* @IPPROTO_IPV6
%22 = load i32, i32* @IPV6_V6ONLY
%23 = call i32 @setsockopt(i32 %6, i32 %21, i32 %22, i32* %2, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %20
%29 = bitcast %struct.sockaddr_in6* %3 to i8*
%30 = load i32, i32* @AF_INET6
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %30, i32* %31
%32 = call i32 @htons(i32 %0)
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @in6addr_any
%35 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%37 = call i32 @bind(i32 %6, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %28
%40 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %28
%43 = call i32 @listen(i32 %6, i32 10)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %42
ret i32 %6
}