#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;
};
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ in6addr_any;
int inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ss_listen_on_ipv6_socket_impl(const char *address, unsigned short port,
                                  int backlog) {
  struct sockaddr_in6 addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin6_family = AF_INET6;
  if (address) {
    if (!(inet_pton(AF_INET6, address, &(addr.sin6_addr)) == 1))
      return -1;
  } else
    addr.sin6_addr = in6addr_any;
  int sock = socket(AF_INET6, SOCK_STREAM, 0);
  if (sock == -1)
    return -2;
  fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
  int optval = 1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  addr.sin6_port = htons(port);
  if ((bind(sock, (struct sockaddr *)&addr, sizeof(addr))) == -1) {
    int saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -3;
  }
  if ((listen(sock, backlog)) == -1) {
    int saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -4;
  }
  return sock;
}


x86 assembly (clang, -O0):
<ss_listen_on_ipv6_socket_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1201 <ss_listen_on_ipv6_socket_impl+0x71>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1080 <inet_pton@plt>
cmp    $0x1,%eax
je     11fc <ss_listen_on_ipv6_socket_impl+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <ss_listen_on_ipv6_socket_impl+0x1af>
jmp    120d <ss_listen_on_ipv6_socket_impl+0x7d>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    123f <ss_listen_on_ipv6_socket_impl+0xaf>
movl   $0xfffffffe,-0x4(%rbp)
jmp    133f <ss_listen_on_ipv6_socket_impl+0x1af>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    %eax,%edx
mov    0x2d45(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movzwl -0x12(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12f9 <ss_listen_on_ipv6_socket_impl+0x169>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x34(%rbp),%ecx
mov    0x2cc5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffd,-0x4(%rbp)
jmp    133f <ss_listen_on_ipv6_socket_impl+0x1af>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1339 <ss_listen_on_ipv6_socket_impl+0x1a9>
mov    0x2c9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x38(%rbp),%ecx
mov    0x2c85(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffc,-0x4(%rbp)
jmp    133f <ss_listen_on_ipv6_socket_impl+0x1af>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_listen_on_ipv6_socket_impl>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e1d(%rip),%rbx
mov    (%rbx),%edi
mov    %edi,0x10(%rsp)
test   %rbp,%rbp
je     11e5 <ss_listen_on_ipv6_socket_impl+0x55>
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
call   1080 <inet_pton@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
jne    12c8 <ss_listen_on_ipv6_socket_impl+0x138>
mov    (%rbx),%edi
jmp    11f2 <ss_listen_on_ipv6_socket_impl+0x62>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a4 <ss_listen_on_ipv6_socket_impl+0x114>
mov    %eax,%ebp
mov    0x2db4(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d89(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
movl   $0x1,0x14(%rsp)
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movzwl %r15w,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12ab <ss_listen_on_ipv6_socket_impl+0x11b>
mov    %ebp,%edi
mov    %r14d,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c8 <ss_listen_on_ipv6_socket_impl+0x138>
mov    $0xfffffffc,%r15d
jmp    12b1 <ss_listen_on_ipv6_socket_impl+0x121>
mov    $0xfffffffe,%ebp
jmp    12c8 <ss_listen_on_ipv6_socket_impl+0x138>
mov    $0xfffffffd,%r15d
mov    0x2cf8(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,(%r14)
mov    %r15d,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@in6addr_any = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @ss_listen_on_ipv6_socket_impl(i8* %0, i16 zeroext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i32
%8 = alloca %struct.sockaddr_in6
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i16 %1, i16* %6
store i32 %2, i32* %7
%13 = call i32 @memset(%struct.sockaddr_in6* %8, i32 0, i32 12)
%14 = load i32, i32* @AF_INET6
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %26
18: ; preds = %3
%19 = load i32, i32* @AF_INET6
%20 = load i8*, i8** %5
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 1
%22 = call i32 @inet_pton(i32 %19, i8* %20, i32* %21)
%23 = icmp eq i32 %22, 1
br i1 %23, label %25, label %24
24: ; preds = %18
store i32 -1, i32* %4
br label %73
25: ; preds = %18
br label %29
26: ; preds = %3
%27 = load i32, i32* @in6addr_any
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 1
store i32 %27, i32* %28
br label %29
29: ; preds = %26, %25
%30 = load i32, i32* @AF_INET6
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 -2, i32* %4
br label %73
36: ; preds = %29
%37 = load i32, i32* %9
%38 = load i32, i32* @F_SETFD
%39 = load i32, i32* %9
%40 = load i32, i32* @F_GETFD
%41 = call i32 (i32, i32, ...) @fcntl(i32 %39, i32 %40)
%42 = load i32, i32* @FD_CLOEXEC
%43 = or i32 %41, %42
%44 = call i32 (i32, i32, ...) @fcntl(i32 %37, i32 %38, i32 %43)
store i32 1, i32* %10
%45 = load i32, i32* %9
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_REUSEADDR
%48 = call i32 @setsockopt(i32 %45, i32 %46, i32 %47, i32* %10, i32 4)
%49 = load i16, i16* %6
%50 = call i32 @htons(i16 zeroext %49)
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %9
%53 = bitcast %struct.sockaddr_in6* %8 to %struct.sockaddr*
%54 = call i32 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %61
56: ; preds = %36
%57 = load i32, i32* @errno
store i32 %57, i32* %11
%58 = load i32, i32* %9
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %11
store i32 %60, i32* @errno
store i32 -3, i32* %4
br label %73
61: ; preds = %36
%62 = load i32, i32* %9
%63 = load i32, i32* %7
%64 = call i32 @listen(i32 %62, i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %71
66: ; preds = %61
%67 = load i32, i32* @errno
store i32 %67, i32* %12
%68 = load i32, i32* %9
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %12
store i32 %70, i32* @errno
store i32 -4, i32* %4
br label %73
71: ; preds = %61
%72 = load i32, i32* %9
store i32 %72, i32* %4
br label %73
73: ; preds = %71, %66, %56, %35, %24
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@in6addr_any = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @ss_listen_on_ipv6_socket_impl(i8* %0, i16 zeroext %1, i32 %2) {
%4 = alloca %struct.sockaddr_in6
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in6* %4 to i8*
%7 = call i32 @memset(%struct.sockaddr_in6* %4, i32 0, i32 12)
%8 = load i32, i32* @AF_INET6
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 2
store i32 %8, i32* %9
%10 = icmp eq i8* %0, null
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 1
%13 = call i32 @inet_pton(i32 %8, i8* %0, i32* %12)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %50
15: ; preds = %11
%16 = load i32, i32* @AF_INET6
br label %20
17: ; preds = %3
%18 = load i32, i32* @in6addr_any
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 1
store i32 %18, i32* %19
br label %20
20: ; preds = %15, %17
%21 = phi i32 [ %16, %15 ], [ %8, %17 ]
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %50, label %25
25: ; preds = %20
%26 = load i32, i32* @F_SETFD
%27 = load i32, i32* @F_GETFD
%28 = call i32 (i32, i32, ...) @fcntl(i32 %23, i32 %27)
%29 = load i32, i32* @FD_CLOEXEC
%30 = or i32 %29, %28
%31 = call i32 (i32, i32, ...) @fcntl(i32 %23, i32 %26, i32 %30)
%32 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i32 @setsockopt(i32 %23, i32 %33, i32 %34, i32* %5, i32 4)
%36 = call i32 @htons(i16 zeroext %1)
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 0
store i32 %36, i32* %37
%38 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%39 = call i32 @bind(i32 %23, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %44, label %41
41: ; preds = %25
%42 = call i32 @listen(i32 %23, i32 %2)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %41, %25
%45 = phi i32 [ -3, %25 ], [ -4, %41 ]
%46 = load i32, i32* @errno
%47 = call i32 @close(i32 %23)
store i32 %46, i32* @errno
br label %48
48: ; preds = %44, %41
%49 = phi i32 [ %23, %41 ], [ %45, %44 ]
br label %50
50: ; preds = %48, %20, %11
%51 = phi i32 [ -1, %11 ], [ %49, %48 ], [ -2, %20 ]
ret i32 %51
}