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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int umask(int);
int make_socket(const char *pathname, mode_t mode) {
  struct sockaddr_un sun;
  socklen_t alen;
  mode_t old_mask;
  int fd, rv = 1;
  memset(&sun, 0, sizeof(sun));
  sun.sun_family = AF_LOCAL;
  strcpy(sun.sun_path, pathname);
  alen = SUN_LEN(&sun);
  fd = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (fd < 0) {
    fprintf(stderr, "Unable to create PF_LOCAL socket\n");
    return 0;
  }
  old_mask = umask(~mode & 0777);
  if (bind(fd, (struct sockaddr *)&sun, alen) < 0) {
    fprintf(stderr, "cannot bind socket to %s: %m", pathname);
    rv = 0;
  }
  umask(old_mask);
  close(fd);
  return rv;
}


x86 assembly (clang, -O0):
<make_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x20(%rbp),%rdi
call   1090 <SUN_LEN@plt>
mov    %eax,-0x24(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1213 <make_socket+0x93>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <make_socket+0xf8>
mov    -0x14(%rbp),%edi
xor    $0xffffffff,%edi
and    $0x1ff,%edi
call   1070 <umask@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1262 <make_socket+0xe2>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <umask@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x8(%rsp),%rbp
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1090 <SUN_LEN@plt>
mov    %eax,%r12d
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1243 <make_socket+0xc3>
mov    %eax,%ebp
not    %ebx
and    $0x1ff,%ebx
mov    %ebx,%edi
call   1070 <umask@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    %r12d,%edx
call   1080 <bind@plt>
mov    $0x1,%r15d
test   %rax,%rax
jns    1233 <make_socket+0xb3>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1070 <umask@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    125a <make_socket+0xda>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Unable to create PF_LOCAL socket\0A\00"
@.str.1 = [29 x i8] c"cannot bind socket to %s: %m\00"
define i32 @make_socket(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %10
%11 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%12 = load i32, i32* @AF_LOCAL
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %4
%17 = call i32 @strcpy(i32 %15, i8* %16)
%18 = call i32 @SUN_LEN(%struct.sockaddr_un* %6)
store i32 %18, i32* %7
%19 = load i32, i32* @PF_LOCAL
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %47
27: ; preds = %2
%28 = load i32, i32* %5
%29 = xor i32 %28, -1
%30 = and i32 %29, 511
%31 = call i32 @umask(i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %9
%33 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%34 = load i32, i32* %7
%35 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %27
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %4
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i32 0, i32* %10
br label %41
41: ; preds = %37, %27
%42 = load i32, i32* %8
%43 = call i32 @umask(i32 %42)
%44 = load i32, i32* %9
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %10
store i32 %46, i32* %3
br label %47
47: ; preds = %41, %24
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Unable to create PF_LOCAL socket\0A\00"
@.str.1 = [29 x i8] c"cannot bind socket to %s: %m\00"
define i32 @make_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%6 = load i32, i32* @AF_LOCAL
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @strcpy(i32 %9, i8* %0)
%11 = call i32 @SUN_LEN(%struct.sockaddr_un* %3)
%12 = load i32, i32* @PF_LOCAL
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %33
19: ; preds = %2
%20 = and i32 %1, 511
%21 = xor i32 %20, 511
%22 = call i32 @umask(i32 %21)
%23 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%24 = call i64 @bind(i32 %14, %struct.sockaddr* %23, i32 %11)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %26, %19
%30 = phi i32 [ 0, %26 ], [ 1, %19 ]
%31 = call i32 @umask(i32 %22)
%32 = call i32 @close(i32 %14)
br label %33
33: ; preds = %29, %16
%34 = phi i32 [ 0, %16 ], [ %30, %29 ]
ret i32 %34
}