#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
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 /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ uds_set_addr(struct sockaddr_un *, char const *);
int /*<<< orphan*/ unlink(char const *);
int uds_server(const char *sock_name,
               void (*on_connection)(int fd, void *copy_data, size_t size),
               void *copy_data, size_t copy_size) {
  struct sockaddr_un addr;
  int sock, conn;
  socklen_t addr_len;
  pid_t child;
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket failed");
    return -1;
  }
  unlink(sock_name);
  uds_set_addr(&addr, sock_name);
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    perror("bind failed");
    return -1;
  }
  if (listen(sock, 5) != 0) {
    perror("listen failed");
    return -1;
  }
  while ((conn = accept(sock, (struct sockaddr *)&addr, &addr_len)) > -1) {
    child = fork();
    if (child == 0) {
      on_connection(conn, copy_data, copy_size);
      exit(EXIT_SUCCESS);
    }
    close(conn);
  }
  close(sock);
  unlink(sock_name);
  return 0;
}


x86 assembly (clang, -O0):
<uds_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11f6 <uds_server+0x56>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <uds_server+0x149>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1050 <uds_set_addr@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     123f <uds_server+0x9f>
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <uds_server+0x149>
mov    -0x34(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     126e <uds_server+0xce>
lea    0xdbd(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <uds_server+0x149>
jmp    1273 <uds_server+0xd3>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jle    12d1 <uds_server+0x131>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12c4 <uds_server+0x124>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   *%rax
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
jmp    1273 <uds_server+0xd3>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uds_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1298 <uds_server+0xf8>
mov    %eax,%ebp
mov    %r13,%rdi
call   1030 <unlink@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <uds_set_addr@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    12a1 <uds_server+0x101>
mov    %ebp,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %rax,%rax
jne    12aa <uds_server+0x10a>
mov    %r15,0x10(%rsp)
mov    %r14,0x8(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1090 <accept@plt>
test   %eax,%eax
js     1278 <uds_server+0xd8>
mov    %eax,%r14d
mov    %rsp,%rbx
lea    0x4(%rsp),%r15
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %r14d,%edi
test   %rax,%rax
je     12bd <uds_server+0x11d>
call   1040 <close@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1250 <uds_server+0xb0>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r13,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
jmp    12b1 <uds_server+0x111>
lea    0xd66(%rip),%rdi
jmp    12b1 <uds_server+0x111>
lea    0xd69(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1289 <uds_server+0xe9>
mov    %r12,%rsi
mov    0x8(%rsp),%rdx
call   *0x10(%rsp)
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

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