#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*/ myaddr;
typedef int /*<<< orphan*/ clientaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int server_fd, client_fd;
  struct sockaddr_un myaddr;
  struct sockaddr_un clientaddr;
  myaddr.sun_family = AF_UNIX;
  strcpy(myaddr.sun_path, "sundysocketfile");
  int clientaddr_len = sizeof(clientaddr);
  server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (server_fd == -1) {
    perror("socket error");
    exit(1);
  }
  if (bind(server_fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
    perror("bind error");
    exit(1);
  }
  if (listen(server_fd, 20) == -1) {
    perror("listen error");
    exit(1);
  }
  printf("listening...\n");
  client_fd =
      accept(server_fd, (struct sockaddr *)&clientaddr, &clientaddr_len);
  if (client_fd == -1) {
    perror("accept error");
    exit(1);
  }
  write(client_fd, "hello sundy", 11);
  printf("write to client\n");
  close(client_fd);
  close(server_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe42(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x8,-0x1c(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1206 <main+0x66>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1236 <main+0x96>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0x14,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1262 <main+0xc2>
lea    0xdd5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdcc(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12a1 <main+0x101>
lea    0xdb1(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    0xda5(%rip),%rsi
mov    $0xb,%edx
call   1040 <write@plt>
lea    0xda0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e3b(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe46(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x8,0x4(%rsp)
mov    (%rbx),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1265 <main+0xc5>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     126e <main+0xce>
mov    %ebx,%edi
mov    $0x14,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     1277 <main+0xd7>
lea    0xe3c(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     1280 <main+0xe0>
mov    %eax,%ebp
lea    0xe0c(%rip),%rsi
mov    %eax,%edi
mov    $0xb,%edx
call   1050 <write@plt>
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xda4(%rip),%rdi
jmp    1287 <main+0xe7>
lea    0xda8(%rip),%rdi
jmp    1287 <main+0xe7>
lea    0xdaa(%rip),%rdi
jmp    1287 <main+0xe7>
lea    0xdae(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str = [16 x i8] c"sundysocketfile\00"
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@.str.4 = [14 x i8] c"listening...\0A\00"
@.str.5 = [13 x i8] c"accept error\00"
@.str.6 = [12 x i8] c"hello sundy\00"
@.str.7 = [17 x i8] c"write to client\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @strcpy(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 8, i32* %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* %1
%14 = load i32, i32* %1
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i32, i32* %1
%21 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = load i32, i32* %1
%29 = call i32 @listen(i32 %28, i32 20)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* %1
%37 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%38 = call i32 @accept(i32 %36, %struct.sockaddr* %37, i32* %5)
store i32 %38, i32* %2
%39 = load i32, i32* %2
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = load i32, i32* %2
%46 = call i32 @write(i32 %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 11)
%47 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %1
%51 = call i32 @close(i32 %50)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str = [16 x i8] c"sundysocketfile\00"
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [13 x i8] c"listen error\00"
@.str.5 = [13 x i8] c"accept error\00"
@.str.6 = [12 x i8] c"hello sundy\00"
@str = [13 x i8] c"listening...\00"
@str.8 = [16 x i8] c"write to client\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = bitcast %struct.sockaddr_un* %1 to i8*
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %6, i32* %7
%8 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i32 @bind(i32 %12, %struct.sockaddr* %18, i32 8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = call i32 @listen(i32 %12, i32 20)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%32 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%33 = call i32 @accept(i32 %12, %struct.sockaddr* %32, i32* %3)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = call i32 @write(i32 %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 11)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
%41 = call i32 @close(i32 %33)
%42 = call i32 @close(i32 %12)
ret i32 0
}