#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_addr;
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ UNLLog(int, char *, ...);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ in6addr_any;
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int ts_listen(int port, int *server_socket) {
  int rc = -1;
  int y = 1;
  struct sockaddr_in6 server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  *server_socket = socket(AF_INET6, SOCK_STREAM, 0);
  if (*server_socket < 0) {
    rc = 1;
    UNLLog(1, "Error while setting TS (socket).  ERR: %s (%i).\n",
           strerror(errno), rc);
    return rc;
  }
  server_addr.sin6_family = AF_INET6;
  server_addr.sin6_port = htons(port);
  server_addr.sin6_addr = in6addr_any;
  if (setsockopt(*server_socket, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(int)) <
      0) {
    rc = 2;
    UNLLog(1, "Error while setting TS (setsockopt). ERR: %s (%i).\n",
           strerror(errno), rc);
    return rc;
  }
  if (bind(*server_socket, (struct sockaddr *)&server_addr,
           sizeof(server_addr)) < 0) {
    rc = 3;
    UNLLog(1, "Error while setting TS (binding). ERR: %s (%i).\n",
           strerror(errno), rc);
    return rc;
  }
  if (listen(*server_socket, 5) < 0) {
    rc = 4;
    UNLLog(1, "Error while setting TS (listening). ERR: %s (%i).\n",
           strerror(errno), rc);
    return rc;
  }
  UNLLog(3, "TS configured.\n");
  return 0;
}


x86 assembly (clang, -O0):
<ts_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1213 <ts_listen+0x93>
movl   $0x1,-0x14(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
mov    $0x1,%edi
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1060 <UNLLog@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1361 <ts_listen+0x1e1>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    129f <ts_listen+0x11f>
movl   $0x2,-0x14(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
mov    $0x1,%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1060 <UNLLog@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1361 <ts_listen+0x1e1>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12f5 <ts_listen+0x175>
movl   $0x3,-0x14(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
mov    $0x1,%edi
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1060 <UNLLog@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1361 <ts_listen+0x1e1>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1347 <ts_listen+0x1c7>
movl   $0x4,-0x14(%rbp)
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
mov    $0x1,%edi
lea    0xd61(%rip),%rsi
mov    $0x0,%al
call   1060 <UNLLog@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1361 <ts_listen+0x1e1>
mov    $0x3,%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1060 <UNLLog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ts_listen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e2a(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     1259 <ts_listen+0xd9>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1281 <ts_listen+0x101>
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     12a9 <ts_listen+0x129>
mov    (%rbx),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     12d1 <ts_listen+0x151>
lea    0xe83(%rip),%rsi
xor    %ebx,%ebx
mov    $0x3,%edi
xor    %eax,%eax
call   1060 <UNLLog@plt>
jmp    12fe <ts_listen+0x17e>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd92(%rip),%rsi
mov    $0x1,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x1,%ecx
jmp    12f7 <ts_listen+0x177>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd9b(%rip),%rsi
mov    $0x2,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x2,%ecx
jmp    12f7 <ts_listen+0x177>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda7(%rip),%rsi
mov    $0x3,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x3,%ecx
jmp    12f7 <ts_listen+0x177>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb0(%rip),%rsi
mov    $0x4,%ebx
mov    $0x1,%edi
mov    %eax,%edx
mov    $0x4,%ecx
xor    %eax,%eax
call   1060 <UNLLog@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [49 x i8] c"Error while setting TS (socket). ERR: %s (%i).\0A\00"
@errno = global i32 0
@in6addr_any = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [52 x i8] c"Error while setting TS (setsockopt). ERR: %s (%i).\0A\00"
@.str.2 = [49 x i8] c"Error while setting TS (binding). ERR: %s (%i).\0A\00"
@.str.3 = [51 x i8] c"Error while setting TS (listening). ERR: %s (%i).\0A\00"
@.str.4 = [16 x i8] c"TS configured.\0A\00"
define i32 @ts_listen(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in6
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 -1, i32* %6
store i32 1, i32* %7
%9 = call i32 @memset(%struct.sockaddr_in6* %8, i32 0, i32 12)
%10 = load i32, i32* @AF_INET6
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = load i32*, i32** %5
store i32 %12, i32* %13
%14 = load i32*, i32** %5
%15 = load i32, i32* %14
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %2
store i32 1, i32* %6
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = load i32, i32* %6
%21 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %6
store i32 %22, i32* %3
br label %68
23: ; preds = %2
%24 = load i32, i32* @AF_INET6
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* %4
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @in6addr_any
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32*, i32** %5
%32 = load i32, i32* %31
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i64 @setsockopt(i32 %32, i32 %33, i32 %34, i32* %7, i32 4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %23
store i32 2, i32* %6
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = load i32, i32* %6
%41 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %39, i32 %40)
%42 = load i32, i32* %6
store i32 %42, i32* %3
br label %68
43: ; preds = %23
%44 = load i32*, i32** %5
%45 = load i32, i32* %44
%46 = bitcast %struct.sockaddr_in6* %8 to %struct.sockaddr*
%47 = call i64 @bind(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %43
store i32 3, i32* %6
%50 = load i32, i32* @errno
%51 = call i32 @strerror(i32 %50)
%52 = load i32, i32* %6
%53 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %51, i32 %52)
%54 = load i32, i32* %6
store i32 %54, i32* %3
br label %68
55: ; preds = %43
%56 = load i32*, i32** %5
%57 = load i32, i32* %56
%58 = call i64 @listen(i32 %57, i32 5)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %55
store i32 4, i32* %6
%61 = load i32, i32* @errno
%62 = call i32 @strerror(i32 %61)
%63 = load i32, i32* %6
%64 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %62, i32 %63)
%65 = load i32, i32* %6
store i32 %65, i32* %3
br label %68
66: ; preds = %55
%67 = call i32 (i32, i8*, ...) @UNLLog(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %68
68: ; preds = %66, %60, %49, %37, %17
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [49 x i8] c"Error while setting TS (socket). ERR: %s (%i).\0A\00"
@errno = global i32 0
@in6addr_any = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [52 x i8] c"Error while setting TS (setsockopt). ERR: %s (%i).\0A\00"
@.str.2 = [49 x i8] c"Error while setting TS (binding). ERR: %s (%i).\0A\00"
@.str.3 = [51 x i8] c"Error while setting TS (listening). ERR: %s (%i).\0A\00"
@.str.4 = [16 x i8] c"TS configured.\0A\00"
define i32 @ts_listen(i32 %0, i32* nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in6
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%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 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %1
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %14, i32 1)
br label %51
16: ; preds = %2
%17 = load i32, i32* @AF_INET6
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 %0)
%20 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @in6addr_any
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %1
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i64 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %3, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 2)
br label %51
32: ; preds = %16
%33 = load i32, i32* %1
%34 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 3)
br label %51
41: ; preds = %32
%42 = load i32, i32* %1
%43 = call i64 @listen(i32 %42, i32 5)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @UNLLog(i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i32 %47, i32 4)
br label %51
49: ; preds = %41
%50 = call i32 (i32, i8*, ...) @UNLLog(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %45, %37, %28, %12
%52 = phi i32 [ 1, %12 ], [ 2, %28 ], [ 3, %37 ], [ 4, %45 ], [ 0, %49 ]
ret i32 %52
}