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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ handle_client(int);
int listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  int socket_fd, client_fd;
  struct sockaddr_un local_addr, client_addr;
  if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket error");
    perror(strerror(errno));
    return -1;
  }
  local_addr.sun_family = AF_UNIX;
  strcpy(local_addr.sun_path, "ECHO");
  unlink(local_addr.sun_path);
  int len = strlen(local_addr.sun_path) + sizeof(local_addr.sun_family);
  if (-1 == bind(socket_fd, (struct sockaddr *)&local_addr, len)) {
    perror("bind error");
    perror(strerror(errno));
    return -2;
  }
  if (-1 == listen(socket_fd, 10)) {
    perror("listen error");
    perror(strerror(errno));
    return -3;
  }
  printf("unix domain socket start");
  for (;;) {
    memset(client_addr.sun_path, 0, sizeof(client_addr.sun_path));
    len = sizeof(client_addr);
    if (-1 == (client_fd =
                   accept(socket_fd, (struct sockaddr *)&client_addr, &len))) {
      perror("accept error");
      perror(strerror(errno));
      exit(-4);
    }
    printf("client info: path: %s\n", client_addr.sun_path);
    if (0 == fork()) {
      handle_client(client_fd);
      exit(0);
    }
  }
  close(socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1242 <main+0x62>
lea    0xde5(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <main+0x1dd>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   10a0 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12c5 <main+0xe5>
lea    0xd74(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rdi
call   10b0 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13bd <main+0x1dd>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    130f <main+0x12f>
lea    0xd35(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rdi
call   10b0 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    13bd <main+0x1dd>
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
movl   $0x10,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1382 <main+0x1a2>
lea    0xce6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rdi
call   10b0 <perror@plt>
mov    $0xfffffffc,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rsi
lea    0xcc3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    13b8 <main+0x1d8>
mov    -0xc(%rbp),%edi
call   1030 <handle_client@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
jmp    131d <main+0x13d>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e4a(%rip),%r14
mov    (%r14),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
jne    11df <main+0x4f>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1258 <main+0xc8>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
lea    0xe1e(%rip),%rsi
call   1040 <strcpy@plt>
call   1030 <unlink@plt>
call   1050 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0x90>
mov    $0xfffffffe,%ebx
lea    0xdf4(%rip),%rdi
jmp    123d <main+0xad>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1262 <main+0xd2>
mov    $0xfffffffd,%ebx
lea    0xde0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdi
call   1090 <perror@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdc1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"ECHO\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [25 x i8] c"unix domain socket start\00"
@.str.5 = [13 x i8] c"accept error\00"
@.str.6 = [23 x i8] c"client info: path: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @perror(i8* %14)
store i32 -1, i32* %1
br label %77
16: ; preds = %0
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @unlink(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
%29 = add i64 %28, 4
%30 = trunc i64 %29 to i32
store i32 %30, i32* %6
%31 = load i32, i32* %2
%32 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%33 = load i32, i32* %6
%34 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 %33)
%35 = icmp eq i32 -1, %34
br i1 %35, label %36, label %41
36: ; preds = %16
%37 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @perror(i8* %39)
store i32 -2, i32* %1
br label %77
41: ; preds = %16
%42 = load i32, i32* %2
%43 = call i32 @listen(i32 %42, i32 10)
%44 = icmp eq i32 -1, %43
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @perror(i8* %48)
store i32 -3, i32* %1
br label %77
50: ; preds = %41
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %76, %50
%53 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i32 @memset(i8* %54, i32 0, i32 8)
store i32 16, i32* %6
%56 = load i32, i32* %2
%57 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%58 = call i32 @accept(i32 %56, %struct.sockaddr* %57, i32* %6)
store i32 %58, i32* %3
%59 = icmp eq i32 -1, %58
br i1 %59, label %60, label %66
60: ; preds = %52
%61 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 @perror(i8* %63)
%65 = call i32 @exit(i32 -4)
unreachable
66: ; preds = %52
%67 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %68)
%70 = call i64 (...) @fork()
%71 = icmp eq i64 0, %70
br i1 %71, label %72, label %76
72: ; preds = %66
%73 = load i32, i32* %3
%74 = call i32 @handle_client(i32 %73)
%75 = call i32 @exit(i32 0)
unreachable
76: ; preds = %66
br label %52
77: ; preds = %45, %36, %11
%78 = load i32, i32* %1
ret i32 %78
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"ECHO\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [25 x i8] c"unix domain socket start\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @perror(i8* %10)
br label %34
12: ; preds = %0
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @unlink(i8* undef)
%17 = tail call i32 @strlen(i8* undef)
%18 = add i32 %17, 4
%19 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%20 = call i32 @bind(i32 %5, %struct.sockaddr* %19, i32 %18)
%21 = icmp eq i32 %20, -1
br i1 %21, label %27, label %22
22: ; preds = %12
%23 = call i32 @listen(i32 %5, i32 10)
%24 = icmp eq i32 %23, -1
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
unreachable
27: ; preds = %22, %12
%28 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %12 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %22 ]
%29 = phi i32 [ -2, %12 ], [ -3, %22 ]
%30 = call i32 @perror(i8* %28)
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @perror(i8* %32)
br label %34
34: ; preds = %27, %7
%35 = phi i32 [ -1, %7 ], [ %29, %27 ]
ret i32 %35
}