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

/* 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*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ in6addr_any;
int listen(int, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_socket_server(unsigned short port) {
  struct sockaddr_in6 addr;
  socklen_t no = 0, yes = 1;
  int fd, r;
  fd = socket(AF_INET6, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  r = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  if (r != 0) {
    close(fd);
    return -1;
  }
  r = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no));
  if (r != 0) {
    close(fd);
    return -1;
  }
  bzero((char *)&addr, sizeof(addr));
  addr.sin6_family = AF_INET6;
  addr.sin6_addr = in6addr_any;
  addr.sin6_port = htons(port);
  r = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (r != 0) {
    close(fd);
    return -1;
  }
  r = listen(fd, 1024);
  if (r != 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<setup_socket_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11bf <setup_socket_server+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <setup_socket_server+0x171>
mov    -0x24(%rbp),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1204 <setup_socket_server+0x94>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <setup_socket_server+0x171>
mov    -0x24(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1249 <setup_socket_server+0xd9>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <setup_socket_server+0x171>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movzwl -0x6(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12ad <setup_socket_server+0x13d>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <setup_socket_server+0x171>
mov    -0x24(%rbp),%edi
mov    $0x400,%esi
call   1060 <listen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12db <setup_socket_server+0x16b>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e1 <setup_socket_server+0x171>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_socket_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
movl   $0x0,0xc(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2e42(%rip),%r12
mov    (%r12),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1267 <setup_socket_server+0xf7>
mov    %eax,%ebp
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    1260 <setup_socket_server+0xf0>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    1260 <setup_socket_server+0xf0>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movzwl %r14w,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jne    1260 <setup_socket_server+0xf0>
mov    %ebp,%edi
mov    $0x400,%esi
call   1060 <listen@plt>
test   %eax,%eax
je     1276 <setup_socket_server+0x106>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%ebx
jmp    1267 <setup_socket_server+0xf7>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, 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
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@in6addr_any = global i32 0
define i32 @setup_socket_server(i16 zeroext %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca %struct.sockaddr_in6
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i16 %0, i16* %3
store i32 0, i32* %5
store i32 1, i32* %6
%9 = load i32, i32* @AF_INET6
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %63
15: ; preds = %1
%16 = load i32, i32* %7
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %6, i32 4)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %2
br label %63
25: ; preds = %15
%26 = load i32, i32* %7
%27 = load i32, i32* @IPPROTO_IPV6
%28 = load i32, i32* @IPV6_V6ONLY
%29 = call i32 @setsockopt(i32 %26, i32 %27, i32 %28, i32* %5, i32 4)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %2
br label %63
35: ; preds = %25
%36 = bitcast %struct.sockaddr_in6* %4 to i8*
%37 = call i32 @bzero(i8* %36, i32 12)
%38 = load i32, i32* @AF_INET6
%39 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 2
store i32 %38, i32* %39
%40 = load i32, i32* @in6addr_any
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 1
store i32 %40, i32* %41
%42 = load i16, i16* %3
%43 = call i32 @htons(i16 zeroext %42)
%44 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* %7
%46 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%47 = call i32 @bind(i32 %45, %struct.sockaddr* %46, i32 12)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %35
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %2
br label %63
53: ; preds = %35
%54 = load i32, i32* %7
%55 = call i32 @listen(i32 %54, i32 1024)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %2
br label %63
61: ; preds = %53
%62 = load i32, i32* %7
store i32 %62, i32* %2
br label %63
63: ; preds = %61, %58, %50, %32, %22, %14
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, 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
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@in6addr_any = global i32 0
define i32 @setup_socket_server(i16 zeroext %0) {
%2 = alloca %struct.sockaddr_in6
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in6* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%8 = load i32, i32* @AF_INET6
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %38, label %12
12: ; preds = %1
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %10, i32 %13, i32 %14, i32* %4, i32 4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %36
17: ; preds = %12
%18 = load i32, i32* @IPPROTO_IPV6
%19 = load i32, i32* @IPV6_V6ONLY
%20 = call i32 @setsockopt(i32 %10, i32 %18, i32 %19, i32* %3, i32 4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = call i32 @bzero(i8* %5, i32 12)
%24 = load i32, i32* @AF_INET6
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @in6addr_any
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @htons(i16 zeroext %0)
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%31 = call i32 @bind(i32 %10, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %22
%34 = call i32 @listen(i32 %10, i32 1024)
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33, %22, %17, %12
%37 = call i32 @close(i32 %10)
br label %38
38: ; preds = %36, %33, %1
%39 = phi i32 [ -1, %1 ], [ %10, %33 ], [ -1, %36 ]
ret i32 %39
}