#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;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
scalar_t__ access(char *, int /*<<< orphan*/);
int bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
scalar_t__ unlink(char *);
int prepare_srv_socket(char *path) {
  static struct sockaddr_un sa;
  int ret = 0;
  if (0 == access(path, F_OK)) {
    if (0 != unlink(path)) {
      printf("Cannot delete outdated socket fine: %s", path);
      return 1;
    }
  }
  if (strlen(path) >= sizeof(sa.sun_path)) {
    printf("UNIX socket path is too long: %lu, max %lu",
           (unsigned long)strlen(path), (unsigned long)sizeof(sa.sun_path) - 1);
    return 1;
  }
  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    printf("Cannot create UNIX socket");
    return 1;
  }
  memset(&sa, 0, sizeof(sa));
  sa.sun_family = AF_UNIX;
  strcpy(sa.sun_path, path);
  if (ret = bind(fd, (struct sockaddr *)&sa, SUN_LEN(&sa))) {
    printf("Cannot bind() UNIX socket %s", path);
    goto err_fd;
  }
  if ((ret = listen(fd, 64))) {
    printf("Cannot listen(%d, 64) UNIX socket %s", fd, path);
    goto err_bind;
  }
  return fd;
err_bind:
  unlink(path);
err_fd:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<prepare_srv_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    121d <prepare_srv_socket+0x6d>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1218 <prepare_srv_socket+0x68>
mov    -0x10(%rbp),%rsi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <prepare_srv_socket+0x1b9>
jmp    121d <prepare_srv_socket+0x6d>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     125d <prepare_srv_socket+0xad>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
movslq %eax,%rsi
lea    0xde2(%rip),%rdi
mov    $0x3,%edx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <prepare_srv_socket+0x1b9>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    129d <prepare_srv_socket+0xed>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <prepare_srv_socket+0x1b9>
lea    0x2dd8(%rip),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2dc1(%rip)
mov    0x2db7(%rip),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    0x2da1(%rip),%rdi
call   10c0 <SUN_LEN@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0x2d90(%rip),%rsi
call   10a0 <bind@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1314 <prepare_srv_socket+0x164>
mov    -0x10(%rbp),%rsi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    135b <prepare_srv_socket+0x1ab>
mov    -0x18(%rbp),%edi
mov    $0x40,%esi
call   1090 <listen@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1347 <prepare_srv_socket+0x197>
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1352 <prepare_srv_socket+0x1a2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1369 <prepare_srv_socket+0x1b9>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_srv_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
test   %rax,%rax
je     1285 <prepare_srv_socket+0xd5>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jb     1206 <prepare_srv_socket+0x56>
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%rsi
lea    0xe37(%rip),%rdi
mov    $0x3,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    12fb <prepare_srv_socket+0x14b>
mov    0x2ddb(%rip),%r15
mov    (%r15),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12ae <prepare_srv_socket+0xfe>
mov    %eax,%ebp
lea    0x2e4b(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x2e37(%rip)
mov    0x2e2d(%rip),%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   10c0 <SUN_LEN@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   10a0 <bind@plt>
test   %eax,%eax
je     12c3 <prepare_srv_socket+0x113>
mov    %eax,%r14d
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12f1 <prepare_srv_socket+0x141>
mov    %rbx,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     11d1 <prepare_srv_socket+0x21>
lea    0xd63(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    12fb <prepare_srv_socket+0x14b>
lea    0xd9d(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    12fb <prepare_srv_socket+0x14b>
mov    %ebp,%edi
mov    $0x40,%esi
call   1090 <listen@plt>
test   %eax,%eax
je     12fb <prepare_srv_socket+0x14b>
mov    %eax,%r14d
lea    0xdac(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@prepare_srv_socket.sa = internal global %struct.sockaddr_un zeroinitializer
@F_OK = global i32 0
@.str = [39 x i8] c"Cannot delete outdated socket fine: %s\00"
@.str.1 = [43 x i8] c"UNIX socket path is too long: %lu, max %lu\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [26 x i8] c"Cannot create UNIX socket\00"
@.str.3 = [29 x i8] c"Cannot bind() UNIX socket %s\00"
@.str.4 = [37 x i8] c"Cannot listen(%d, 64) UNIX socket %s\00"
define i32 @prepare_srv_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%6 = load i8*, i8** %3
%7 = load i32, i32* @F_OK
%8 = call i64 @access(i8* %6, i32 %7)
%9 = icmp eq i64 0, %8
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @unlink(i8* %11)
%13 = icmp ne i64 0, %12
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %2
br label %66
17: ; preds = %10
br label %18
18: ; preds = %17, %1
%19 = load i8*, i8** %3
%20 = call i32 @strlen(i8* %19)
%21 = sext i32 %20 to i64
%22 = icmp uge i64 %21, 4
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i64 %26, i64 3)
store i32 1, i32* %2
br label %66
28: ; preds = %18
%29 = load i32, i32* @AF_UNIX
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %66
36: ; preds = %28
%37 = call i32 @memset(%struct.sockaddr_un* @prepare_srv_socket.sa, i32 0, i32 8)
%38 = load i32, i32* @AF_UNIX
store i32 %38, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @prepare_srv_socket.sa, i32 0, i32 1)
%39 = load i32, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @prepare_srv_socket.sa, i32 0, i32 0)
%40 = load i8*, i8** %3
%41 = call i32 @strcpy(i32 %39, i8* %40)
%42 = load i32, i32* %5
%43 = call i32 @SUN_LEN(%struct.sockaddr_un* @prepare_srv_socket.sa)
%44 = call i32 @bind(i32 %42, %struct.sockaddr* bitcast (%struct.sockaddr_un* @prepare_srv_socket.sa to %struct.sockaddr*), i32 %43)
store i32 %44, i32* %4
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %36
%47 = load i8*, i8** %3
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %47)
br label %62
49: ; preds = %36
%50 = load i32, i32* %5
%51 = call i32 @listen(i32 %50, i32 64)
store i32 %51, i32* %4
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i32, i32* %5
%55 = load i8*, i8** %3
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %54, i8* %55)
br label %59
57: ; preds = %49
%58 = load i32, i32* %5
store i32 %58, i32* %2
br label %66
59: ; preds = %53
%60 = load i8*, i8** %3
%61 = call i64 @unlink(i8* %60)
br label %62
62: ; preds = %59, %46
%63 = load i32, i32* %5
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %4
store i32 %65, i32* %2
br label %66
66: ; preds = %62, %57, %34, %23, %14
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@prepare_srv_socket.sa = internal global %struct.sockaddr_un zeroinitializer
@F_OK = global i32 0
@.str = [39 x i8] c"Cannot delete outdated socket fine: %s\00"
@.str.1 = [43 x i8] c"UNIX socket path is too long: %lu, max %lu\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [26 x i8] c"Cannot create UNIX socket\00"
@.str.3 = [29 x i8] c"Cannot bind() UNIX socket %s\00"
@.str.4 = [37 x i8] c"Cannot listen(%d, 64) UNIX socket %s\00"
define i32 @prepare_srv_socket(i8* %0) {
%2 = load i32, i32* @F_OK
%3 = tail call i64 @access(i8* %0, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = tail call i64 @unlink(i8* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %43
10: ; preds = %5, %1
%11 = tail call i32 @strlen(i8* %0)
%12 = icmp ugt i32 %11, 3
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @strlen(i8* %0)
%15 = sext i32 %14 to i64
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i64 %15, i64 3)
br label %43
17: ; preds = %10
%18 = load i32, i32* @AF_UNIX
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %43
24: ; preds = %17
%25 = tail call i32 @memset(%struct.sockaddr_un* @prepare_srv_socket.sa, i32 0, i32 8)
%26 = load i32, i32* @AF_UNIX
store i32 %26, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @prepare_srv_socket.sa, i64 0, i32 1)
%27 = load i32, i32* getelementptr inbounds (%struct.sockaddr_un, %struct.sockaddr_un* @prepare_srv_socket.sa, i64 0, i32 0)
%28 = tail call i32 @strcpy(i32 %27, i8* %0)
%29 = tail call i32 @SUN_LEN(%struct.sockaddr_un* @prepare_srv_socket.sa)
%30 = tail call i32 @bind(i32 %20, %struct.sockaddr* bitcast (%struct.sockaddr_un* @prepare_srv_socket.sa to %struct.sockaddr*), i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %40
34: ; preds = %24
%35 = tail call i32 @listen(i32 %20, i32 64)
%36 = icmp eq i32 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %20, i8* %0)
%39 = tail call i64 @unlink(i8* %0)
br label %40
40: ; preds = %37, %32
%41 = phi i32 [ %30, %32 ], [ %35, %37 ]
%42 = tail call i32 @close(i32 %20)
br label %43
43: ; preds = %22, %40, %34, %13, %8
%44 = phi i32 [ 1, %8 ], [ 1, %13 ], [ 1, %22 ], [ %41, %40 ], [ %20, %34 ]
ret i32 %44
}