#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*/ sa;
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ chmod(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int net_create_socket(char *, int /*<<< orphan*/);
int net_listen(char *, int, struct sockaddr *, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int net_unix_server(char *err, const char *path, mode_t perm) {
  int sock;
  struct sockaddr_un sa;
  if ((sock = net_create_socket(err, AF_LOCAL)) == -1) {
    return -1;
  }
  memset(&sa, 0, sizeof(sa));
  sa.sun_family = AF_LOCAL;
  strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
  if (net_listen(err, sock, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    return -1;
  }
  if (perm) {
    chmod(sa.sun_path, perm);
  }
  return sock;
}


x86 assembly (clang, -O0):
<net_unix_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1050 <net_create_socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    118d <net_unix_server+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <net_unix_server+0xaf>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
lea    -0x28(%rbp),%rdx
mov    $0x8,%ecx
call   1070 <net_listen@plt>
cmp    $0xffffffff,%eax
jne    11e4 <net_unix_server+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <net_unix_server+0xaf>
cmpl   $0x0,-0x1c(%rbp)
je     11f9 <net_unix_server+0xa9>
mov    -0x28(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <chmod@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_unix_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e7d(%rip),%r13
mov    0x0(%r13),%esi
call   1050 <net_create_socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11d2 <net_unix_server+0x82>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r12,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rsp,%rdx
mov    $0x8,%ecx
call   1070 <net_listen@plt>
cmp    $0xffffffff,%eax
je     11d2 <net_unix_server+0x82>
test   %r14d,%r14d
je     11cf <net_unix_server+0x7f>
mov    (%rsp),%edi
mov    %r14d,%esi
call   1060 <chmod@plt>
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x8,%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_LOCAL = global i32 0
define i32 @net_unix_server(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @AF_LOCAL
%12 = call i32 @net_create_socket(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %39
15: ; preds = %3
%16 = call i32 @memset(%struct.sockaddr_un* %9, i32 0, i32 8)
%17 = load i32, i32* @AF_LOCAL
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i8*, i8** %6
%22 = call i32 @strncpy(i32 %20, i8* %21, i32 3)
%23 = load i8*, i8** %5
%24 = load i32, i32* %8
%25 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%26 = call i32 @net_listen(i8* %23, i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %15
store i32 -1, i32* %4
br label %39
29: ; preds = %15
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* %7
%36 = call i32 @chmod(i32 %34, i32 %35)
br label %37
37: ; preds = %32, %29
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %39
39: ; preds = %37, %28, %14
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
define i32 @net_unix_server(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.sockaddr_un* %4 to i8*
%6 = load i32, i32* @AF_LOCAL
%7 = tail call i32 @net_create_socket(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %24, label %9
9: ; preds = %3
%10 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%11 = load i32, i32* @AF_LOCAL
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @strncpy(i32 %14, i8* %1, i32 3)
%16 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%17 = call i32 @net_listen(i8* %0, i32 %7, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i32 %17, -1
br i1 %18, label %24, label %19
19: ; preds = %9
%20 = icmp eq i32 %2, 0
br i1 %20, label %24, label %21
21: ; preds = %19
%22 = load i32, i32* %13
%23 = call i32 @chmod(i32 %22, i32 %2)
br label %24
24: ; preds = %19, %21, %9, %3
%25 = phi i32 [ -1, %3 ], [ -1, %9 ], [ %7, %21 ], [ %7, %19 ]
ret i32 %25
}