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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(void *, char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  int sockfd;
  struct sockaddr_un addr;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd == -1) {
    printf("create socket failed.\n");
    return -1;
  }
  bzero(&addr, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strcpy((void *)&addr.sun_path, "/tmp/unix_socket");
  unlink("/tmp/unix_socket");
  bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  listen(sockfd, 5);
  while (1) {
    int n;
    int client_sockfd;
    client_sockfd = accept(sockfd, NULL, NULL);
    if (client_sockfd == -1) {
      fprintf(stderr, "accept error.\n");
      break;
    }
    do {
      char buf[512];
      n = recv(client_sockfd, buf, 512, 0);
      if (n > 0) {
        buf[n] = '\0';
        printf("recv:\n%s\n", buf);
        n = send(client_sockfd, buf, n, 0);
      }
    } while (n > 0);
    close(client_sockfd);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1219 <main+0x59>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <main+0x16e>
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xdd9(%rip),%rsi
call   1050 <strcpy@plt>
lea    0xdcd(%rip),%rdi
call   1040 <unlink@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12a3 <main+0xe3>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
call   1090 <fprintf@plt>
jmp    131f <main+0x15f>
jmp    12a8 <main+0xe8>
mov    -0x28(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1303 <main+0x143>
movslq -0x24(%rbp),%rax
movb   $0x0,-0x230(%rbp,%rax,1)
lea    -0x230(%rbp),%rsi
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x24(%rbp)
jmp    1308 <main+0x148>
cmpl   $0x0,-0x24(%rbp)
jg     12a8 <main+0xe8>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
jmp    126d <main+0xad>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    0x2e00(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12fe <main+0x12e>
mov    %eax,%r15d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   10d0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
lea    0xdd9(%rip),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
mov    %rbp,%rdi
call   1040 <unlink@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10c0 <bind@plt>
mov    %r15d,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
xor    %r14d,%r14d
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
cmp    $0xffffffff,%eax
je     12df <main+0x10f>
mov    %eax,%ebp
lea    0x10(%rsp),%r13
lea    0xda7(%rip),%r12
jmp    12a0 <main+0xd0>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12df <main+0x10f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    127b <main+0xab>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
test   %eax,%eax
jg     12a0 <main+0xd0>
jmp    127b <main+0xab>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
call   10a0 <fprintf@plt>
mov    %r15d,%edi
call   1090 <close@plt>
jmp    1310 <main+0x140>
lea    0xd25(%rip),%rdi
call   1060 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"create socket failed.\0A\00"
@.str.1 = [17 x i8] c"/tmp/unix_socket\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"accept error.\0A\00"
@.str.3 = [10 x i8] c"recv:\0A%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
%8 = alloca i32
%9 = alloca i32
%10 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%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* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
18: ; preds = %2
%19 = call i32 @bzero(%struct.sockaddr_un* %7, i32 8)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%23 = bitcast i32* %22 to i8*
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 8)
%29 = load i32, i32* %6
%30 = call i32 @listen(i32 %29, i32 5)
br label %31
31: ; preds = %18, %60
%32 = load i32, i32* %6
%33 = call i32 @accept(i32 %32, i32* null, i32* null)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %63
39: ; preds = %31
br label %40
40: ; preds = %57, %39
%41 = load i32, i32* %9
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%43 = call i32 @recv(i32 %41, i8* %42, i32 512, i32 0)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %40
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %48
store i8 0, i8* %49
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = load i32, i32* %9
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%54 = load i32, i32* %8
%55 = call i32 @send(i32 %52, i8* %53, i32 %54, i32 0)
store i32 %55, i32* %8
br label %56
56: ; preds = %46, %40
br label %57
57: ; preds = %56
%58 = load i32, i32* %8
%59 = icmp sgt i32 %58, 0
br i1 %59, label %40, label %60
60: ; preds = %57
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
br label %31
63: ; preds = %36
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
store i32 0, i32* %3
br label %66
66: ; preds = %63, %16
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"/tmp/unix_socket\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"accept error.\0A\00"
@.str.3 = [10 x i8] c"recv:\0A%s\0A\00"
@str = [22 x i8] c"create socket failed.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [512 x i8]
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %45
12: ; preds = %2
%13 = call i32 @bzero(%struct.sockaddr_un* %3, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%19 = call i32 @bind(i32 %8, %struct.sockaddr* %18, i32 8)
%20 = call i32 @listen(i32 %8, i32 5)
%21 = call i32 @accept(i32 %8, i32* null, i32* null)
%22 = icmp eq i32 %21, -1
br i1 %22, label %41, label %23
23: ; preds = %12
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
br label %25
25: ; preds = %23, %37
%26 = phi i32 [ %21, %23 ], [ %39, %37 ]
br label %27
27: ; preds = %25, %31
%28 = call i32 @recv(i32 %26, i8* %24, i32 512, i32 0)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %27
br label %37
31: ; preds = %27
%32 = zext i32 %28 to i64
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%35 = call i32 @send(i32 %26, i8* %24, i32 %28, i32 0)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %27, label %37
37: ; preds = %31, %30
%38 = call i32 @close(i32 %26)
%39 = call i32 @accept(i32 %8, i32* null, i32* null)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %25
41: ; preds = %37, %12
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @close(i32 %8)
br label %45
45: ; preds = %41, %10
%46 = phi i32 [ -1, %10 ], [ 0, %41 ]
ret i32 %46
}