#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;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ one;
typedef int /*<<< orphan*/ 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 /*<<< orphan*/ TCP_NODELAY;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ set_blocking(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(int listen_port) {
  int s = socket(AF_INET6, SOCK_STREAM, 0);
  if (s < 0) {
    perror("socket");
    return s;
  }
  if (set_blocking(s, false) < 0) {
    close(s);
    return -1;
  }
  if (listen_port) {
    struct sockaddr_in6 addr = {0};
    addr.sin6_family = AF_INET6;
    addr.sin6_port = htons(listen_port);
    int one = 1;
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
      perror("setsockopt(SO_REUSEADDR)");
      close(s);
      return -1;
    }
    if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0) {
      perror("setsockopt(TCP_NODELAY)");
      close(s);
      return -1;
    }
    if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
      perror("bind");
      close(s);
      return -1;
    }
    if (listen(s, 1024) < 0) {
      perror("listen");
      close(s);
      return -1;
    }
  }
  return s;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d8 <create_socket+0x48>
lea    0xe38(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1351 <create_socket+0x1c1>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1050 <set_blocking@plt>
cmp    $0x0,%rax
jge    1200 <create_socket+0x70>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <create_socket+0x1c1>
cmpl   $0x0,-0x8(%rbp)
je     134b <create_socket+0x1bb>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1286 <create_socket+0xf6>
lea    0xd9a(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <create_socket+0x1c1>
mov    -0xc(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12d4 <create_socket+0x144>
lea    0xd65(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <create_socket+0x1c1>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    130f <create_socket+0x17f>
lea    0xd42(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <create_socket+0x1c1>
mov    -0xc(%rbp),%edi
mov    $0x400,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    1346 <create_socket+0x1b6>
lea    0xd10(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1351 <create_socket+0x1c1>
jmp    134b <create_socket+0x1bb>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    0x2e3f(%rip),%r14
mov    (%r14),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     126c <create_socket+0xec>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <set_blocking@plt>
test   %rax,%rax
js     1298 <create_socket+0x118>
test   %ebp,%ebp
je     12a4 <create_socket+0x124>
movl   $0x0,0x18(%rsp)
mov    (%r14),%eax
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     127a <create_socket+0xfa>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1283 <create_socket+0x103>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     128c <create_socket+0x10c>
mov    %ebx,%edi
mov    $0x400,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    12a4 <create_socket+0x124>
lea    0xdd3(%rip),%rdi
jmp    1293 <create_socket+0x113>
lea    0xd8d(%rip),%rdi
call   1090 <perror@plt>
jmp    12a4 <create_socket+0x124>
lea    0xd86(%rip),%rdi
jmp    1293 <create_socket+0x113>
lea    0xd96(%rip),%rdi
jmp    1293 <create_socket+0x113>
lea    0xda5(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
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 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [25 x i8] c"setsockopt(SO_REUSEADDR)\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.2 = [24 x i8] c"setsockopt(TCP_NODELAY)\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @create_socket(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 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %4
store i32 %14, i32* %2
br label %71
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i64 @set_blocking(i32 %16, i32 0)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %2
br label %71
22: ; preds = %15
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %69
25: ; preds = %22
%26 = bitcast %struct.sockaddr_in6* %5 to i8*
%27 = load i32, i32* @AF_INET6
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %3
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
store i32 %30, i32* %31
store i32 1, i32* %6
%32 = load i32, i32* %4
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = call i64 @setsockopt(i32 %32, i32 %33, i32 %34, i32* %6, i32 4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %25
%38 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %2
br label %71
41: ; preds = %25
%42 = load i32, i32* %4
%43 = load i32, i32* @IPPROTO_TCP
%44 = load i32, i32* @TCP_NODELAY
%45 = call i64 @setsockopt(i32 %42, i32 %43, i32 %44, i32* %6, i32 4)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %2
br label %71
51: ; preds = %41
%52 = load i32, i32* %4
%53 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%54 = call i64 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* %4
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %2
br label %71
60: ; preds = %51
%61 = load i32, i32* %4
%62 = call i64 @listen(i32 %61, i32 1024)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%66 = load i32, i32* %4
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %2
br label %71
68: ; preds = %60
br label %69
69: ; preds = %68, %22
%70 = load i32, i32* %4
store i32 %70, i32* %2
br label %71
71: ; preds = %69, %64, %56, %47, %37, %19, %12
%72 = load i32, i32* %2
ret i32 %72
}

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 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [25 x i8] c"setsockopt(SO_REUSEADDR)\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.2 = [24 x i8] c"setsockopt(TCP_NODELAY)\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
define i32 @create_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in6
%3 = alloca i32
%4 = load i32, i32* @AF_INET6
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %54
10: ; preds = %1
%11 = tail call i64 @set_blocking(i32 %6, i32 0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @close(i32 %6)
br label %54
15: ; preds = %10
%16 = icmp eq i32 %0, 0
br i1 %16, label %54, label %17
17: ; preds = %15
%18 = bitcast %struct.sockaddr_in6* %2 to i8*
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
store i32 0, i32* %19
%20 = load i32, i32* @AF_INET6
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 %0)
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i64 @setsockopt(i32 %6, i32 %25, i32 %26, i32* %3, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @close(i32 %6)
br label %54
32: ; preds = %17
%33 = load i32, i32* @IPPROTO_TCP
%34 = load i32, i32* @TCP_NODELAY
%35 = call i64 @setsockopt(i32 %6, i32 %33, i32 %34, i32* %3, i32 4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @close(i32 %6)
br label %54
40: ; preds = %32
%41 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%42 = call i64 @bind(i32 %6, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @close(i32 %6)
br label %54
47: ; preds = %40
%48 = call i64 @listen(i32 %6, i32 1024)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @close(i32 %6)
br label %54
53: ; preds = %47
br label %54
54: ; preds = %15, %53, %29, %37, %44, %50, %13, %8
%55 = phi i32 [ %6, %8 ], [ -1, %13 ], [ -1, %50 ], [ -1, %44 ], [ -1, %37 ], [ -1, %29 ], [ %6, %53 ], [ %6, %15 ]
ret i32 %55
}