#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 *);
main() {
  int sock, msgsock, rval;
  struct sockaddr_un server;
  char buf[65536];
  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");
      break;
    } else
      do {
        bzero(buf, sizeof(buf));
        if ((rval = read(msgsock, buf, sizeof(buf))) < 0)
          perror("reading stream message");
        else if (rval == 0) {
          printf("Ending connection\n");
          close(msgsock);
          close(sock);
          unlink("socket");
        } else
          ;
      } while (rval > 0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%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   1040 <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    1323 <main+0x173>
jmp    1285 <main+0xd5>
lea    -0x10020(%rbp),%rdi
mov    $0x10000,%esi
call   10b0 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x10020(%rbp),%rsi
mov    $0x10000,%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    130a <main+0x15a>
cmpl   $0x0,-0x10(%rbp)
jne    1300 <main+0x150>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xd2f(%rip),%rdi
call   1030 <unlink@plt>
jmp    1305 <main+0x155>
jmp    1305 <main+0x155>
jmp    130a <main+0x15a>
jmp    130f <main+0x15f>
cmpl   $0x0,-0x10(%rbp)
jg     1285 <main+0xd5>
jmp    131e <main+0x16e>
jmp    1256 <main+0xa6>
mov    -0x4(%rbp),%eax
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    0x2e10(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    11fd <main+0x3d>
lea    0xe08(%rip),%rdi
call   10b0 <perror@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xe1b(%rip),%rsi
call   1040 <strcpy@plt>
mov    %rsp,%rsi
mov    %r13d,%edi
mov    $0x10,%edx
call   10a0 <bind@plt>
test   %rax,%rax
je     1230 <main+0x70>
lea    0xdeb(%rip),%rdi
call   10b0 <perror@plt>
mov    (%rsp),%rsi
lea    0xdf1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
jne    1280 <main+0xc0>
lea    0xdd9(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0xdb9(%rip),%r14
lea    0xdc9(%rip),%r15
lea    0xd89(%rip),%r12
jmp    12d0 <main+0x110>
mov    %r15,%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1260 <main+0xa0>
mov    %rbx,%rdi
mov    $0x10000,%esi
call   10c0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10000,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12f4 <main+0x134>
jne    12d0 <main+0x110>
jmp    129e <main+0xde>
mov    %r14,%rdi
call   10b0 <perror@plt>
jmp    12bd <main+0xfd>

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"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca [65536 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, %66
%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 %67
39: ; preds = %32
br label %40
40: ; preds = %62, %39
%41 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%42 = call i32 @bzero(i8* %41, i32 65536)
%43 = load i32, i32* %3
%44 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 65536)
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 %61
49: ; preds = %40
%50 = load i32, i32* %4
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %49
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* %3
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %2
%57 = call i32 @close(i32 %56)
%58 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %60
59: ; preds = %49
br label %60
60: ; preds = %59, %52
br label %61
61: ; preds = %60, %47
br label %62
62: ; preds = %61
%63 = load i32, i32* %4
%64 = icmp sgt i32 %63, 0
br i1 %64, label %40, label %65
65: ; preds = %62
br label %66
66: ; preds = %65
br label %32
67: ; preds = %37
%68 = load i32, i32* %1
ret i32 %68
}

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 = [18 x i8] c"Ending connection\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [65536 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = getelementptr inbounds [65536 x i8], [65536 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)
%25 = call i32 @accept(i32 %7, i32 0, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %32, label %30
27: ; preds = %38, %42
%28 = call i32 @accept(i32 %7, i32 0, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %32, label %30
30: ; preds = %21, %27
%31 = phi i32 [ %28, %27 ], [ %25, %21 ]
br label %34
32: ; preds = %27, %21
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
34: ; preds = %40, %30
%35 = call i32 @bzero(i8* %4, i32 65536)
%36 = call i32 @read(i32 %31, i8* %4, i32 65536)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %27
40: ; preds = %34
%41 = icmp eq i32 %36, 0
br i1 %41, label %42, label %34
42: ; preds = %40
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%44 = call i32 @close(i32 %31)
%45 = call i32 @close(i32 %7)
%46 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %27
}