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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
main() {
  int sock, msgsock, rval;
  struct sockaddr_un server;
  char buf[1024];
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("opening stream socket");
  }
  server.sun_family = AF_UNIX;
  strcpy(server.sun_path, "socket");
  if (bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un))) {
    perror("binding stream socket");
  }
  printf("Socket has name %s\n", server.sun_path);
  listen(sock, 5);
  while (1) {
    msgsock = accept(sock, 0, 0);
    if (msgsock == -1)
      perror("accept");
    else
      do {
        bzero(buf, sizeof(buf));
        if ((rval = read(msgsock, buf, 1024)) < 0)
          perror("reading stream message");
        else if (rval == 0)
          printf("Ending connection\n");
        else
          printf("-->%s\n", buf);
        write(sock, "a", sizeof("a"));
      } while (rval > 0);
    close(msgsock);
  }
  close(sock);
  unlink("socket");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f4 <main+0x44>
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1237 <main+0x87>
lea    0xde4(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1280 <main+0xd0>
lea    0xdca(%rip),%rdi
call   10a0 <perror@plt>
jmp    1321 <main+0x171>
jmp    1285 <main+0xd5>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   10b0 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    12c7 <main+0x117>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
jmp    12fe <main+0x14e>
cmpl   $0x0,-0x10(%rbp)
jne    12e4 <main+0x134>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f9 <main+0x149>
lea    -0x420(%rbp),%rsi
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12fe <main+0x14e>
mov    -0x8(%rbp),%edi
lea    0xd70(%rip),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmpl   $0x0,-0x10(%rbp)
jg     1285 <main+0xd5>
jmp    1321 <main+0x171>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
jmp    1256 <main+0xa6>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2e10(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1305 <main+0x145>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xe24(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    1316 <main+0x156>
mov    (%rsp),%rsi
lea    0xe03(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0x10(%rsp),%rbx
lea    0xe1d(%rip),%r12
lea    0xe18(%rip),%r13
lea    0xe08(%rip),%r14
jmp    127f <main+0xbf>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x2,%edx
call   1050 <write@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     12f4 <main+0x134>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12e3 <main+0x123>
test   %eax,%eax
je     1260 <main+0xa0>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x2,%edx
call   1050 <write@plt>
jmp    12a0 <main+0xe0>
lea    0xd5d(%rip),%rdi
call   10b0 <perror@plt>
jmp    1268 <main+0xa8>
lea    0xd45(%rip),%rdi
call   10b0 <perror@plt>
jmp    1277 <main+0xb7>
lea    0xcf4(%rip),%rdi
call   10b0 <perror@plt>
jmp    11f4 <main+0x34>
lea    0xcf9(%rip),%rdi
call   10b0 <perror@plt>
jmp    121e <main+0x5e>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"opening stream socket\00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [22 x i8] c"binding stream socket\00"
@.str.3 = [20 x i8] c"Socket has name %s\0A\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [23 x i8] c"reading stream message\00"
@.str.6 = [19 x i8] c"Ending connection\0A\00"
@.str.7 = [7 x i8] c"-->%s\0A\00"
@.str.8 = [2 x i8] c"a\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca [1024 x i8]
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 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 16)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %14
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = load i32, i32* %2
%31 = call i32 @listen(i32 %30, i32 5)
br label %32
32: ; preds = %26, %65
%33 = load i32, i32* %2
%34 = call i32 @accept(i32 %33, i32 0, i32 0)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %65
39: ; preds = %32
br label %40
40: ; preds = %61, %39
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i32 @bzero(i8* %41, i32 1024)
%43 = load i32, i32* %3
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 1024)
store i32 %45, i32* %4
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %58
49: ; preds = %40
%50 = load i32, i32* %4
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %57
54: ; preds = %49
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %54, %52
br label %58
58: ; preds = %57, %47
%59 = load i32, i32* %2
%60 = call i32 @write(i32 %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 2)
br label %61
61: ; preds = %58
%62 = load i32, i32* %4
%63 = icmp sgt i32 %62, 0
br i1 %63, label %40, label %64
64: ; preds = %61
br label %65
65: ; preds = %64, %37
%66 = load i32, i32* %3
%67 = call i32 @close(i32 %66)
br label %32
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"opening stream socket\00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [22 x i8] c"binding stream socket\00"
@.str.3 = [20 x i8] c"Socket has name %s\0A\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [23 x i8] c"reading stream message\00"
@.str.7 = [7 x i8] c"-->%s\0A\00"
@.str.8 = [2 x i8] c"a\00"
@str = [18 x i8] c"Ending connection\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [1024 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%15 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%17 = call i64 @bind(i32 %7, %struct.sockaddr* %16, i32 16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = load i8*, i8** %14
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = call i32 @listen(i32 %7, i32 5)
br label %25
25: ; preds = %49, %21
%26 = call i32 @accept(i32 %7, i32 0, i32 0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = call i32 @bzero(i8* %4, i32 1024)
%30 = call i32 @read(i32 %26, i8* %4, i32 1024)
%31 = icmp slt i32 %30, 0
br i1 %31, label %34, label %36
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %49
34: ; preds = %43, %28
%35 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %41
36: ; preds = %28, %43
%37 = phi i32 [ %47, %43 ], [ %30, %28 ]
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = call i32 @write(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 2)
br label %49
43: ; preds = %36
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%45 = call i32 @write(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 2)
%46 = call i32 @bzero(i8* %4, i32 1024)
%47 = call i32 @read(i32 %26, i8* %4, i32 1024)
%48 = icmp slt i32 %47, 0
br i1 %48, label %34, label %36
49: ; preds = %41, %32
%50 = call i32 @close(i32 %26)
br label %25
}