#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*/ socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ listen(int, int);
int /*<<< orphan*/ parse_message(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
int listen_on_dom_socket(char *sock_path) {
  int dom_sock, sock_msg, read_val;
  struct sockaddr_un server;
  char buf[1024];
  dom_sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (dom_sock < 0) {
    return dom_sock;
  }
  server.sun_family = AF_UNIX;
  strcpy(server.sun_path, sock_path);
  if (bind(dom_sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un))) {
    printf("clacksidd: Couldn't bind to domain socket\n");
    goto error;
  }
  printf("Domain socket bound: %d\n", dom_sock);
  if (listen(dom_sock, 5) != 0) {
    printf("clacksidd: Couldn't listen on socket: %d\n", errno);
    goto error;
  }
  for (;;) {
    int server_sock_len = sizeof(server);
    bzero(buf, sizeof(buf));
    sock_msg = accept(dom_sock, (struct sockaddr *)&server,
                      (socklen_t *)&server_sock_len);
    if (sock_msg < 0) {
      printf("clacksidd: Couldn't accept incoming connection: %d\n", errno);
      goto error;
    } else
      do {
        if ((read_val = read(sock_msg, buf, 1024)) < 0)
          printf("clacksidd: Couldn't read from dom socket: %d\n", errno);
        else if (read_val == 0)
          parse_message(buf);
      } while (read_val > 0);
    close(sock_msg);
  }
error:
  close(dom_sock);
  unlink(sock_path);
  return -1;
}


x86 assembly (clang, -O0):
<listen_on_dom_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f0 <listen_on_dom_socket+0x40>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <listen_on_dom_socket+0x1bb>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     1236 <listen_on_dom_socket+0x86>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1353 <listen_on_dom_socket+0x1a3>
mov    -0x14(%rbp),%esi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     127a <listen_on_dom_socket+0xca>
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1353 <listen_on_dom_socket+0x1a3>
jmp    127f <listen_on_dom_socket+0xcf>
movl   $0x8,-0x434(%rbp)
lea    -0x430(%rbp),%rdi
mov    $0x400,%esi
call   10b0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x434(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d6 <listen_on_dom_socket+0x126>
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1353 <listen_on_dom_socket+0x1a3>
jmp    12db <listen_on_dom_socket+0x12b>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1317 <listen_on_dom_socket+0x167>
mov    0x2cce(%rip),%rax
mov    (%rax),%esi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1332 <listen_on_dom_socket+0x182>
cmpl   $0x0,-0x1c(%rbp)
jne    132d <listen_on_dom_socket+0x17d>
lea    -0x430(%rbp),%rdi
call   1050 <parse_message@plt>
jmp    1332 <listen_on_dom_socket+0x182>
jmp    1337 <listen_on_dom_socket+0x187>
cmpl   $0x0,-0x1c(%rbp)
jg     12db <listen_on_dom_socket+0x12b>
jmp    1346 <listen_on_dom_socket+0x196>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    127f <listen_on_dom_socket+0xcf>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_on_dom_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
mov    0x2e0d(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     133b <listen_on_dom_socket+0x17b>
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10b0 <bind@plt>
test   %rax,%rax
je     122c <listen_on_dom_socket+0x6c>
lea    0xe83(%rip),%rdi
call   1060 <puts@plt>
jmp    1327 <listen_on_dom_socket+0x167>
lea    0xdcd(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
test   %rax,%rax
je     1262 <listen_on_dom_socket+0xa2>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
lea    0xdbc(%rip),%rdi
jmp    1320 <listen_on_dom_socket+0x160>
movl   $0x8,0x4(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     1310 <listen_on_dom_socket+0x150>
mov    %eax,%r15d
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r12
lea    0x4(%rsp),%r13
jmp    12f0 <listen_on_dom_socket+0x130>
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
lea    0xdc1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
movl   $0x8,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10d0 <accept@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1310 <listen_on_dom_socket+0x150>
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
js     12a6 <listen_on_dom_socket+0xe6>
jne    12f0 <listen_on_dom_socket+0x130>
mov    %rbx,%rdi
call   1050 <parse_message@plt>
jmp    12bd <listen_on_dom_socket+0xfd>
mov    0x2cb9(%rip),%rax
mov    (%rax),%esi
lea    0xd23(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x418,%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_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [43 x i8] c"clacksidd: Couldn't bind to domain socket\0A\00"
@.str.1 = [25 x i8] c"Domain socket bound: %d\0A\00"
@.str.2 = [42 x i8] c"clacksidd: Couldn't listen on socket: %d\0A\00"
@errno = global i32 0
@.str.3 = [52 x i8] c"clacksidd: Couldn't accept incoming connection: %d\0A\00"
@.str.4 = [46 x i8] c"clacksidd: Couldn't read from dom socket: %d\0A\00"
define i32 @listen_on_dom_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
%8 = alloca [1024 x i8]
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i32, i32* %4
store i32 %16, i32* %2
br label %80
17: ; preds = %1
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %75
30: ; preds = %17
%31 = load i32, i32* %4
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %4
%34 = call i64 @listen(i32 %33, i32 5)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i32, i32* @errno
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %37)
br label %75
39: ; preds = %30
br label %40
40: ; preds = %72, %39
store i32 8, i32* %9
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = call i32 @bzero(i8* %41, i32 1024)
%43 = load i32, i32* %4
%44 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %9)
store i32 %45, i32* %5
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* @errno
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %49)
br label %75
51: ; preds = %40
br label %52
52: ; preds = %68, %51
%53 = load i32, i32* %5
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 1024)
store i32 %55, i32* %6
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i32, i32* @errno
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %67
60: ; preds = %52
%61 = load i32, i32* %6
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%65 = call i32 @parse_message(i8* %64)
br label %66
66: ; preds = %63, %60
br label %67
67: ; preds = %66, %57
br label %68
68: ; preds = %67
%69 = load i32, i32* %6
%70 = icmp sgt i32 %69, 0
br i1 %70, label %52, label %71
71: ; preds = %68
br label %72
72: ; preds = %71
%73 = load i32, i32* %5
%74 = call i32 @close(i32 %73)
br label %40
75: ; preds = %48, %36, %28
%76 = load i32, i32* %4
%77 = call i32 @close(i32 %76)
%78 = load i8*, i8** %3
%79 = call i32 @unlink(i8* %78)
store i32 -1, i32* %2
br label %80
80: ; preds = %75, %15
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [25 x i8] c"Domain socket bound: %d\0A\00"
@.str.2 = [42 x i8] c"clacksidd: Couldn't listen on socket: %d\0A\00"
@errno = global i32 0
@.str.3 = [52 x i8] c"clacksidd: Couldn't accept incoming connection: %d\0A\00"
@.str.4 = [46 x i8] c"clacksidd: Couldn't read from dom socket: %d\0A\00"
@str = [42 x i8] c"clacksidd: Couldn't bind to domain socket\00"
define i32 @listen_on_dom_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %55, label %11
11: ; preds = %1
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 @strcpy(i32 undef, i8* %0)
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i64 @bind(i32 %9, %struct.sockaddr* %15, i32 8)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %11
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %52
20: ; preds = %11
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%22 = call i64 @listen(i32 %9, i32 5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = bitcast i32* %4 to i8*
store i32 8, i32* %4
%26 = call i32 @bzero(i8* %6, i32 1024)
%27 = call i32 @accept(i32 %9, %struct.sockaddr* %15, i32* %4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %34, label %32
29: ; preds = %20
%30 = load i32, i32* @errno
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %52
32: ; preds = %24, %47
%33 = phi i32 [ %50, %47 ], [ %27, %24 ]
br label %37
34: ; preds = %47, %24
%35 = load i32, i32* @errno
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %52
37: ; preds = %32, %43
%38 = call i32 @read(i32 %33, i8* %6, i32 1024)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %41)
br label %47
43: ; preds = %37
%44 = icmp eq i32 %38, 0
br i1 %44, label %45, label %37
45: ; preds = %43
%46 = call i32 @parse_message(i8* %6)
br label %47
47: ; preds = %45, %40
%48 = call i32 @close(i32 %33)
store i32 8, i32* %4
%49 = call i32 @bzero(i8* %6, i32 1024)
%50 = call i32 @accept(i32 %9, %struct.sockaddr* %15, i32* %4)
%51 = icmp slt i32 %50, 0
br i1 %51, label %34, label %32
52: ; preds = %34, %29, %18
%53 = call i32 @close(i32 %9)
%54 = call i32 @unlink(i8* %0)
br label %55
55: ; preds = %1, %52
%56 = phi i32 [ -1, %52 ], [ %9, %1 ]
ret i32 %56
}