#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 */
typedef int /*<<< orphan*/ val;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int AF_INET6;
int AF_UNIX;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_V6ONLY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_REUSEPORT;
scalar_t__ bind(int, void *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int gen_address(int, char *, char const *, int, int *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ log_printf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int create_socket(const char *bindaddr, int bindport, int netproto) {
  int val, fd, sinsize, subproto;
  char buffer[256];
  memset(buffer, 0, sizeof(buffer));
  sinsize = gen_address(netproto, buffer, bindaddr, bindport, &subproto);
  if (sinsize < 0)
    return -1;
  if (netproto == AF_UNIX)
    unlink(bindaddr);
  fd = socket(netproto, SOCK_STREAM, subproto);
  if (fd < 0)
    goto fail;
  val = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  val = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  if (netproto == AF_INET6) {
    val = 1;
    setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
  }
  if (bind(fd, (void *)buffer, sinsize) != 0)
    goto fail;
  if (listen(fd, 10) != 0)
    goto fail;
  return fd;
fail:
  log_printf(0, "create_socket: %s", strerror(errno));
  if (fd >= 0)
    close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    -0x28(%rbp),%r8
call   1090 <gen_address@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11fb <create_socket+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <create_socket+0x1ae>
mov    -0x18(%rbp),%eax
mov    0x2ddb(%rip),%rcx
cmp    (%rcx),%eax
jne    1216 <create_socket+0x76>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
call   10c0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    123c <create_socket+0x9c>
jmp    1317 <create_socket+0x177>
movl   $0x1,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
mov    0x2d57(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x18(%rbp),%eax
mov    0x2d34(%rip),%rcx
cmp    (%rcx),%eax
jne    12cf <create_socket+0x12f>
movl   $0x1,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x20(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12f0 <create_socket+0x150>
jmp    1317 <create_socket+0x177>
mov    -0x20(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     130c <create_socket+0x16c>
jmp    1317 <create_socket+0x177>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    134e <create_socket+0x1ae>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%edx
xor    %edi,%edi
lea    0xcd0(%rip),%rsi
call   1070 <log_printf@plt>
cmpl   $0x0,-0x20(%rbp)
jl     1347 <create_socket+0x1a7>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edx,%r12d
mov    %esi,%ebx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    0xc(%rsp),%r8
mov    %r12d,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %ebx,%ecx
call   1090 <gen_address@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     12fe <create_socket+0x15e>
mov    %eax,%r15d
mov    0x2de6(%rip),%rax
cmp    %r12d,(%rax)
jne    1207 <create_socket+0x67>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    0xc(%rsp),%edx
mov    %r12d,%edi
call   10c0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12d5 <create_socket+0x135>
movl   $0x1,0x8(%rsp)
mov    0x2db3(%rip),%rbx
mov    (%rbx),%esi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%r13
mov    %ebp,%edi
mov    %r13,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
movl   $0x1,0x8(%rsp)
mov    (%rbx),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r13,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2d51(%rip),%rax
cmp    %r12d,(%rax)
jne    12b0 <create_socket+0x110>
movl   $0x1,0x8(%rsp)
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %r15d,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    12d5 <create_socket+0x135>
mov    %ebp,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     1313 <create_socket+0x173>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd16(%rip),%rsi
xor    %edi,%edi
mov    %eax,%edx
call   1070 <log_printf@plt>
test   %ebp,%ebp
js     12fe <create_socket+0x15e>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%r14d
jmp    12fe <create_socket+0x15e>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@AF_INET6 = global i32 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str = [18 x i8] c"create_socket: %s\00"
@errno = global i32 0
define i32 @create_socket(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [256 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 256)
%15 = load i32, i32* %7
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call i32 @gen_address(i32 %15, i8* %16, i8* %17, i32 %18, i32* %11)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %3
store i32 -1, i32* %4
br label %79
23: ; preds = %3
%24 = load i32, i32* %7
%25 = load i32, i32* @AF_UNIX
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = call i32 @unlink(i8* %28)
br label %30
30: ; preds = %27, %23
%31 = load i32, i32* %7
%32 = load i32, i32* @SOCK_STREAM
%33 = load i32, i32* %11
%34 = call i32 @socket(i32 %31, i32 %32, i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %69
38: ; preds = %30
store i32 1, i32* %8
%39 = load i32, i32* %9
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = call i32 @setsockopt(i32 %39, i32 %40, i32 %41, i32* %8, i32 4)
store i32 1, i32* %8
%43 = load i32, i32* %9
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEPORT
%46 = call i32 @setsockopt(i32 %43, i32 %44, i32 %45, i32* %8, i32 4)
%47 = load i32, i32* %7
%48 = load i32, i32* @AF_INET6
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %38
store i32 1, i32* %8
%51 = load i32, i32* %9
%52 = load i32, i32* @IPPROTO_IPV6
%53 = load i32, i32* @IPV6_V6ONLY
%54 = call i32 @setsockopt(i32 %51, i32 %52, i32 %53, i32* %8, i32 4)
br label %55
55: ; preds = %50, %38
%56 = load i32, i32* %9
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%58 = load i32, i32* %10
%59 = call i64 @bind(i32 %56, i8* %57, i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
br label %69
62: ; preds = %55
%63 = load i32, i32* %9
%64 = call i64 @listen(i32 %63, i32 10)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %62
br label %69
67: ; preds = %62
%68 = load i32, i32* %9
store i32 %68, i32* %4
br label %79
69: ; preds = %66, %61, %37
%70 = load i32, i32* @errno
%71 = call i32 @strerror(i32 %70)
%72 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %9
%74 = icmp sge i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %69
%76 = load i32, i32* %9
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %69
store i32 -1, i32* %4
br label %79
79: ; preds = %78, %67, %22
%80 = load i32, i32* %4
ret i32 %80
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@AF_INET6 = global i32 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str = [18 x i8] c"create_socket: %s\00"
@errno = global i32 0
define i32 @create_socket(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 256)
%11 = call i32 @gen_address(i32 %2, i8* %9, i8* %0, i32 %1, i32* %5)
%12 = icmp slt i32 %11, 0
br i1 %12, label %49, label %13
13: ; preds = %3
%14 = load i32, i32* @AF_UNIX
%15 = icmp eq i32 %14, %2
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @unlink(i8* %0)
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* @SOCK_STREAM
%20 = load i32, i32* %5
%21 = call i32 @socket(i32 %2, i32 %19, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %42, label %23
23: ; preds = %18
store i32 1, i32* %4
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %21, i32 %24, i32 %25, i32* %4, i32 4)
store i32 1, i32* %4
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEPORT
%29 = call i32 @setsockopt(i32 %21, i32 %27, i32 %28, i32* %4, i32 4)
%30 = load i32, i32* @AF_INET6
%31 = icmp eq i32 %30, %2
br i1 %31, label %32, label %36
32: ; preds = %23
store i32 1, i32* %4
%33 = load i32, i32* @IPPROTO_IPV6
%34 = load i32, i32* @IPV6_V6ONLY
%35 = call i32 @setsockopt(i32 %21, i32 %33, i32 %34, i32* %4, i32 4)
br label %36
36: ; preds = %32, %23
%37 = call i64 @bind(i32 %21, i8* %9, i32 %11)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i64 @listen(i32 %21, i32 10)
%41 = icmp eq i64 %40, 0
br i1 %41, label %49, label %42
42: ; preds = %39, %36, %18
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %44)
%46 = icmp sgt i32 %21, -1
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @close(i32 %21)
br label %49
49: ; preds = %42, %47, %39, %3
%50 = phi i32 [ -1, %3 ], [ %21, %39 ], [ -1, %47 ], [ -1, %42 ]
ret i32 %50
}