#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*/ sa;
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ chmod(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ rr_net_error(char *, char *, int /*<<< orphan*/);
int rr_net_listen(char *, int, struct sockaddr *, int, int);
int rr_net_reuseaddr(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int rr_net_unixserver(char *err, char *path, mode_t perm, int backlog) {
  int s;
  struct sockaddr_un sa;
  if ((s = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
    rr_net_error(err, "creating unix domain socket: %s", strerror(errno));
    return -1;
  }
  if (rr_net_reuseaddr(err, s) == -1) {
    close(s);
    return -1;
  }
  memset(&sa, 0, sizeof(sa));
  sa.sun_family = AF_LOCAL;
  strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
  if (rr_net_listen(err, s, (struct sockaddr *)&sa, sizeof(sa), backlog) == -1)
    return -1;
  if (perm)
    chmod(sa.sun_path, perm);
  return s;
}


x86 assembly (clang, -O0):
<rr_net_unixserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11ff <rr_net_unixserver+0x6f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe12(%rip),%rsi
call   1080 <rr_net_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <rr_net_unixserver+0x10e>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <rr_net_reuseaddr@plt>
cmp    $0xffffffff,%eax
jne    1228 <rr_net_unixserver+0x98>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <rr_net_unixserver+0x10e>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%r8d
mov    $0x8,%ecx
call   1070 <rr_net_listen@plt>
cmp    $0xffffffff,%eax
jne    1283 <rr_net_unixserver+0xf3>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <rr_net_unixserver+0x10e>
cmpl   $0x0,-0x1c(%rbp)
je     1298 <rr_net_unixserver+0x108>
mov    -0x30(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1090 <chmod@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rr_net_unixserver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbp
mov    0x2e3a(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     122b <rr_net_unixserver+0x9b>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %eax,%esi
call   1040 <rr_net_reuseaddr@plt>
cmp    $0xffffffff,%eax
je     124c <rr_net_unixserver+0xbc>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r12,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %rsp,%rdx
mov    $0x8,%ecx
mov    %r15d,%r8d
call   1070 <rr_net_listen@plt>
cmp    $0xffffffff,%eax
je     1253 <rr_net_unixserver+0xc3>
test   %r14d,%r14d
je     1258 <rr_net_unixserver+0xc8>
mov    (%rsp),%edi
mov    %r14d,%esi
call   1090 <chmod@plt>
jmp    1258 <rr_net_unixserver+0xc8>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc0(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   1080 <rr_net_error@plt>
jmp    1253 <rr_net_unixserver+0xc3>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"creating unix domain socket: %s\00"
@errno = global i32 0
define i32 @rr_net_unixserver(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_un
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* @AF_LOCAL
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @rr_net_error(i8* %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 -1, i32* %5
br label %54
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = load i32, i32* %10
%24 = call i32 @rr_net_reuseaddr(i8* %22, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %10
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %5
br label %54
29: ; preds = %21
%30 = call i32 @memset(%struct.sockaddr_un* %11, i32 0, i32 8)
%31 = load i32, i32* @AF_LOCAL
%32 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i8*, i8** %7
%36 = call i32 @strncpy(i32 %34, i8* %35, i32 3)
%37 = load i8*, i8** %6
%38 = load i32, i32* %10
%39 = bitcast %struct.sockaddr_un* %11 to %struct.sockaddr*
%40 = load i32, i32* %9
%41 = call i32 @rr_net_listen(i8* %37, i32 %38, %struct.sockaddr* %39, i32 8, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %29
store i32 -1, i32* %5
br label %54
44: ; preds = %29
%45 = load i32, i32* %8
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 0
%49 = load i32, i32* %48
%50 = load i32, i32* %8
%51 = call i32 @chmod(i32 %49, i32 %50)
br label %52
52: ; preds = %47, %44
%53 = load i32, i32* %10
store i32 %53, i32* %5
br label %54
54: ; preds = %52, %43, %26, %16
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"creating unix domain socket: %s\00"
@errno = global i32 0
define i32 @rr_net_unixserver(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_un
%6 = bitcast %struct.sockaddr_un* %5 to i8*
%7 = load i32, i32* @AF_LOCAL
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %4
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 @rr_net_error(i8* %0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %35
15: ; preds = %4
%16 = tail call i32 @rr_net_reuseaddr(i8* %0, i32 %9)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @close(i32 %9)
br label %35
20: ; preds = %15
%21 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%22 = load i32, i32* @AF_LOCAL
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @strncpy(i32 %25, i8* %1, i32 3)
%27 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%28 = call i32 @rr_net_listen(i8* %0, i32 %9, %struct.sockaddr* %27, i32 8, i32 %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %35, label %30
30: ; preds = %20
%31 = icmp eq i32 %2, 0
br i1 %31, label %35, label %32
32: ; preds = %30
%33 = load i32, i32* %24
%34 = call i32 @chmod(i32 %33, i32 %2)
br label %35
35: ; preds = %30, %32, %20, %18, %11
%36 = phi i32 [ -1, %11 ], [ -1, %18 ], [ -1, %20 ], [ %9, %32 ], [ %9, %30 ]
ret i32 %36
}