#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*/ sockaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ unlink(char const *);
int open_lsocket(const char *filename) {
  struct sockaddr_un sockaddr;
  int sock;
  errno = EINVAL;
  if (filename == NULL || !*filename)
    return -1;
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  unlink(filename);
  sockaddr.sun_family = AF_UNIX;
  strncpy(sockaddr.sun_path, filename, sizeof(sockaddr.sun_path));
  if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1 ||
      listen(sock, -1) == -1) {
    shutdown(sock, SHUT_RDWR);
    close(sock);
    return -1;
  }
  fcntl(sock, F_SETFD, FD_CLOEXEC);
  return sock;
}


x86 assembly (clang, -O0):
<open_lsocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%ecx
mov    0x2e14(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11b6 <open_lsocket+0x36>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11c2 <open_lsocket+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <open_lsocket+0x10f>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f4 <open_lsocket+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <open_lsocket+0x10f>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     124a <open_lsocket+0xca>
mov    -0x1c(%rbp),%edi
mov    $0xffffffff,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    126f <open_lsocket+0xef>
mov    -0x1c(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
call   1060 <shutdown@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128f <open_lsocket+0x10f>
mov    -0x1c(%rbp),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
call   1050 <fcntl@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_lsocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    0x2e18(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     125c <open_lsocket+0xdc>
mov    %rdi,%rbp
cmpb   $0x0,(%rdi)
je     125c <open_lsocket+0xdc>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     125c <open_lsocket+0xdc>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbp,%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1243 <open_lsocket+0xc3>
mov    %r14d,%edi
mov    $0xffffffff,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1243 <open_lsocket+0xc3>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
call   1050 <fcntl@plt>
mov    %r14d,%ebx
jmp    125c <open_lsocket+0xdc>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1060 <shutdown@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@SHUT_RDWR = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_lsocket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @EINVAL
store i32 %6, i32* @errno
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %9, %1
store i32 -1, i32* %2
br label %50
14: ; preds = %9
%15 = load i32, i32* @PF_UNIX
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %2
br label %50
21: ; preds = %14
%22 = load i8*, i8** %3
%23 = call i32 @unlink(i8* %22)
%24 = load i32, i32* @AF_UNIX
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i8*, i8** %3
%29 = call i32 @strncpy(i32 %27, i8* %28, i32 4)
%30 = load i32, i32* %5
%31 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 8)
%33 = icmp eq i32 %32, -1
br i1 %33, label %38, label %34
34: ; preds = %21
%35 = load i32, i32* %5
%36 = call i32 @listen(i32 %35, i32 -1)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %34, %21
%39 = load i32, i32* %5
%40 = load i32, i32* @SHUT_RDWR
%41 = call i32 @shutdown(i32 %39, i32 %40)
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %2
br label %50
44: ; preds = %34
%45 = load i32, i32* %5
%46 = load i32, i32* @F_SETFD
%47 = load i32, i32* @FD_CLOEXEC
%48 = call i32 @fcntl(i32 %45, i32 %46, i32 %47)
%49 = load i32, i32* %5
store i32 %49, i32* %2
br label %50
50: ; preds = %44, %38, %20, %13
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@SHUT_RDWR = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_lsocket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @EINVAL
store i32 %4, i32* @errno
%5 = icmp eq i8* %0, null
br i1 %5, label %33, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %33, label %9
9: ; preds = %6
%10 = load i32, i32* @PF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %33, label %14
14: ; preds = %9
%15 = tail call i32 @unlink(i8* %0)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%19 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%20 = call i32 @bind(i32 %12, %struct.sockaddr* %19, i32 8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %14
%23 = call i32 @listen(i32 %12, i32 -1)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22, %14
%26 = load i32, i32* @SHUT_RDWR
%27 = call i32 @shutdown(i32 %12, i32 %26)
%28 = call i32 @close(i32 %12)
br label %33
29: ; preds = %22
%30 = load i32, i32* @F_SETFD
%31 = load i32, i32* @FD_CLOEXEC
%32 = call i32 @fcntl(i32 %12, i32 %30, i32 %31)
br label %33
33: ; preds = %9, %1, %6, %29, %25
%34 = phi i32 [ -1, %25 ], [ %12, %29 ], [ -1, %6 ], [ -1, %1 ], [ -1, %9 ]
ret i32 %34
}