#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 /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ 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;
  sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (sock < 0)
    goto fail;
  su.sun_family = PF_LOCAL;
  snprintf(su.sun_path, (sizeof(su.sun_path)), "%s", sockpath);
  unlink(su.sun_path);
  om = umask(077);
  if (bind(sock, (struct sockaddr *)&su, sizeof(su)) < 0) {
    umask(om);
    goto fail;
  }
  umask(om);
  if (listen(sock, SOMAXCONN) < 0)
    goto fail;
  return sock;
fail:
  if (sock >= 0)
    close(sock);
  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    0x2e46(%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    1242 <sock_listen+0xd2>
mov    0x2e1b(%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    -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    120f <sock_listen+0x9f>
mov    -0x24(%rbp),%edi
call   1060 <umask@plt>
jmp    1242 <sock_listen+0xd2>
mov    -0x24(%rbp),%edi
call   1060 <umask@plt>
mov    -0x14(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1237 <sock_listen+0xc7>
jmp    1242 <sock_listen+0xd2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <sock_listen+0xeb>
cmpl   $0x0,-0x14(%rbp)
jl     1254 <sock_listen+0xe4>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
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,%rbx
mov    0x2e4f(%rip),%r15
mov    (%r15),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     120d <sock_listen+0x9d>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
lea    0xe52(%rip),%rdx
mov    $0x4,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
call   1030 <unlink@plt>
mov    $0x3f,%edi
call   1060 <umask@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
mov    %rax,%rbx
mov    %r15d,%edi
call   1060 <umask@plt>
test   %rbx,%rbx
js     1206 <sock_listen+0x96>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <listen@plt>
test   %rax,%rax
js     1206 <sock_listen+0x96>
mov    %ebp,%r14d
jmp    120d <sock_listen+0x9d>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%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"
@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
store i8* %0, i8** %3
store i32 -1, i32* %4
%7 = load i32, i32* @PF_LOCAL
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
br label %41
13: ; preds = %1
%14 = load i32, i32* @PF_LOCAL
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @snprintf(i32 %17, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @unlink(i32 %21)
%23 = call i32 @umask(i32 63)
store i32 %23, i32* %6
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %13
%29 = load i32, i32* %6
%30 = call i32 @umask(i32 %29)
br label %41
31: ; preds = %13
%32 = load i32, i32* %6
%33 = call i32 @umask(i32 %32)
%34 = load i32, i32* %4
%35 = load i32, i32* @SOMAXCONN
%36 = call i64 @listen(i32 %34, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %31
br label %41
39: ; preds = %31
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %48
41: ; preds = %38, %28, %12
%42 = load i32, i32* %4
%43 = icmp sge i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* %4
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44, %41
store i32 -1, i32* %2
br label %48
48: ; preds = %47, %39
%49 = load i32, i32* %2
ret i32 %49
}

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"
@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 %24, 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 = tail call i32 @snprintf(i32 undef, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = tail call i32 @unlink(i32 undef)
%13 = tail call i32 @umask(i32 63)
%14 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%15 = call i64 @bind(i32 %6, %struct.sockaddr* %14, i32 8)
%16 = icmp slt i64 %15, 0
%17 = call i32 @umask(i32 %13)
br i1 %16, label %22, label %18
18: ; preds = %8
%19 = load i32, i32* @SOMAXCONN
%20 = call i64 @listen(i32 %6, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %8, %18
%23 = call i32 @close(i32 %6)
br label %24
24: ; preds = %1, %22, %18
%25 = phi i32 [ %6, %18 ], [ -1, %22 ], [ -1, %1 ]
ret i32 %25
}