#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*/ server_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int server_sockfd, client_sockfd;
  int server_len, client_len;
  struct sockaddr_un server_addr;
  struct sockaddr_un client_addr;
  unlink("server_socket");
  server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  server_addr.sun_family = AF_UNIX;
  strcpy(server_addr.sun_path, "server_socket");
  server_len = sizeof(server_addr);
  bind(server_sockfd, (struct sockaddr *)&server_addr, server_len);
  listen(server_sockfd, 5);
  while (1) {
    char ch;
    printf("Server waiting\n");
    client_len = sizeof(client_addr);
    client_sockfd =
        accept(server_sockfd, (struct sockaddr *)&client_addr, &client_len);
    if (client_sockfd == -1) {
      perror("accept failed");
      return -1;
    }
    read(client_sockfd, &ch, 1);
    ch++;
    write(client_sockfd, &ch, 1);
    close(client_sockfd);
  }
  unlink("server_socket");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    0xe03(%rip),%rsi
call   1040 <strcpy@plt>
movl   $0x8,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   10a0 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0xde2(%rip),%rdi
call   1060 <printf@plt>
movl   $0x8,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    126c <main+0xbc>
lea    0xdc2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0xc(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    -0x29(%rbp),%al
add    $0x1,%al
mov    %al,-0x29(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    1225 <main+0x75>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe3b(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e14(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r13d
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0x8,%edx
call   10a0 <bind@plt>
mov    %r13d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0xdfd(%rip),%rdi
call   1050 <puts@plt>
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r13d,%edi
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     12af <main+0xff>
mov    %eax,%ebp
lea    0x3(%rsp),%rbx
lea    0xdcb(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
addb   $0x1,0x3(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14,%rdi
call   1050 <puts@plt>
movl   $0x8,0x4(%rsp)
mov    %r13d,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1260 <main+0xb0>
lea    0xd58(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%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 }
@.str = [14 x i8] c"server_socket\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"Server waiting\0A\00"
@.str.2 = [14 x i8] c"accept failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca %struct.sockaddr_un
%8 = alloca i8
store i32 0, i32* %1
%9 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 8, i32* %4
%18 = load i32, i32* %2
%19 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%20 = load i32, i32* %4
%21 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @listen(i32 %22, i32 5)
br label %24
24: ; preds = %0, %33
%25 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 8, i32* %5
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%28 = call i32 @accept(i32 %26, %struct.sockaddr* %27, i32* %5)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
ret i32 -1
33: ; preds = %24
%34 = load i32, i32* %3
%35 = call i32 @read(i32 %34, i8* %8, i32 1)
%36 = load i8, i8* %8
%37 = add i8 %36, 1
store i8 %37, i8* %8
%38 = load i32, i32* %3
%39 = call i32 @write(i32 %38, i8* %8, i32 1)
%40 = load i32, i32* %3
%41 = call i32 @close(i32 %40)
br label %24
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"server_socket\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [14 x i8] c"accept failed\00"
@str = [15 x i8] c"Server waiting\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_un
%4 = alloca i8
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.sockaddr_un* %2 to i8*
%7 = bitcast %struct.sockaddr_un* %3 to i8*
%8 = tail call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = call i32 @bind(i32 %11, %struct.sockaddr* %15, i32 8)
%17 = call i32 @listen(i32 %11, i32 5)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
store i32 8, i32* %1
%19 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%20 = call i32 @accept(i32 %11, %struct.sockaddr* %19, i32* %1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %32, label %22
22: ; preds = %0, %22
%23 = phi i32 [ %30, %22 ], [ %20, %0 ]
%24 = call i32 @read(i32 %23, i8* %4, i32 1)
%25 = load i8, i8* %4
%26 = add i8 %25, 1
store i8 %26, i8* %4
%27 = call i32 @write(i32 %23, i8* %4, i32 1)
%28 = call i32 @close(i32 %23)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
store i32 8, i32* %1
%30 = call i32 @accept(i32 %11, %struct.sockaddr* %19, i32* %1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %22
32: ; preds = %22, %0
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
ret i32 -1
}