#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
struct linger {
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ ling;
typedef int /*<<< orphan*/ flags;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ SO_LINGER;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ S_ISSOCK(int /*<<< orphan*/);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
char *conf_get(char *);
int listen(int, int);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int new_socket_unix();
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int umask(int);
int /*<<< orphan*/ unlink(char *);
int server_socket_unix(void) {
  int sfd;
  struct linger ling = {0, 0};
  struct sockaddr_un addr;
  struct stat tstat;
  int flags = 1;
  int old_umask, access_mask = 0777;
  char *path;
  path = conf_get("path");
  if (!path) {
    return -1;
  }
  if ((sfd = new_socket_unix()) == -1) {
    return -1;
  }
  if (lstat(path, &tstat) == 0) {
    if (S_ISSOCK(tstat.st_mode))
      unlink(path);
  }
  setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags));
  setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
  setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
  old_umask = umask(~(access_mask & 0777));
  if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    close(sfd);
    umask(old_umask);
    return 1;
  }
  umask(old_umask);
  if (listen(sfd, 10000) == -1) {
    close(sfd);
    return 1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<server_socket_unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
movl   $0x1,-0x24(%rbp)
movl   $0x1ff,-0x2c(%rbp)
lea    0xe13(%rip),%rdi
call   10c0 <conf_get@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    120d <server_socket_unix+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <server_socket_unix+0x1be>
mov    $0x0,%al
call   10b0 <new_socket_unix@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    122c <server_socket_unix+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    137e <server_socket_unix+0x1be>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0x0,%rax
jne    1263 <server_socket_unix+0xa3>
mov    -0x20(%rbp),%edi
call   1060 <S_ISSOCK@plt>
cmp    $0x0,%rax
je     125e <server_socket_unix+0x9e>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
jmp    1263 <server_socket_unix+0xa3>
mov    -0x8(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    -0x8(%rbp),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
mov    0x2d22(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1050 <setsockopt@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x2c(%rbp),%edi
and    $0x1ff,%edi
xor    $0xffffffff,%edi
call   10a0 <umask@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1346 <server_socket_unix+0x186>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%edi
call   10a0 <umask@plt>
movl   $0x1,-0x4(%rbp)
jmp    137e <server_socket_unix+0x1be>
mov    -0x28(%rbp),%edi
call   10a0 <umask@plt>
mov    -0x8(%rbp),%edi
mov    $0x2710,%esi
call   10d0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1378 <server_socket_unix+0x1b8>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    137e <server_socket_unix+0x1be>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_socket_unix>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x1,0x4(%rsp)
lea    0xe1e(%rip),%rdi
call   10c0 <conf_get@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1324 <server_socket_unix+0x164>
mov    %rax,%rbx
xor    %eax,%eax
call   10b0 <new_socket_unix@plt>
cmp    $0xffffffff,%eax
je     1324 <server_socket_unix+0x164>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <lstat@plt>
test   %rax,%rax
jne    1230 <server_socket_unix+0x70>
mov    (%rsp),%edi
call   1060 <S_ISSOCK@plt>
test   %rax,%rax
je     1230 <server_socket_unix+0x70>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2db1(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r15
mov    %r14d,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %r14d,%edi
mov    $0x8,%r8d
call   1050 <setsockopt@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    $0xfffffe00,%edi
call   10a0 <umask@plt>
mov    %eax,%ebx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
je     1306 <server_socket_unix+0x146>
mov    %ebx,%edi
call   10a0 <umask@plt>
mov    %r14d,%edi
mov    $0x2710,%esi
call   10d0 <listen@plt>
cmp    $0xffffffff,%eax
je     1317 <server_socket_unix+0x157>
mov    %r14d,%ebp
jmp    1324 <server_socket_unix+0x164>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %ebx,%edi
call   10a0 <umask@plt>
jmp    131f <server_socket_unix+0x15f>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.linger = type { i32, i32 }
%struct.sockaddr_un = type { i32, i32 }
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"path\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_LINGER = global i32 0
@AF_UNIX = global i32 0
define i32 @server_socket_unix() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.linger
%4 = alloca %struct.sockaddr_un
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = bitcast %struct.linger* %3 to i8*
store i32 1, i32* %6
store i32 511, i32* %8
%11 = call i8* @conf_get(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %0
store i32 -1, i32* %1
br label %79
15: ; preds = %0
%16 = call i32 (...) @new_socket_unix()
store i32 %16, i32* %2
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %1
br label %79
19: ; preds = %15
%20 = load i8*, i8** %9
%21 = call i64 @lstat(i8* %20, %struct.stat* %5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @S_ISSOCK(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %9
%30 = call i32 @unlink(i8* %29)
br label %31
31: ; preds = %28, %23
br label %32
32: ; preds = %31, %19
%33 = load i32, i32* %2
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = bitcast i32* %6 to i8*
%37 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i8* %36, i32 4)
%38 = load i32, i32* %2
%39 = load i32, i32* @SOL_SOCKET
%40 = load i32, i32* @SO_KEEPALIVE
%41 = bitcast i32* %6 to i8*
%42 = call i32 @setsockopt(i32 %38, i32 %39, i32 %40, i8* %41, i32 4)
%43 = load i32, i32* %2
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_LINGER
%46 = bitcast %struct.linger* %3 to i8*
%47 = call i32 @setsockopt(i32 %43, i32 %44, i32 %45, i8* %46, i32 8)
%48 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%49 = load i32, i32* @AF_UNIX
%50 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %49, i32* %50
%51 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i8*, i8** %9
%54 = call i32 @strncpy(i32 %52, i8* %53, i32 3)
%55 = load i32, i32* %8
%56 = and i32 %55, 511
%57 = xor i32 %56, -1
%58 = call i32 @umask(i32 %57)
store i32 %58, i32* %7
%59 = load i32, i32* %2
%60 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%61 = call i32 @bind(i32 %59, %struct.sockaddr* %60, i32 8)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %68
63: ; preds = %32
%64 = load i32, i32* %2
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %7
%67 = call i32 @umask(i32 %66)
store i32 1, i32* %1
br label %79
68: ; preds = %32
%69 = load i32, i32* %7
%70 = call i32 @umask(i32 %69)
%71 = load i32, i32* %2
%72 = call i32 @listen(i32 %71, i32 10000)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %77
74: ; preds = %68
%75 = load i32, i32* %2
%76 = call i32 @close(i32 %75)
store i32 1, i32* %1
br label %79
77: ; preds = %68
%78 = load i32, i32* %2
store i32 %78, i32* %1
br label %79
79: ; preds = %77, %74, %63, %18, %14
%80 = load i32, i32* %1
ret i32 %80
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"path\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_LINGER = global i32 0
@AF_UNIX = global i32 0
define i32 @server_socket_unix() {
%1 = alloca i64
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.stat
%4 = alloca i32
%5 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%6 = bitcast %struct.sockaddr_un* %2 to i8*
%7 = bitcast %struct.stat* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%9 = tail call i8* @conf_get(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %53, label %11
11: ; preds = %0
%12 = tail call i32 (...) @new_socket_unix()
%13 = icmp eq i32 %12, -1
br i1 %13, label %53, label %14
14: ; preds = %11
%15 = call i64 @lstat(i8* %9, %struct.stat* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i64 @S_ISSOCK(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = tail call i32 @unlink(i8* %9)
br label %24
24: ; preds = %17, %22, %14
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %12, i32 %25, i32 %26, i8* %8, i32 4)
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_KEEPALIVE
%30 = call i32 @setsockopt(i32 %12, i32 %28, i32 %29, i8* %8, i32 4)
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_LINGER
%33 = call i32 @setsockopt(i32 %12, i32 %31, i32 %32, i8* %5, i32 8)
%34 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%35 = load i32, i32* @AF_UNIX
%36 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @strncpy(i32 %38, i8* %9, i32 3)
%40 = call i32 @umask(i32 -512)
%41 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%42 = call i32 @bind(i32 %12, %struct.sockaddr* %41, i32 8)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %24
%45 = call i32 @close(i32 %12)
%46 = call i32 @umask(i32 %40)
br label %53
47: ; preds = %24
%48 = call i32 @umask(i32 %40)
%49 = call i32 @listen(i32 %12, i32 10000)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @close(i32 %12)
br label %53
53: ; preds = %47, %11, %0, %51, %44
%54 = phi i32 [ 1, %44 ], [ 1, %51 ], [ -1, %0 ], [ -1, %11 ], [ %12, %47 ]
ret i32 %54
}