#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 dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int S_IRUSR;
int S_IWUSR;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ chmod(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ runes_die(char *, char *, ...);
int /*<<< orphan*/ runes_mkdir_p(char *);
int /*<<< orphan*/ runes_socket_populate_sockaddr(char *, struct sockaddr_un *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strdup(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *strrchr(char *, char);
int /*<<< orphan*/ unlink(char *);
int runes_socket_server_open(char *path) {
  char *dir, *slash;
  int s;
  struct sockaddr_un server;
  runes_socket_populate_sockaddr(path, &server);
  dir = strdup(path);
  slash = strrchr(dir, '/');
  if (slash == NULL) {
    runes_die("socket path %s must be an absolute path", path);
  }
  *slash = '\0';
  runes_mkdir_p(dir);
  free(dir);
  unlink(path);
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s < 0) {
    runes_die("couldn't create socket: %s", strerror(errno));
  }
  if (bind(s, (struct sockaddr *)(&server), sizeof(struct sockaddr_un))) {
    runes_die("couldn't bind to socket %s: %s", path, strerror(errno));
  }
  if (chmod(path, S_IRUSR | S_IWUSR)) {
    runes_die("couldn't chmod socket %s: %s", path, strerror(errno));
  }
  if (listen(s, 5)) {
    runes_die("couldn't listen on socket %s: %s", path, strerror(errno));
  }
  return s;
}


x86 assembly (clang, -O0):
<runes_socket_server_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10d0 <runes_socket_populate_sockaddr@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1215 <runes_socket_server_open+0x55>
mov    -0x8(%rbp),%rsi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1050 <runes_die@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1070 <runes_mkdir_p@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1040 <unlink@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    127c <runes_socket_server_open+0xbc>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
movslq %eax,%rsi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <runes_die@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12c1 <runes_socket_server_open+0x101>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <runes_die@plt>
mov    -0x8(%rbp),%rdi
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
mov    0x2d0b(%rip),%rax
or     (%rax),%esi
call   1090 <chmod@plt>
cmp    $0x0,%rax
je     1310 <runes_socket_server_open+0x150>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1050 <runes_die@plt>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1351 <runes_socket_server_open+0x191>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1050 <runes_die@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runes_socket_server_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   10d0 <runes_socket_populate_sockaddr@plt>
mov    %r14,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1206 <runes_socket_server_open+0x46>
lea    0xe04(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <runes_die@plt>
movb   $0x0,(%rbx)
mov    %rbp,%rdi
call   1070 <runes_mkdir_p@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    125f <runes_socket_server_open+0x9f>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
movslq %eax,%rsi
lea    0xdd0(%rip),%rdi
xor    %eax,%eax
call   1050 <runes_die@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   10a0 <bind@plt>
test   %rax,%rax
je     1296 <runes_socket_server_open+0xd6>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdb9(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <runes_die@plt>
mov    0x2d23(%rip),%rax
mov    0x2d3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   1090 <chmod@plt>
test   %rax,%rax
je     12d6 <runes_socket_server_open+0x116>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd98(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <runes_die@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     1308 <runes_socket_server_open+0x148>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd83(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <runes_die@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [40 x i8] c"socket path %s must be an absolute path\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [27 x i8] c"couldn't create socket: %s\00"
@errno = global i32 0
@.str.2 = [31 x i8] c"couldn't bind to socket %s: %s\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [29 x i8] c"couldn't chmod socket %s: %s\00"
@.str.4 = [33 x i8] c"couldn't listen on socket %s: %s\00"
define i32 @runes_socket_server_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @runes_socket_populate_sockaddr(i8* %7, %struct.sockaddr_un* %6)
%9 = load i8*, i8** %2
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = call i8* @strrchr(i8* %11, i8 signext 47)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %1
%19 = load i8*, i8** %4
store i8 0, i8* %19
%20 = load i8*, i8** %3
%21 = call i32 @runes_mkdir_p(i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
%24 = load i8*, i8** %2
%25 = call i32 @unlink(i8* %24)
%26 = load i32, i32* @AF_UNIX
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %18
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = sext i32 %33 to i64
%35 = inttoptr i64 %34 to i8*
%36 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %31, %18
%38 = load i32, i32* %5
%39 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%40 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 4)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i8*, i8** %2
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %43, i32 %45)
br label %47
47: ; preds = %42, %37
%48 = load i8*, i8** %2
%49 = load i32, i32* @S_IRUSR
%50 = load i32, i32* @S_IWUSR
%51 = or i32 %49, %50
%52 = call i64 @chmod(i8* %48, i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %47
%55 = load i8*, i8** %2
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %55, i32 %57)
br label %59
59: ; preds = %54, %47
%60 = load i32, i32* %5
%61 = call i64 @listen(i32 %60, i32 5)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %59
%64 = load i8*, i8** %2
%65 = load i32, i32* @errno
%66 = call i32 @strerror(i32 %65)
%67 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %64, i32 %66)
br label %68
68: ; preds = %63, %59
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [40 x i8] c"socket path %s must be an absolute path\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [27 x i8] c"couldn't create socket: %s\00"
@errno = global i32 0
@.str.2 = [31 x i8] c"couldn't bind to socket %s: %s\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [29 x i8] c"couldn't chmod socket %s: %s\00"
@.str.4 = [33 x i8] c"couldn't listen on socket %s: %s\00"
define i32 @runes_socket_server_open(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = call i32 @runes_socket_populate_sockaddr(i8* %0, %struct.sockaddr_un* %2)
%5 = call i8* @strdup(i8* %0)
%6 = call i8* @strrchr(i8* dereferenceable(1) %5, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %8, %1
store i8 0, i8* %6
%11 = call i32 @runes_mkdir_p(i8* %5)
%12 = call i32 @free(i8* %5)
%13 = call i32 @unlink(i8* %0)
%14 = load i32, i32* @AF_UNIX
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %10
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i8*
%23 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %18, %10
%25 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%26 = call i64 @bind(i32 %16, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %30)
br label %32
32: ; preds = %28, %24
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %34, %33
%36 = call i64 @chmod(i8* %0, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %32
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %40)
br label %42
42: ; preds = %38, %32
%43 = call i64 @listen(i32 %16, i32 5)
%44 = icmp eq i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %42
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 (i8*, i8*, ...) @runes_die(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %47)
br label %49
49: ; preds = %45, %42
ret i32 %16
}