#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*/ serv;
typedef int /*<<< orphan*/ cli;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int read_stream(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
int main() {
  struct sockaddr_un serv, cli;
  char request[400], reply[400];
  int listenfd, sockfd, n, clilen;
  if ((listenfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
    err_sys("socket error");
  memset(&serv, 0, sizeof(serv));
  serv.sun_family = AF_UNIX;
  strncpy(serv.sun_path, "/tmp/tcp3.serv", sizeof(serv.sun_path));
  if (bind(listenfd, (struct sockaddr *)&serv, sizeof(serv)) < 0)
    err_sys("bind error");
  if (listen(listenfd, SOMAXCONN) < 0)
    err_sys("listen error");
  for (;;) {
    clilen = sizeof(cli);
    if ((sockfd = accept(listenfd, (struct sockaddr *)&cli, &clilen)) < 0)
      err_sys("accept error");
    if ((n = read_stream(sockfd, request, 400)) < 0)
      err_sys("read_stream error");
    strncpy(reply, "Hello, Unix domain client!\n", 400);
    if (write(sockfd, reply, 400) != 400)
      err_sys("write error");
    close(sockfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x360,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x354(%rbp)
cmp    $0x0,%eax
jge    11e6 <main+0x46>
lea    0xe1f(%rip),%rdi
call   1040 <err_sys@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x354(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1241 <main+0xa1>
lea    0xde0(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x354(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    126b <main+0xcb>
lea    0xdc1(%rip),%rdi
call   1040 <err_sys@plt>
jmp    1270 <main+0xd0>
movl   $0x10,-0x360(%rbp)
mov    -0x354(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x360(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x358(%rbp)
cmp    $0x0,%eax
jge    12ab <main+0x10b>
lea    0xd8e(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x358(%rbp),%edi
lea    -0x1c0(%rbp),%rsi
mov    $0x190,%edx
call   1080 <read_stream@plt>
mov    %eax,-0x35c(%rbp)
cmp    $0x0,%eax
jge    12dd <main+0x13d>
lea    0xd69(%rip),%rdi
call   1040 <err_sys@plt>
lea    -0x350(%rbp),%rdi
lea    0xd68(%rip),%rsi
mov    $0x190,%edx
call   1030 <strncpy@plt>
mov    -0x358(%rbp),%edi
lea    -0x350(%rbp),%rsi
mov    $0x190,%edx
call   1050 <write@plt>
cmp    $0x190,%eax
je     1323 <main+0x183>
lea    0xd51(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x358(%rbp),%edi
call   1070 <close@plt>
jmp    1270 <main+0xd0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x358,%rsp
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11dc <main+0x3c>
lea    0xe29(%rip),%rdi
call   1040 <err_sys@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x10(%rsp),%rdi
lea    0xe04(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1233 <main+0x93>
lea    0xdee(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <listen@plt>
test   %rax,%rax
jns    1254 <main+0xb4>
lea    0xdd8(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xc(%rsp),%r15
lea    0x1c0(%rsp),%r13
lea    0xdeb(%rip),%r12
lea    0x30(%rsp),%rbp
jmp    1278 <main+0xd8>
nop
mov    %r14d,%edi
call   1070 <close@plt>
movl   $0x10,0xc(%rsp)
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    12a2 <main+0x102>
lea    0xd97(%rip),%rdi
call   1040 <err_sys@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x190,%edx
call   1080 <read_stream@plt>
test   %eax,%eax
jns    12c2 <main+0x122>
lea    0xd84(%rip),%rdi
call   1040 <err_sys@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x190,%edx
call   1030 <strncpy@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x190,%edx
call   1050 <write@plt>
cmp    $0x190,%eax
je     1270 <main+0xd0>
lea    0xd7f(%rip),%rdi
call   1040 <err_sys@plt>
jmp    1270 <main+0xd0>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@AF_UNIX = global i32 0
@.str.1 = [15 x i8] c"/tmp/tcp3.serv\00"
@.str.2 = [11 x i8] c"bind error\00"
@SOMAXCONN = global i32 0
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [13 x i8] c"accept error\00"
@.str.5 = [18 x i8] c"read_stream error\00"
@.str.6 = [28 x i8] c"Hello, Unix domain client!\0A\00"
@.str.7 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_un
%4 = alloca [400 x i8]
%5 = alloca [400 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = load i32, i32* @PF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strncpy(i8* %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 8)
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 16)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %16
%30 = load i32, i32* %6
%31 = load i32, i32* @SOMAXCONN
%32 = call i64 @listen(i32 %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
br label %37
37: ; preds = %60, %36
store i32 16, i32* %9
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%40 = call i32 @accept(i32 %38, %struct.sockaddr* %39, i32* %9)
store i32 %40, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = load i32, i32* %7
%46 = getelementptr inbounds [400 x i8], [400 x i8]* %4, i64 0, i64 0
%47 = call i32 @read_stream(i32 %45, i8* %46, i32 400)
store i32 %47, i32* %8
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %44
%52 = getelementptr inbounds [400 x i8], [400 x i8]* %5, i64 0, i64 0
%53 = call i32 @strncpy(i8* %52, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 400)
%54 = load i32, i32* %7
%55 = getelementptr inbounds [400 x i8], [400 x i8]* %5, i64 0, i64 0
%56 = call i32 @write(i32 %54, i8* %55, i32 400)
%57 = icmp ne i32 %56, 400
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %60
60: ; preds = %58, %51
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
br label %37
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@AF_UNIX = global i32 0
@.str.1 = [15 x i8] c"/tmp/tcp3.serv\00"
@.str.2 = [11 x i8] c"bind error\00"
@SOMAXCONN = global i32 0
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [13 x i8] c"accept error\00"
@.str.5 = [18 x i8] c"read_stream error\00"
@.str.6 = [28 x i8] c"Hello, Unix domain client!\0A\00"
@.str.7 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca %struct.sockaddr_un
%3 = alloca [400 x i8]
%4 = alloca [400 x i8]
%5 = alloca i32
%6 = bitcast %struct.sockaddr_un* %1 to i8*
%7 = bitcast %struct.sockaddr_un* %2 to i8*
%8 = getelementptr inbounds [400 x i8], [400 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [400 x i8], [400 x i8]* %4, i64 0, i64 0
%10 = bitcast i32* %5 to i8*
%11 = load i32, i32* @PF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 16)
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strncpy(i8* %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 8)
%24 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%25 = call i64 @bind(i32 %13, %struct.sockaddr* %24, i32 16)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %17
%30 = load i32, i32* @SOMAXCONN
%31 = call i64 @listen(i32 %13, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
br label %37
37: ; preds = %53, %35
store i32 16, i32* %5
%38 = call i32 @accept(i32 %13, %struct.sockaddr* %36, i32* %5)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = call i32 @read_stream(i32 %38, i8* %8, i32 400)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = call i32 @strncpy(i8* %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 400)
%49 = call i32 @write(i32 %38, i8* %9, i32 400)
%50 = icmp eq i32 %49, 400
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = call i32 @close(i32 %38)
br label %37
}