#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_DGRAM;
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);
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_u(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_DGRAM, 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;
  }
  signal(SIGPIPE, SIG_IGN);
  return server_sock;
}


x86 assembly (clang, -O0):
<unix_server_u>:
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    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11d6 <unix_server_u+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <unix_server_u+0x144>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1244 <unix_server_u+0xc4>
mov    -0x18(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d76(%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    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xd8d(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12a7 <unix_server_u+0x127>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <unix_server_u+0x144>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
mov    0x2d09(%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_u>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1285 <unix_server_u+0x105>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %ebp,%ebp
jne    1215 <unix_server_u+0x95>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    0x2da6(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%edi
lea    0xdbf(%rip),%rdx
mov    $0x4,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     127e <unix_server_u+0xfe>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    %ebx,%r15d
jmp    1285 <unix_server_u+0x105>
mov    %ebx,%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_DGRAM = 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_u(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_DGRAM
%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 %54
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 %54
49: ; preds = %34
%50 = load i32, i32* @SIGPIPE
%51 = load i32, i32* @SIG_IGN
%52 = call i32 @signal(i32 %50, i32 %51)
%53 = load i32, i32* %6
store i32 %53, i32* %3
br label %54
54: ; preds = %49, %46, %15
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = 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_u(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_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %39, 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 %39
35: ; preds = %23
%36 = load i32, i32* @SIGPIPE
%37 = load i32, i32* @SIG_IGN
%38 = call i32 @signal(i32 %36, i32 %37)
br label %39
39: ; preds = %2, %35, %33
%40 = phi i32 [ -1, %33 ], [ %9, %35 ], [ -1, %2 ]
ret i32 %40
}