#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*/ unix_socket_name;
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ENOENT;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
int /*<<< orphan*/ umask(int);
scalar_t__ unlink(char *);
int sock_dgram(char *path) {
  struct sockaddr_un unix_socket_name = {0};
  int fd;
  if (unlink(path) < 0) {
    if (errno != ENOENT) {
      fprintf(stderr, "%s: ", path);
      perror("unlink");
      return -1;
    }
  }
  unix_socket_name.sun_family = AF_UNIX;
  if (strlen(path) >= sizeof(unix_socket_name.sun_path))
    return -1;
  strcpy(unix_socket_name.sun_path, path);
  fd = socket(PF_UNIX, SOCK_DGRAM, 0);
  if (fd == -1)
    return -1;
  umask(0077);
  if (bind(fd, &unix_socket_name, sizeof(unix_socket_name))) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<sock_dgram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    121f <sock_dgram+0x7f>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
je     121a <sock_dgram+0x7a>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xdfc(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <sock_dgram+0x129>
jmp    121f <sock_dgram+0x7f>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     124c <sock_dgram+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <sock_dgram+0x129>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    128a <sock_dgram+0xea>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <sock_dgram+0x129>
mov    $0x3f,%edi
call   1090 <umask@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12c3 <sock_dgram+0x123>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <sock_dgram+0x129>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_dgram>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movl   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
call   1030 <unlink@plt>
test   %rax,%rax
jns    11ca <sock_dgram+0x3a>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    1247 <sock_dgram+0xb7>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0xffffffff,%ebp
cmp    $0x3,%eax
ja     123e <sock_dgram+0xae>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     123e <sock_dgram+0xae>
mov    %eax,%ebx
mov    $0x3f,%edi
call   1080 <umask@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
je     123c <sock_dgram+0xac>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    123e <sock_dgram+0xae>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    123e <sock_dgram+0xae>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [5 x i8] c"%s: \00"
@.str.1 = [7 x i8] c"unlink\00"
@AF_UNIX = global i32 0
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @sock_dgram(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
store i8* %0, i8** %3
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = load i8*, i8** %3
%8 = call i64 @unlink(i8* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %49
19: ; preds = %10
br label %20
20: ; preds = %19, %1
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i8*, i8** %3
%24 = call i32 @strlen(i8* %23)
%25 = sext i32 %24 to i64
%26 = icmp uge i64 %25, 4
br i1 %26, label %27, label %28
27: ; preds = %20
store i32 -1, i32* %2
br label %49
28: ; preds = %20
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i8*, i8** %3
%32 = call i32 @strcpy(i32 %30, i8* %31)
%33 = load i32, i32* @PF_UNIX
%34 = load i32, i32* @SOCK_DGRAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %28
store i32 -1, i32* %2
br label %49
39: ; preds = %28
%40 = call i32 @umask(i32 63)
%41 = load i32, i32* %5
%42 = call i64 @bind(i32 %41, %struct.sockaddr_un* %4, i32 12)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %2
br label %49
47: ; preds = %39
%48 = load i32, i32* %5
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %44, %38, %27, %14
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32, i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [5 x i8] c"%s: \00"
@.str.1 = [7 x i8] c"unlink\00"
@AF_UNIX = global i32 0
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @sock_dgram(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i64 @unlink(i8* %0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %31
14: ; preds = %6, %1
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = tail call i32 @strlen(i8* %0)
%18 = icmp ugt i32 %17, 3
br i1 %18, label %31, label %19
19: ; preds = %14
%20 = tail call i32 @strcpy(i32 0, i8* %0)
%21 = load i32, i32* @PF_UNIX
%22 = load i32, i32* @SOCK_DGRAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = tail call i32 @umask(i32 63)
%27 = call i64 @bind(i32 %23, %struct.sockaddr_un* %2, i32 12)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @close(i32 %23)
br label %31
31: ; preds = %25, %19, %14, %29, %10
%32 = phi i32 [ -1, %10 ], [ -1, %29 ], [ -1, %14 ], [ -1, %19 ], [ %23, %25 ]
ret i32 %32
}