#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*/ srv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
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 create_socket6(int port) {
  int sockfd;
  struct sockaddr_in6 srv_addr;
  int opt_reuseadddr;
  srv_addr.sin6_family = AF_INET6;
  srv_addr.sin6_addr = in6addr_any;
  srv_addr.sin6_port = htons(port);
  sockfd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  if (sockfd == -1) {
    perror("socket6 error");
    return -1;
  }
  opt_reuseadddr = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt_reuseadddr,
                 sizeof(int)) == -1) {
    perror("Can not set SO_REUSEADDR option6");
    return -1;
  }
  if (bind(sockfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr)) == -1) {
    perror("bind socket6 error");
    return -1;
  }
  if (listen(sockfd, 10) == -1) {
    perror("listen socket6 error");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<create_socket6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d3 <create_socket6+0x73>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <create_socket6+0x125>
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    121f <create_socket6+0xbf>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <create_socket6+0x125>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1251 <create_socket6+0xf1>
lea    0xde1(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <create_socket6+0x125>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    127f <create_socket6+0x11f>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <create_socket6+0x125>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket6>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e64(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1209 <create_socket6+0xa9>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1212 <create_socket6+0xb2>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     121b <create_socket6+0xbb>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    122c <create_socket6+0xcc>
lea    0xe2d(%rip),%rdi
jmp    1222 <create_socket6+0xc2>
lea    0xe16(%rip),%rdi
jmp    1222 <create_socket6+0xc2>
lea    0xde7(%rip),%rdi
jmp    1222 <create_socket6+0xc2>
lea    0xdff(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
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
@IPPROTO_TCP = global i32 0
@.str = [14 x i8] c"socket6 error\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [33 x i8] c"Can not set SO_REUSEADDR option6\00"
@.str.2 = [19 x i8] c"bind socket6 error\00"
@.str.3 = [21 x i8] c"listen socket6 error\00"
define i32 @create_socket6(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in6
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @AF_INET6
%8 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @in6addr_any
%10 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* %3
%12 = call i32 @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET6
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @IPPROTO_TCP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
22: ; preds = %1
store i32 1, i32* %6
%23 = load i32, i32* %4
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %6, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
30: ; preds = %22
%31 = load i32, i32* %4
%32 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
37: ; preds = %30
%38 = load i32, i32* %4
%39 = call i32 @listen(i32 %38, i32 10)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
43: ; preds = %37
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %41, %35, %28, %20
%46 = load i32, i32* %2
ret i32 %46
}

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
@IPPROTO_TCP = global i32 0
@.str = [14 x i8] c"socket6 error\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [33 x i8] c"Can not set SO_REUSEADDR option6\00"
@.str.2 = [19 x i8] c"bind socket6 error\00"
@.str.3 = [21 x i8] c"listen socket6 error\00"
define i32 @create_socket6(i32 %0) {
%2 = alloca %struct.sockaddr_in6
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in6* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @AF_INET6
%7 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @in6addr_any
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 %0)
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET6
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = tail call i32 @socket(i32 %12, i32 %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %37
19: ; preds = %1
store i32 1, i32* %3
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %15, i32 %20, i32 %21, i32* %3, i32 4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %37
26: ; preds = %19
%27 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%28 = call i32 @bind(i32 %15, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %37
32: ; preds = %26
%33 = call i32 @listen(i32 %15, i32 10)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %32, %35, %30, %24, %17
%38 = phi i32 [ -1, %17 ], [ -1, %24 ], [ -1, %30 ], [ -1, %35 ], [ %15, %32 ]
ret i32 %38
}