#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_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int unix_server_t(const char *sockfile, int blocking) {
  int server_sock = 0;
  int on = 1;
  int stat = 0;
  struct sockaddr_un serveraddr;
  server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (server_sock < 0) {
    return -1;
  }
  {
    on = 1;
    setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  }
  if (!blocking) {
    stat = fcntl(server_sock, F_GETFL, NULL);
    stat |= O_NONBLOCK;
    fcntl(server_sock, F_SETFL, stat);
  }
  memset(&serveraddr, 0, sizeof(serveraddr));
  serveraddr.sun_family = AF_LOCAL;
  snprintf(serveraddr.sun_path, sizeof(serveraddr.sun_path), "%s", sockfile);
  if (bind(server_sock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) <
      0) {
    close(server_sock);
    return -1;
  }
  if (listen(server_sock, 5) < 0) {
    close(server_sock);
    return -1;
  }
  signal(SIGPIPE, SIG_IGN);
  return server_sock;
}


x86 assembly (clang, -O0):
<unix_server_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e6 <unix_server_t+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <unix_server_t+0x16f>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1254 <unix_server_t+0xc4>
mov    -0x18(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
call   1040 <fcntl@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xd7d(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12b7 <unix_server_t+0x127>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <unix_server_t+0x16f>
mov    -0x18(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12e2 <unix_server_t+0x152>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <unix_server_t+0x16f>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_server_t>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     12a6 <unix_server_t+0x116>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %ebx,%ebx
jne    1225 <unix_server_t+0x95>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2dcf(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9e(%rip),%rcx
mov    (%rcx),%esi
mov    %ebp,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%edi
lea    0xdaf(%rip),%rdx
mov    $0x4,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     129f <unix_server_t+0x10f>
mov    %ebp,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     129f <unix_server_t+0x10f>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    %ebp,%r15d
jmp    12a6 <unix_server_t+0x116>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@AF_LOCAL = global i32 0
@.str = [3 x i8] c"%s\00"
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
define i32 @unix_server_t(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = load i32, i32* @AF_UNIX
%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 %44, label %11
11: ; preds = %2
store i32 1, i32* %3
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %9, i32 %12, i32 %13, i32* %3, i32 4)
%15 = icmp eq i32 %1, 0
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i32, i32* @F_GETFL
%18 = call i32 @fcntl(i32 %9, i32 %17, i32 0)
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %19, %18
%21 = load i32, i32* @F_SETFL
%22 = call i32 @fcntl(i32 %9, i32 %21, i32 %20)
br label %23
23: ; preds = %16, %11
%24 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%25 = load i32, i32* @AF_LOCAL
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @snprintf(i32 %28, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%30 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%31 = call i64 @bind(i32 %9, %struct.sockaddr* %30, i32 8)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 @close(i32 %9)
br label %44
35: ; preds = %23
%36 = call i64 @listen(i32 %9, i32 5)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @close(i32 %9)
br label %44
40: ; preds = %35
%41 = load i32, i32* @SIGPIPE
%42 = load i32, i32* @SIG_IGN
%43 = call i32 @signal(i32 %41, i32 %42)
br label %44
44: ; preds = %2, %40, %38, %33
%45 = phi i32 [ -1, %33 ], [ -1, %38 ], [ %9, %40 ], [ -1, %2 ]
ret i32 %45
}