#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*/ socklen_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int connection_handler(int);
scalar_t__ fork();
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  struct sockaddr_un address;
  int socket_fd, connection_fd;
  socklen_t address_length;
  pid_t child;
  socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    printf("socket() failed\n");
    return 1;
  }
  unlink("./demo_socket");
  memset(&address, 0, sizeof(struct sockaddr_un));
  address.sun_family = AF_UNIX;
  snprintf(address.sun_path, 108, "./demo_socket");
  if (bind(socket_fd, (struct sockaddr *)&address,
           sizeof(struct sockaddr_un)) != 0) {
    printf("bind() failed\n");
    return 1;
  }
  if (listen(socket_fd, 5) != 0) {
    printf("listen() failed\n");
    return 1;
  }
  while ((connection_fd = accept(socket_fd, (struct sockaddr *)&address,
                                 &address_length)) > -1) {
    child = fork();
    if (child == 0) {
      return connection_handler(connection_fd);
    }
    close(connection_fd);
  }
  close(socket_fd);
  unlink("./demo_socket");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fd <main+0x4d>
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130a <main+0x15a>
lea    0xe0d(%rip),%rdi
call   1030 <unlink@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
mov    $0x6c,%esi
lea    0xddd(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     126c <main+0xbc>
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130a <main+0x15a>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     129b <main+0xeb>
lea    0xda4(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130a <main+0x15a>
jmp    12a0 <main+0xf0>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jle    12ef <main+0x13f>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12e2 <main+0x132>
mov    -0x18(%rbp),%edi
call   1060 <connection_handler@plt>
mov    %eax,-0x4(%rbp)
jmp    130a <main+0x15a>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
jmp    12a0 <main+0xf0>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
lea    0xd13(%rip),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1237 <main+0x87>
mov    %eax,%ebx
lea    0xe20(%rip),%rbp
mov    %rbp,%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    $0x6c,%esi
mov    %rbp,%rdx
call   1050 <snprintf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %rax,%rax
je     1240 <main+0x90>
lea    0xde9(%rip),%rdi
jmp    1258 <main+0xa8>
lea    0xdee(%rip),%rdi
jmp    1258 <main+0xa8>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
je     126d <main+0xbd>
lea    0xdb6(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <accept@plt>
test   %eax,%eax
js     12b6 <main+0x106>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
lea    0x14(%rsp),%r15
xchg   %ax,%ax
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %ebp,%edi
test   %rax,%rax
je     12cd <main+0x11d>
call   1080 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1290 <main+0xe0>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xd3c(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
jmp    1262 <main+0xb2>
call   1060 <connection_handler@plt>
jmp    1262 <main+0xb2>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"socket() failed\0A\00"
@.str.1 = [14 x i8] c"./demo_socket\00"
@AF_UNIX = global i32 0
@.str.2 = [15 x i8] c"bind() failed\0A\00"
@.str.3 = [17 x i8] c"listen() failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %54
14: ; preds = %0
%15 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @snprintf(i32 %20, i32 108, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %3
%23 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 8)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %54
28: ; preds = %14
%29 = load i32, i32* %3
%30 = call i64 @listen(i32 %29, i32 5)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %54
34: ; preds = %28
br label %35
35: ; preds = %47, %34
%36 = load i32, i32* %3
%37 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%38 = call i32 @accept(i32 %36, %struct.sockaddr* %37, i32* %5)
store i32 %38, i32* %4
%39 = icmp sgt i32 %38, -1
br i1 %39, label %40, label %50
40: ; preds = %35
%41 = call i64 (...) @fork()
store i64 %41, i64* %6
%42 = load i64, i64* %6
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %4
%46 = call i32 @connection_handler(i32 %45)
store i32 %46, i32* %1
br label %54
47: ; preds = %40
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
br label %35
50: ; preds = %35
%51 = load i32, i32* %3
%52 = call i32 @close(i32 %51)
%53 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %54
54: ; preds = %50, %44, %32, %26, %12
%55 = load i32, i32* %1
ret i32 %55
}

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