#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ listen_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IN6ADDR_ANY_INIT;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_RECVPKTINFO;
int /*<<< orphan*/ IPV6_V6ONLY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int magic_create_sock6(uint16_t listen_port) {
  static const int on = 1;
  struct sockaddr_in6 listen_addr = {.sin6_family = AF_INET6,
                                     .sin6_port = htons(listen_port),
                                     .sin6_addr = IN6ADDR_ANY_INIT};
  int fd, ret;
  fd = socket(AF_INET6, SOCK_DGRAM, 0);
  if (fd < 0)
    return fd;
  ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  if (ret < 0)
    goto err;
  ret = setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on));
  if (ret < 0)
    goto err;
  ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
  if (ret < 0)
    goto err;
  ret = bind(fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr));
  if (ret < 0)
    goto err;
  return fd;
err:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<magic_create_sock6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11af <magic_create_sock6+0x5f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1296 <magic_create_sock6+0x146>
mov    -0x1c(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
lea    0xe35(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e8 <magic_create_sock6+0x98>
jmp    1288 <magic_create_sock6+0x138>
mov    -0x1c(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    0xdfc(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1221 <magic_create_sock6+0xd1>
jmp    1288 <magic_create_sock6+0x138>
mov    -0x1c(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
lea    0xdc3(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    125a <magic_create_sock6+0x10a>
jmp    1288 <magic_create_sock6+0x138>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    127d <magic_create_sock6+0x12d>
jmp    1288 <magic_create_sock6+0x138>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1296 <magic_create_sock6+0x146>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<magic_create_sock6>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     122b <magic_create_sock6+0xdb>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
lea    0xe53(%rip),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1220 <magic_create_sock6+0xd0>
mov    0x2df3(%rip),%r14
mov    (%r14),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
lea    0xe28(%rip),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1220 <magic_create_sock6+0xd0>
mov    (%r14),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0xe04(%rip),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1220 <magic_create_sock6+0xd0>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
jns    122b <magic_create_sock6+0xdb>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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