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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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) {
  struct sockaddr_un local, remote;
  int s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s == -1) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, "unix-stream-server.temp");
  unlink(local.sun_path);
  int len = strlen(local.sun_path) + sizeof(local.sun_family);
  if (bind(s, (struct sockaddr *)&local, len) == -1) {
    perror("bind");
    exit(EXIT_FAILURE);
  }
  if (listen(s, 5) == -1) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  for (;;) {
    printf("Waiting for a connection...\n");
    socklen_t remotesize = sizeof(remote);
    int s2 = accept(s, (struct sockaddr *)&remote, &remotesize);
    if (s2 == -1) {
      perror("accept");
      exit(EXIT_FAILURE);
    }
    printf("Connected.\n");
    int done = 0;
    do {
      char str[100];
      int n = recv(s2, str, 100, 0);
      if (n < 0) {
        perror("recv");
        done = 1;
      } else if (n == 0)
        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,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1222 <main+0x52>
lea    0xdf1(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xdcf(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x10(%rbp),%edi
call   1040 <unlink@plt>
mov    -0x10(%rbp),%edi
call   1060 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1288 <main+0xb8>
lea    0xdaa(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12b8 <main+0xe8>
lea    0xd7f(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    12bd <main+0xed>
lea    0xd67(%rip),%rdi
call   1080 <printf@plt>
movl   $0x8,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1307 <main+0x137>
lea    0xd54(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    0xd41(%rip),%rdi
call   1080 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    135b <main+0x18b>
lea    0xd11(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x2c(%rbp)
jmp    1374 <main+0x1a4>
cmpl   $0x0,-0x94(%rbp)
jne    136f <main+0x19f>
movl   $0x1,-0x2c(%rbp)
jmp    1374 <main+0x1a4>
cmpl   $0x0,-0x2c(%rbp)
jne    13b7 <main+0x1e7>
mov    -0x28(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0x0,%rax
jge    13b2 <main+0x1e2>
lea    0xcba(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x2c(%rbp)
jmp    13b7 <main+0x1e7>
jmp    13bc <main+0x1ec>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    131a <main+0x14a>
mov    -0x28(%rbp),%edi
call   1090 <close@plt>
jmp    12bd <main+0xed>

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    0xdfa(%rip),%rdi
jmp    1291 <main+0xc1>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x1c(%rsp)
lea    0xded(%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    0xddb(%rip),%rdi
jmp    1291 <main+0xc1>
mov    %ebp,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1260 <main+0x90>
lea    0xdc6(%rip),%rdi
jmp    1291 <main+0xc1>
lea    0xdd5(%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    12a4 <main+0xd4>
lea    0xd9a(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    %eax,%r14d
lea    0x20(%rsp),%rbx
lea    0xd7f(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
jmp    1301 <main+0x131>
lea    0xd71(%rip),%rdi
call   10c0 <perror@plt>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
call   1090 <close@plt>
lea    0xd5d(%rip),%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    0xd50(%rip),%rdi
call   1060 <puts@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %r15,%rdi
test   %eax,%eax
js     12c6 <main+0xf6>
cs nopw 0x0(%rax,%rax,1)
test   %eax,%eax
je     12d0 <main+0x100>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
test   %rax,%rax
js     12bf <main+0xef>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    1330 <main+0x160>
mov    %r15,%rdi
jmp    12c6 <main+0xf6>

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

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"
@EXIT_FAILURE = global i32 0
@.str.1 = [24 x i8] c"unix-stream-server.temp\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 %struct.sockaddr_un
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_un* %1 to i8*
%6 = bitcast %struct.sockaddr_un* %2 to i8*
%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 eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @unlink(i32 undef)
%20 = tail call i32 @strlen(i32 undef)
%21 = add i32 %20, 4
%22 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%23 = call i32 @bind(i32 %9, %struct.sockaddr* %22, i32 %21)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %15
%30 = call i32 @listen(i32 %9, i32 5)
%31 = icmp eq i32 %30, -1
br i1 %31, label %40, 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))
%34 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%35 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%36 = call i32 @accept(i32 %9, %struct.sockaddr* %35, i32* %3)
%37 = icmp eq i32 %36, -1
br i1 %37, label %44, label %38
38: ; preds = %32
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
br label %48
40: ; preds = %29
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %65, %32
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %38, %65
%49 = phi i32 [ %36, %38 ], [ %68, %65 ]
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
%51 = call i32 @recv(i32 %49, i8* %39, i32 100, i32 0)
%52 = icmp slt i32 %51, 0
br i1 %52, label %62, label %53
53: ; preds = %48, %59
%54 = phi i32 [ %60, %59 ], [ %51, %48 ]
%55 = icmp eq i32 %54, 0
br i1 %55, label %65, label %56
56: ; preds = %53
%57 = call i64 @send(i32 %49, i8* %39, i32 %54, i32 0)
%58 = icmp slt i64 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = call i32 @recv(i32 %49, i8* %39, i32 100, i32 0)
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %53
62: ; preds = %56, %59, %48
%63 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), %48 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), %59 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %56 ]
%64 = call i32 @perror(i8* %63)
br label %65
65: ; preds = %53, %62
%66 = call i32 @close(i32 %49)
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
store i32 8, i32* %3
%68 = call i32 @accept(i32 %9, %struct.sockaddr* %35, i32* %3)
%69 = icmp eq i32 %68, -1
br i1 %69, label %44, label %48
}