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

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int const, struct sockaddr const *, int);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ in6addr_any;
int listen(int const, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_socket(const int port) {
  const int sock = socket(AF_INET6, SOCK_STREAM, 0);
  struct sockaddr_in6 addr = {
      .sin6_family = AF_INET6,
      .sin6_port = htons(port),
      .sin6_addr = in6addr_any,
  };
  if (sock < 0)
    return -1;
  if (bind(sock, (const struct sockaddr *)&addr, sizeof(addr)) < 0)
    return -1;
  if (listen(sock, 5) == -1)
    return -1;
  return sock;
}


x86 assembly (clang, -O0):
<tcp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a0 <tcp_socket+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <tcp_socket+0xaf>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11c7 <tcp_socket+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <tcp_socket+0xaf>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
jne    11e9 <tcp_socket+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <tcp_socket+0xaf>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e8f(%rip),%r14
mov    (%r14),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    $0xffffffff,%ebp
test   %ebx,%ebx
js     11b5 <tcp_socket+0x75>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11b5 <tcp_socket+0x75>
mov    %ebx,%edi
mov    $0x5,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%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
@in6addr_any = global i32 0
define i32 @tcp_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in6
store i32 %0, i32* %3
%6 = load i32, i32* @AF_INET6
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
%10 = load i32, i32* @in6addr_any
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
%12 = load i32, i32* %3
%13 = call i32 @htons(i32 %12)
store i32 %13, i32* %11
%14 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 2
%15 = load i32, i32* @AF_INET6
store i32 %15, i32* %14
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 -1, i32* %2
br label %32
19: ; preds = %1
%20 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %2
br label %32
25: ; preds = %19
%26 = load i32, i32* %4
%27 = call i32 @listen(i32 %26, i32 5)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %2
br label %32
30: ; preds = %25
%31 = load i32, i32* %4
store i32 %31, i32* %2
br label %32
32: ; preds = %30, %29, %24, %18
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@in6addr_any = global i32 0
define i32 @tcp_socket(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 = bitcast %struct.sockaddr_in6* %2 to i8*
%7 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
%8 = load i32, i32* @in6addr_any
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
%10 = tail call i32 @htons(i32 %0)
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
%12 = load i32, i32* @AF_INET6
store i32 %12, i32* %11
%13 = icmp slt i32 %5, 0
br i1 %13, label %22, label %14
14: ; preds = %1
%15 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%16 = call i64 @bind(i32 %5, %struct.sockaddr* %15, i32 12)
%17 = icmp slt i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = call i32 @listen(i32 %5, i32 5)
%20 = icmp eq i32 %19, -1
%21 = select i1 %20, i32 -1, i32 %5
br label %22
22: ; preds = %18, %14, %1
%23 = phi i32 [ -1, %1 ], [ -1, %14 ], [ %21, %18 ]
ret i32 %23
}