#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 {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int eslib_file_exists(char *);
scalar_t__ eslib_file_mkfile(char *, int);
scalar_t__ eslib_sock_setnonblock(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int strnlen(char *, int);
scalar_t__ unlink(char *);
int eslib_sock_create_passive(char *path, int backlog) {
  int r;
  int sock;
  struct sockaddr_un addr;
  if (path == NULL)
    return -1;
  if (strnlen(path, sizeof(addr.sun_path)) >= sizeof(addr.sun_path) - 1) {
    printf("path too long\n");
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
  path = addr.sun_path;
  r = eslib_file_exists(path);
  if (r == 0) {
    printf("file does not exist, will create: %s \n", path);
    if (eslib_file_mkfile(path, 0755))
      printf("mkfile error\n");
  } else if (r == -1)
    return -1;
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1)
    return -1;
  if (eslib_sock_setnonblock(sock))
    goto fail;
  if (unlink(path)) {
    printf("error removing socket file: %s\n", strerror(errno));
    goto fail;
  }
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr))) {
    printf("bind(): %s\n", strerror(errno));
    goto fail;
  }
  if (listen(sock, backlog)) {
    printf("listen(): %s\n", strerror(errno));
    goto fail;
  }
  chmod(path, 0766);
  return sock;
fail:
  close(sock);
  return -1;
}


x86 assembly (clang, -O0):
<eslib_sock_create_passive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1206 <eslib_sock_create_passive+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fe <eslib_sock_create_passive+0x21e>
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1080 <strnlen@plt>
cltq
cmp    $0x7,%rax
jb     123a <eslib_sock_create_passive+0x5a>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fe <eslib_sock_create_passive+0x21e>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <eslib_file_exists@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    12c3 <eslib_sock_create_passive+0xe3>
mov    -0x10(%rbp),%rsi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1ed,%esi
call   1050 <eslib_file_mkfile@plt>
cmp    $0x0,%rax
je     12be <eslib_sock_create_passive+0xde>
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12de <eslib_sock_create_passive+0xfe>
cmpl   $0xffffffff,-0x18(%rbp)
jne    12d9 <eslib_sock_create_passive+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fe <eslib_sock_create_passive+0x21e>
jmp    12de <eslib_sock_create_passive+0xfe>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
mov    0x2cea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1310 <eslib_sock_create_passive+0x130>
movl   $0xffffffff,-0x4(%rbp)
jmp    13fe <eslib_sock_create_passive+0x21e>
mov    -0x1c(%rbp),%edi
call   10c0 <eslib_sock_setnonblock@plt>
cmp    $0x0,%rax
je     1327 <eslib_sock_create_passive+0x147>
jmp    13ef <eslib_sock_create_passive+0x20f>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     135e <eslib_sock_create_passive+0x17e>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xcf2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13ef <eslib_sock_create_passive+0x20f>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   10e0 <bind@plt>
cmp    $0x0,%rax
je     139d <eslib_sock_create_passive+0x1bd>
mov    0x2c50(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xcd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13ef <eslib_sock_create_passive+0x20f>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
je     13d6 <eslib_sock_create_passive+0x1f6>
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xca6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13ef <eslib_sock_create_passive+0x20f>
mov    -0x10(%rbp),%rdi
mov    $0x1f6,%esi
call   10d0 <chmod@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13fe <eslib_sock_create_passive+0x21e>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_sock_create_passive>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     1343 <eslib_sock_create_passive+0x153>
mov    %esi,%r14d
mov    %rdi,%rbp
mov    $0x8,%esi
call   1090 <strnlen@plt>
cmp    $0x7,%eax
jb     122e <eslib_sock_create_passive+0x3e>
lea    0xe4a(%rip),%rdi
call   1050 <puts@plt>
jmp    1343 <eslib_sock_create_passive+0x153>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
mov    0x2da2(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   10b0 <eslib_file_exists@plt>
cmp    $0xffffffff,%eax
je     12e5 <eslib_sock_create_passive+0xf5>
test   %eax,%eax
jne    12a4 <eslib_sock_create_passive+0xb4>
lea    0xd84(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
mov    $0x1ed,%esi
call   1060 <eslib_file_mkfile@plt>
test   %rax,%rax
je     12a4 <eslib_sock_create_passive+0xb4>
lea    0xdc2(%rip),%rdi
call   1050 <puts@plt>
mov    (%r15),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
je     1343 <eslib_sock_create_passive+0x153>
mov    %eax,%r15d
mov    %eax,%edi
call   10d0 <eslib_sock_setnonblock@plt>
test   %rax,%rax
jne    133b <eslib_sock_create_passive+0x14b>
mov    %rbp,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     12e9 <eslib_sock_create_passive+0xf9>
lea    0xd44(%rip),%rbp
jmp    1320 <eslib_sock_create_passive+0x130>
mov    %eax,%ebx
jmp    1343 <eslib_sock_create_passive+0x153>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0x10,%edx
call   10f0 <bind@plt>
test   %rax,%rax
je     1309 <eslib_sock_create_passive+0x119>
lea    0xd40(%rip),%rbp
jmp    1320 <eslib_sock_create_passive+0x130>
mov    %r15d,%edi
mov    %r14d,%esi
call   10c0 <listen@plt>
test   %rax,%rax
je     1350 <eslib_sock_create_passive+0x160>
lea    0xd33(%rip),%rbp
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
mov    $0x1f6,%esi
call   10e0 <chmod@plt>
mov    %r15d,%ebx
jmp    1343 <eslib_sock_create_passive+0x153>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"path too long\0A\00"
@AF_UNIX = global i32 0
@.str.1 = [39 x i8] c"file does not exist, will create: %s \0A\00"
@.str.2 = [14 x i8] c"mkfile error\0A\00"
@SOCK_STREAM = global i32 0
@.str.3 = [32 x i8] c"error removing socket file: %s\0A\00"
@errno = global i32 0
@.str.4 = [12 x i8] c"bind(): %s\0A\00"
@.str.5 = [14 x i8] c"listen(): %s\0A\00"
define i32 @eslib_sock_create_passive(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %92
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @strnlen(i8* %13, i32 8)
%15 = sext i32 %14 to i64
%16 = icmp uge i64 %15, 7
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
19: ; preds = %12
%20 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 16)
%21 = load i32, i32* @AF_UNIX
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = load i8*, i8** %4
%26 = call i32 @strncpy(i8* %24, i8* %25, i32 7)
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%28 = load i8*, i8** %27
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = call i32 @eslib_file_exists(i8* %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %19
%34 = load i8*, i8** %4
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %4
%37 = call i64 @eslib_file_mkfile(i8* %36, i32 493)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
br label %47
42: ; preds = %19
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 -1, i32* %3
br label %92
46: ; preds = %42
br label %47
47: ; preds = %46, %41
%48 = load i32, i32* @AF_UNIX
%49 = load i32, i32* @SOCK_STREAM
%50 = call i32 @socket(i32 %48, i32 %49, i32 0)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %47
store i32 -1, i32* %3
br label %92
54: ; preds = %47
%55 = load i32, i32* %7
%56 = call i64 @eslib_sock_setnonblock(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %54
br label %89
59: ; preds = %54
%60 = load i8*, i8** %4
%61 = call i64 @unlink(i8* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %89
67: ; preds = %59
%68 = load i32, i32* %7
%69 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%70 = call i64 @bind(i32 %68, %struct.sockaddr* %69, i32 16)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %67
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %74)
br label %89
76: ; preds = %67
%77 = load i32, i32* %7
%78 = load i32, i32* %5
%79 = call i64 @listen(i32 %77, i32 %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %85
81: ; preds = %76
%82 = load i32, i32* @errno
%83 = call i8* @strerror(i32 %82)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %83)
br label %89
85: ; preds = %76
%86 = load i8*, i8** %4
%87 = call i32 @chmod(i8* %86, i32 502)
%88 = load i32, i32* %7
store i32 %88, i32* %3
br label %92
89: ; preds = %81, %72, %63, %58
%90 = load i32, i32* %7
%91 = call i32 @close(i32 %90)
store i32 -1, i32* %3
br label %92
92: ; preds = %89, %85, %53, %45, %17, %11
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str.1 = [39 x i8] c"file does not exist, will create: %s \0A\00"
@SOCK_STREAM = global i32 0
@.str.3 = [32 x i8] c"error removing socket file: %s\0A\00"
@errno = global i32 0
@.str.4 = [12 x i8] c"bind(): %s\0A\00"
@.str.5 = [14 x i8] c"listen(): %s\0A\00"
@str = [13 x i8] c"mkfile error\00"
@str.6 = [14 x i8] c"path too long\00"
define i32 @eslib_sock_create_passive(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_un
%4 = bitcast %struct.sockaddr_un* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %53, label %6
6: ; preds = %2
%7 = tail call i32 @strnlen(i8* %0, i32 8)
%8 = icmp ugt i32 %7, 6
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %53
11: ; preds = %6
%12 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 16)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @strncpy(i8* %16, i8* %0, i32 7)
%18 = load i8*, i8** %15
%19 = call i32 @eslib_file_exists(i8* %18)
switch i32 %19, label %26 [
i32 0, label %20
i32 -1, label %53
]
20: ; preds = %11
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%22 = call i64 @eslib_file_mkfile(i8* %18, i32 493)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %11, %20, %24
%27 = load i32, i32* @AF_UNIX
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %53, label %31
31: ; preds = %26
%32 = call i64 @eslib_sock_setnonblock(i32 %29)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %51
34: ; preds = %31
%35 = call i64 @unlink(i8* %18)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%39 = call i64 @bind(i32 %29, %struct.sockaddr* %38, i32 16)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = call i64 @listen(i32 %29, i32 %1)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @chmod(i8* %18, i32 502)
br label %53
46: ; preds = %41, %37, %34
%47 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), %34 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), %37 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), %41 ]
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %47, i8* %49)
br label %51
51: ; preds = %46, %31
%52 = call i32 @close(i32 %29)
br label %53
53: ; preds = %26, %11, %2, %51, %44, %9
%54 = phi i32 [ -1, %9 ], [ -1, %51 ], [ %29, %44 ], [ -1, %2 ], [ %19, %11 ], [ -1, %26 ]
ret i32 %54
}