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

/* 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*/ exit(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int main(void) {
  int s, s2, t, len;
  struct sockaddr_un local, remote;
  char str[100];
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, "echo_socket");
  unlink(local.sun_path);
  len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(s, (struct sockaddr *)&local, len) == -1) {
    perror("bind");
    exit(1);
  }
  if (listen(s, 5) == -1) {
    perror("listen");
    exit(1);
  }
  for (;;) {
    int done, n;
    printf("Waiting for a connection...\n");
    t = sizeof(remote);
    if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
      perror("accept");
      exit(1);
    }
    printf("Connected.\n");
    done = 0;
    do {
      n = recv(s2, str, 100, 0);
      if (n <= 0) {
        if (n < 0)
          perror("recv");
        done = 1;
      }
      if (!done)
        if (send(s2, str, n, 0) < 0) {
          perror("send");
          done = 1;
        }
    } while (!done);
    close(s2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    121d <main+0x4d>
lea    0xdf7(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    0xdcd(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%edi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%edi
call   1060 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xaf>
lea    0xd9c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ab <main+0xdb>
lea    0xd75(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12b0 <main+0xe0>
lea    0xd61(%rip),%rdi
call   1080 <printf@plt>
movl   $0x8,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    12f5 <main+0x125>
lea    0xd4f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd40(%rip),%rdi
call   1080 <printf@plt>
movl   $0x0,-0x94(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0x0,-0x98(%rbp)
jg     1357 <main+0x187>
cmpl   $0x0,-0x98(%rbp)
jge    134d <main+0x17d>
lea    0xd00(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jne    13a0 <main+0x1d0>
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x98(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0x0,%rax
jge    139b <main+0x1cb>
lea    0xcc1(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x94(%rbp)
jmp    13a0 <main+0x1d0>
jmp    13a5 <main+0x1d5>
cmpl   $0x0,-0x94(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    130b <main+0x13b>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
jmp    12b0 <main+0xe0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2e00(%rip),%rbx
mov    (%rbx),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
jne    120b <main+0x3b>
lea    0xdff(%rip),%rdi
jmp    1291 <main+0xc1>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x1c(%rsp)
lea    0xde6(%rip),%rsi
call   1050 <strcpy@plt>
call   1040 <unlink@plt>
call   1070 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1246 <main+0x76>
lea    0xdc8(%rip),%rdi
jmp    1291 <main+0xc1>
mov    %ebp,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1260 <main+0x90>
lea    0xdb3(%rip),%rdi
jmp    1291 <main+0xc1>
lea    0xdc2(%rip),%rdi
call   1060 <puts@plt>
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
jne    12a0 <main+0xd0>
lea    0xd87(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %eax,%r14d
lea    0x20(%rsp),%rbx
lea    0xd7a(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
jmp    12f6 <main+0x126>
nopl   0x0(%rax,%rax,1)
lea    0xd58(%rip),%rdi
js     1341 <main+0x171>
mov    %r14d,%edi
call   1090 <close@plt>
mov    %r15,%rdi
call   1060 <puts@plt>
movl   $0x8,0xc(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10d0 <accept@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     128a <main+0xba>
lea    0xd48(%rip),%rdi
call   1060 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12c0 <main+0xf0>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
test   %rax,%rax
jns    1310 <main+0x140>
lea    0xce3(%rip),%rdi
call   10c0 <perror@plt>
jmp    12c9 <main+0xf9>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [12 x i8] c"echo_socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [29 x i8] c"Waiting for a connection...\0A\00"
@.str.5 = [7 x i8] c"accept\00"
@.str.6 = [12 x i8] c"Connected.\0A\00"
@.str.7 = [5 x i8] c"recv\00"
@.str.8 = [5 x i8] c"send\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca %struct.sockaddr_un
%8 = alloca [100 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strcpy(i32 %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @unlink(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @strlen(i32 %28)
%30 = sext i32 %29 to i64
%31 = add i64 %30, 4
%32 = trunc i64 %31 to i32
store i32 %32, i32* %5
%33 = load i32, i32* %2
%34 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%35 = load i32, i32* %5
%36 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %18
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %18
%42 = load i32, i32* %2
%43 = call i32 @listen(i32 %42, i32 5)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %41
br label %49
49: ; preds = %89, %48
%50 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 8, i32* %4
%51 = load i32, i32* %2
%52 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%53 = call i32 @accept(i32 %51, %struct.sockaddr* %52, i32* %4)
store i32 %53, i32* %3
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %49
%59 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %9
br label %60
60: ; preds = %85, %58
%61 = load i32, i32* %3
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%63 = call i32 @recv(i32 %61, i8* %62, i32 100, i32 0)
store i32 %63, i32* %10
%64 = load i32, i32* %10
%65 = icmp sle i32 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %60
%67 = load i32, i32* %10
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %71
71: ; preds = %69, %66
store i32 1, i32* %9
br label %72
72: ; preds = %71, %60
%73 = load i32, i32* %9
%74 = icmp ne i32 %73, 0
br i1 %74, label %84, label %75
75: ; preds = %72
%76 = load i32, i32* %3
%77 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%78 = load i32, i32* %10
%79 = call i64 @send(i32 %76, i8* %77, i32 %78, i32 0)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %75
%82 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %9
br label %83
83: ; preds = %81, %75
br label %84
84: ; preds = %83, %72
br label %85
85: ; preds = %84
%86 = load i32, i32* %9
%87 = icmp ne i32 %86, 0
%88 = xor i1 %87, true
br i1 %88, label %60, label %89
89: ; preds = %85
%90 = load i32, i32* %3
%91 = call i32 @close(i32 %90)
br label %49
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [12 x i8] c"echo_socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.5 = [7 x i8] c"accept\00"
@.str.7 = [5 x i8] c"recv\00"
@.str.8 = [5 x i8] c"send\00"
@str = [28 x i8] c"Waiting for a connection...\00"
@str.9 = [11 x i8] c"Connected.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_un
%4 = alloca [100 x i8]
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.sockaddr_un* %2 to i8*
%7 = bitcast %struct.sockaddr_un* %3 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @unlink(i32 undef)
%21 = tail call i32 @strlen(i32 undef)
%22 = add i32 %21, 4
%23 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%24 = call i32 @bind(i32 %11, %struct.sockaddr* %23, i32 %22)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %16
%30 = call i32 @listen(i32 %11, i32 5)
%31 = icmp eq i32 %30, -1
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
store i32 8, i32* %1
%34 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%35 = call i32 @accept(i32 %11, %struct.sockaddr* %34, i32* %1)
%36 = icmp eq i32 %35, -1
br i1 %36, label %40, label %43
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %57, %32
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32, %57
%44 = phi i32 [ %60, %57 ], [ %35, %32 ]
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
br label %46
46: ; preds = %51, %43
%47 = call i32 @recv(i32 %44, i8* %8, i32 100, i32 0)
%48 = icmp slt i32 %47, 1
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = icmp slt i32 %47, 0
br i1 %50, label %54, label %57
51: ; preds = %46
%52 = call i64 @send(i32 %44, i8* %8, i32 %47, i32 0)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %46
54: ; preds = %51, %49
%55 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), %49 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %51 ]
%56 = call i32 @perror(i8* %55)
br label %57
57: ; preds = %54, %49
%58 = call i32 @close(i32 %44)
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
store i32 8, i32* %1
%60 = call i32 @accept(i32 %11, %struct.sockaddr* %34, i32* %1)
%61 = icmp eq i32 %60, -1
br i1 %61, label %40, label %43
}