#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ENOENT;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
scalar_t__ unlink(char *);
int listen_socket(char *path) {
  struct sockaddr_un addr;
  int sock;
  if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    do {
      fprintf(stderr, "Exiting.\n");
      exit(255);
    } while (0);
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, path);
  if (unlink(path) < 0) {
    if (errno != ENOENT) {
      perror("unlink");
      do {
        fprintf(stderr, "Exiting.\n");
        exit(255);
      } while (0);
    }
  }
  if (bind(sock, (struct sockaddr *)(&addr), sizeof(addr)) < 0) {
    perror("bind");
    do {
      fprintf(stderr, "Exiting.\n");
      exit(255);
    } while (0);
  }
  if (listen(sock, 5) < 0) {
    perror("listen");
    do {
      fprintf(stderr, "Exiting.\n");
      exit(255);
    } while (0);
  }
  fprintf(stderr, "Accepting connections on %s\n", path);
  return sock;
}


x86 assembly (clang, -O0):
<listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ee <listen_socket+0x5e>
lea    0xe38(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xff,%edi
call   10a0 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    1275 <listen_socket+0xe5>
mov    0x2d90(%rip),%rax
mov    (%rax),%rax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%rax
je     1270 <listen_socket+0xe0>
lea    0xdc7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xff,%edi
call   10a0 <exit@plt>
jmp    1275 <listen_socket+0xe5>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12bd <listen_socket+0x12d>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xd5b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1301 <listen_socket+0x171>
lea    0xd42(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd17(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xff,%edi
call   10a0 <exit@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     124b <listen_socket+0xbb>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    1203 <listen_socket+0x73>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
jne    1266 <listen_socket+0xd6>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1254 <listen_socket+0xc4>
mov    %ebp,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     125d <listen_socket+0xcd>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdae(%rip),%rdi
jmp    126d <listen_socket+0xdd>
lea    0xdbd(%rip),%rdi
jmp    126d <listen_socket+0xdd>
lea    0xdb9(%rip),%rdi
jmp    126d <listen_socket+0xdd>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [10 x i8] c"Exiting.\0A\00"
@AF_UNIX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [7 x i8] c"unlink\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
@.str.5 = [29 x i8] c"Accepting connections on %s\0A\00"
define i32 @listen_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 255)
unreachable
15: ; No predecessors!
br label %16
16: ; preds = %15, %1
%17 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i8*, i8** %2
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = load i8*, i8** %2
%25 = call i64 @unlink(i8* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %16
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENOENT
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 255)
unreachable
37: ; No predecessors!
br label %38
38: ; preds = %37, %27
br label %39
39: ; preds = %38, %16
%40 = load i32, i32* %4
%41 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 8)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32 @exit(i32 255)
unreachable
50: ; No predecessors!
br label %51
51: ; preds = %50, %39
%52 = load i32, i32* %4
%53 = call i64 @listen(i32 %52, i32 5)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %51
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%60 = call i32 @exit(i32 255)
unreachable
61: ; No predecessors!
br label %62
62: ; preds = %61, %51
%63 = load i32, i32* @stderr
%64 = load i8*, i8** %2
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@stderr = global i32 0
@.str.1 = [10 x i8] c"Exiting.\0A\00"
@AF_UNIX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [7 x i8] c"unlink\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"listen\00"
@.str.5 = [29 x i8] c"Accepting connections on %s\0A\00"
define i32 @listen_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @PF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 255)
unreachable
13: ; preds = %1
%14 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strcpy(i32 %18, i8* %0)
%20 = call i64 @unlink(i8* %0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %13
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOENT
%25 = icmp eq i64 %23, %24
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 255)
unreachable
31: ; preds = %22, %13
%32 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%33 = call i64 @bind(i32 %6, %struct.sockaddr* %32, i32 8)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 255)
unreachable
40: ; preds = %31
%41 = call i64 @listen(i32 %6, i32 5)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 255)
unreachable
48: ; preds = %40
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %0)
ret i32 %6
}