#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*/ sin6;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ PF_INET6;
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);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_server_socket(int port, int local) {
  struct sockaddr_in6 sin6;
  int s, rc, saved_errno;
  int one = 1;
  s = socket(PF_INET6, SOCK_STREAM, 0);
  if (s < 0)
    return -1;
  rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  if (rc < 0)
    goto fail;
  rc = fcntl(s, F_GETFL, 0);
  if (rc < 0)
    goto fail;
  rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
  if (rc < 0)
    goto fail;
  rc = fcntl(s, F_GETFD, 0);
  if (rc < 0)
    goto fail;
  rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
  if (rc < 0)
    goto fail;
  memset(&sin6, 0, sizeof(sin6));
  sin6.sin6_family = AF_INET6;
  sin6.sin6_port = htons(port);
  if (local) {
    rc = inet_pton(AF_INET6, "::1", &sin6.sin6_addr);
    if (rc < 0)
      goto fail;
  }
  rc = bind(s, (struct sockaddr *)&sin6, sizeof(sin6));
  if (rc < 0)
    goto fail;
  rc = listen(s, 2);
  if (rc < 0)
    goto fail;
  return s;
fail:
  saved_errno = errno;
  close(s);
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<tcp_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x28(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d7 <tcp_server_socket+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <tcp_server_socket+0x1fa>
mov    -0x1c(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    120d <tcp_server_socket+0x7d>
jmp    1363 <tcp_server_socket+0x1d3>
mov    -0x1c(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1232 <tcp_server_socket+0xa2>
jmp    1363 <tcp_server_socket+0x1d3>
mov    -0x1c(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2d90(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1261 <tcp_server_socket+0xd1>
jmp    1363 <tcp_server_socket+0x1d3>
mov    -0x1c(%rbp),%edi
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1286 <tcp_server_socket+0xf6>
jmp    1363 <tcp_server_socket+0x1d3>
mov    -0x1c(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2d0c(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12b5 <tcp_server_socket+0x125>
jmp    1363 <tcp_server_socket+0x1d3>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1316 <tcp_server_socket+0x186>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
lea    0xd06(%rip),%rsi
call   1080 <inet_pton@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1311 <tcp_server_socket+0x181>
jmp    1363 <tcp_server_socket+0x1d3>
jmp    1316 <tcp_server_socket+0x186>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1339 <tcp_server_socket+0x1a9>
jmp    1363 <tcp_server_socket+0x1d3>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1090 <listen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1358 <tcp_server_socket+0x1c8>
jmp    1363 <tcp_server_socket+0x1d3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    138a <tcp_server_socket+0x1fa>
mov    0x2c36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c1f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
movl   $0x1,0x4(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     1302 <tcp_server_socket+0x172>
mov    %eax,%r13d
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
mov    0x2d93(%rip),%rcx
mov    (%rcx),%esi
mov    0x2daa(%rip),%rcx
or     (%rcx),%eax
mov    %r13d,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
mov    0x2d5c(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d3b(%rip),%rcx
or     (%rcx),%eax
mov    %r13d,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d3b(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
test   %r14d,%r14d
je     12c3 <tcp_server_socket+0x133>
mov    (%rbx),%edi
lea    0xd4b(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1080 <inet_pton@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
mov    %r13d,%edi
mov    $0x2,%esi
call   1090 <listen@plt>
test   %eax,%eax
js     12ef <tcp_server_socket+0x15f>
mov    %r13d,%r12d
jmp    1302 <tcp_server_socket+0x172>
mov    0x2caa(%rip),%rbx
mov    (%rbx),%ebp
mov    %r13d,%edi
call   1070 <close@plt>
mov    %ebp,(%rbx)
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@AF_INET6 = global i32 0
@.str = [4 x i8] c"::1\00"
@errno = global i32 0
define i32 @tcp_server_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in6
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 1, i32* %10
%11 = load i32, i32* @PF_INET6
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %96
17: ; preds = %2
%18 = load i32, i32* %7
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %10, i32 4)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
br label %91
25: ; preds = %17
%26 = load i32, i32* %7
%27 = load i32, i32* @F_GETFL
%28 = call i32 @fcntl(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
br label %91
32: ; preds = %25
%33 = load i32, i32* %7
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* %8
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i32 @fcntl(i32 %33, i32 %34, i32 %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %32
br label %91
42: ; preds = %32
%43 = load i32, i32* %7
%44 = load i32, i32* @F_GETFD
%45 = call i32 @fcntl(i32 %43, i32 %44, i32 0)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %42
br label %91
49: ; preds = %42
%50 = load i32, i32* %7
%51 = load i32, i32* @F_SETFD
%52 = load i32, i32* %8
%53 = load i32, i32* @FD_CLOEXEC
%54 = or i32 %52, %53
%55 = call i32 @fcntl(i32 %50, i32 %51, i32 %54)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %49
br label %91
59: ; preds = %49
%60 = call i32 @memset(%struct.sockaddr_in6* %6, i32 0, i32 12)
%61 = load i32, i32* @AF_INET6
%62 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 2
store i32 %61, i32* %62
%63 = load i32, i32* %4
%64 = call i32 @htons(i32 %63)
%65 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 1
store i32 %64, i32* %65
%66 = load i32, i32* %5
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %76
68: ; preds = %59
%69 = load i32, i32* @AF_INET6
%70 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
%71 = call i32 @inet_pton(i32 %69, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %70)
store i32 %71, i32* %8
%72 = load i32, i32* %8
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %68
br label %91
75: ; preds = %68
br label %76
76: ; preds = %75, %59
%77 = load i32, i32* %7
%78 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%79 = call i32 @bind(i32 %77, %struct.sockaddr* %78, i32 12)
store i32 %79, i32* %8
%80 = load i32, i32* %8
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %76
br label %91
83: ; preds = %76
%84 = load i32, i32* %7
%85 = call i32 @listen(i32 %84, i32 2)
store i32 %85, i32* %8
%86 = load i32, i32* %8
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %83
br label %91
89: ; preds = %83
%90 = load i32, i32* %7
store i32 %90, i32* %3
br label %96
91: ; preds = %88, %82, %74, %58, %48, %41, %31, %24
%92 = load i32, i32* @errno
store i32 %92, i32* %9
%93 = load i32, i32* %7
%94 = call i32 @close(i32 %93)
%95 = load i32, i32* %9
store i32 %95, i32* @errno
store i32 -1, i32* %3
br label %96
96: ; preds = %91, %89, %16
%97 = load i32, i32* %3
ret i32 %97
}

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