#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*/ su;
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int mode_t;

/* Variables and functions */
int ENAMETOOLONG;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ listen(int, int /*<<< orphan*/);
int snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int umask(int);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int sock_listen(const char *sockpath) {
  int sock = -1;
  struct sockaddr_un su;
  mode_t om;
  int ret;
  int saved_errno;
  sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (sock < 0)
    goto fail;
  su.sun_family = PF_LOCAL;
  ret = snprintf(su.sun_path, sizeof(su.sun_path), "%s", sockpath);
  if (ret < 0 || (size_t)ret >= sizeof(su.sun_path)) {
    errno = ENAMETOOLONG;
    goto fail;
  }
  unlink(su.sun_path);
  om = umask(077);
  if (bind(sock, (struct sockaddr *)&su, sizeof(su)) < 0) {
    saved_errno = errno;
    umask(om);
    errno = saved_errno;
    goto fail;
  }
  umask(om);
  if (listen(sock, SOMAXCONN) < 0)
    goto fail;
  return sock;
fail:
  saved_errno = errno;
  if (sock >= 0) {
    close(sock);
    unlink(su.sun_path);
  }
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<sock_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ae <sock_listen+0x3e>
jmp    128c <sock_listen+0x11c>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe33(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     11ed <sock_listen+0x7d>
movslq -0x28(%rbp),%rax
cmp    $0x4,%rax
jb     1204 <sock_listen+0x94>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd3(%rip),%rax
mov    %ecx,(%rax)
jmp    128c <sock_listen+0x11c>
mov    -0x20(%rbp),%edi
call   1030 <unlink@plt>
mov    $0x3f,%edi
call   1060 <umask@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1259 <sock_listen+0xe9>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <umask@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2d7e(%rip),%rax
mov    %ecx,(%rax)
jmp    128c <sock_listen+0x11c>
mov    -0x24(%rbp),%edi
call   1060 <umask@plt>
mov    -0x14(%rbp),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1281 <sock_listen+0x111>
jmp    128c <sock_listen+0x11c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c5 <sock_listen+0x155>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     12b2 <sock_listen+0x142>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2d14(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e3f(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11ce <sock_listen+0x5e>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
lea    0xe58(%rip),%rdx
mov    $0x4,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
cmp    $0x4,%eax
jb     11d9 <sock_listen+0x69>
mov    0x2e07(%rip),%rax
mov    (%rax),%ebp
mov    0x2e06(%rip),%rax
mov    %ebp,(%rax)
jmp    123c <sock_listen+0xcc>
mov    0x2dfb(%rip),%rax
mov    (%rax),%ebp
jmp    124b <sock_listen+0xdb>
call   1030 <unlink@plt>
mov    $0x3f,%edi
call   1060 <umask@plt>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1227 <sock_listen+0xb7>
mov    %r14d,%edi
call   1060 <umask@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <listen@plt>
test   %rax,%rax
jns    1259 <sock_listen+0xe9>
mov    0x2dad(%rip),%rax
mov    (%rax),%ebp
jmp    123c <sock_listen+0xcc>
mov    0x2da2(%rip),%r15
mov    (%r15),%ebp
mov    %r14d,%edi
call   1060 <umask@plt>
mov    %ebp,(%r15)
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%rsp),%edi
call   1030 <unlink@plt>
mov    0x2d7e(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@SOMAXCONN = global i32 0
define i32 @sock_listen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
%9 = load i32, i32* @PF_LOCAL
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
br label %54
15: ; preds = %1
%16 = load i32, i32* @PF_LOCAL
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i8*, i8** %3
%21 = call i32 @snprintf(i32 %19, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %15
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = icmp uge i64 %26, 4
br i1 %27, label %28, label %30
28: ; preds = %24, %15
%29 = load i32, i32* @ENAMETOOLONG
store i32 %29, i32* @errno
br label %54
30: ; preds = %24
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @unlink(i32 %32)
%34 = call i32 @umask(i32 63)
store i32 %34, i32* %6
%35 = load i32, i32* %4
%36 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%37 = call i64 @bind(i32 %35, %struct.sockaddr* %36, i32 8)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %30
%40 = load i32, i32* @errno
store i32 %40, i32* %8
%41 = load i32, i32* %6
%42 = call i32 @umask(i32 %41)
%43 = load i32, i32* %8
store i32 %43, i32* @errno
br label %54
44: ; preds = %30
%45 = load i32, i32* %6
%46 = call i32 @umask(i32 %45)
%47 = load i32, i32* %4
%48 = load i32, i32* @SOMAXCONN
%49 = call i64 @listen(i32 %47, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %44
br label %54
52: ; preds = %44
%53 = load i32, i32* %4
store i32 %53, i32* %2
br label %66
54: ; preds = %51, %39, %28, %14
%55 = load i32, i32* @errno
store i32 %55, i32* %8
%56 = load i32, i32* %4
%57 = icmp sge i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %54
%59 = load i32, i32* %4
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @unlink(i32 %62)
br label %64
64: ; preds = %58, %54
%65 = load i32, i32* %8
store i32 %65, i32* @errno
store i32 -1, i32* %2
br label %66
66: ; preds = %64, %52
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@SOMAXCONN = global i32 0
define i32 @sock_listen(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @PF_LOCAL
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %32, label %8
8: ; preds = %1
%9 = load i32, i32* @PF_LOCAL
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%12 = tail call i32 @snprintf(i32 undef, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = icmp ugt i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = load i32, i32* @ENAMETOOLONG
store i32 %15, i32* @errno
br label %34
16: ; preds = %8
%17 = tail call i32 @unlink(i32 undef)
%18 = tail call i32 @umask(i32 63)
%19 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%20 = call i64 @bind(i32 %6, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i32, i32* @errno
%24 = call i32 @umask(i32 %18)
store i32 %23, i32* @errno
br label %34
25: ; preds = %16
%26 = call i32 @umask(i32 %18)
%27 = load i32, i32* @SOMAXCONN
%28 = call i64 @listen(i32 %6, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %25
%31 = load i32, i32* @errno
br label %34
32: ; preds = %1
%33 = load i32, i32* @errno
br label %39
34: ; preds = %30, %22, %14
%35 = phi i32 [ %31, %30 ], [ %23, %22 ], [ %15, %14 ]
%36 = call i32 @close(i32 %6)
%37 = load i32, i32* %11
%38 = call i32 @unlink(i32 %37)
br label %39
39: ; preds = %32, %34
%40 = phi i32 [ %35, %34 ], [ %33, %32 ]
store i32 %40, i32* @errno
br label %41
41: ; preds = %25, %39
%42 = phi i32 [ -1, %39 ], [ %6, %25 ]
ret i32 %42
}