#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_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int open_listener(int port) {
  int sock = socket(AF_INET6, SOCK_STREAM, 0);
  if (sock == -1) {
    syslog(LOG_NOTICE, "failed to open socket: %s", strerror(errno));
  }
  struct sockaddr_in6 sa;
  memset((void *)&sa, 0, sizeof(sa));
  sa.sin6_family = AF_INET6;
  sa.sin6_port = htons(port);
  if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    syslog(LOG_NOTICE, "failed to bind socket: %s", strerror(errno));
  }
  if (listen(sock, 1) < 0) {
    syslog(LOG_NOTICE, "failed to listen: %s", strerror(errno));
  }
  return sock;
}


x86 assembly (clang, -O0):
<open_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cc <open_listener+0x5c>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xe39(%rip),%rsi
call   1050 <syslog@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1239 <open_listener+0xc9>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xde6(%rip),%rsi
call   1050 <syslog@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    127b <open_listener+0x10b>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xdbe(%rip),%rsi
call   1050 <syslog@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_listener>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r15d
mov    0x2e62(%rip),%r12
mov    (%r12),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11ca <open_listener+0x5a>
mov    0x2e20(%rip),%rax
mov    (%rax),%r14d
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe40(%rip),%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1050 <syslog@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    122d <open_listener+0xbd>
mov    0x2dbb(%rip),%rax
mov    (%rax),%ebp
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf6(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <syslog@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    1265 <open_listener+0xf5>
mov    0x2d83(%rip),%rax
mov    (%rax),%ebp
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdd8(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <syslog@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@LOG_NOTICE = global i32 0
@.str = [26 x i8] c"failed to open socket: %s\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"failed to bind socket: %s\00"
@.str.2 = [21 x i8] c"failed to listen: %s\00"
define i32 @open_listener(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in6
store i32 %0, i32* %2
%5 = load i32, i32* @AF_INET6
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @LOG_NOTICE
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @syslog(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %10, %1
%16 = bitcast %struct.sockaddr_in6* %4 to i8*
%17 = call i32 @memset(i8* %16, i32 0, i32 8)
%18 = load i32, i32* @AF_INET6
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %2
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %3
%24 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %15
%28 = load i32, i32* @LOG_NOTICE
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @syslog(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %27, %15
%33 = load i32, i32* %3
%34 = call i64 @listen(i32 %33, i32 1)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @LOG_NOTICE
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 @syslog(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %36, %32
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@LOG_NOTICE = global i32 0
@.str = [26 x i8] c"failed to open socket: %s\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"failed to bind socket: %s\00"
@.str.2 = [21 x i8] c"failed to listen: %s\00"
define i32 @open_listener(i32 %0) {
%2 = alloca %struct.sockaddr_in6
%3 = load i32, i32* @AF_INET6
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @LOG_NOTICE
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @syslog(i32 %8, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %7, %1
%13 = bitcast %struct.sockaddr_in6* %2 to i8*
%14 = call i32 @memset(i8* %13, i32 0, i32 8)
%15 = load i32, i32* @AF_INET6
%16 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @htons(i32 %0)
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%20 = call i64 @bind(i32 %5, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %12
%23 = load i32, i32* @LOG_NOTICE
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @syslog(i32 %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %22, %12
%28 = call i64 @listen(i32 %5, i32 1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @LOG_NOTICE
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 @syslog(i32 %31, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %30, %27
ret i32 %5
}