#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 {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
size_t SUN_LEN(struct sockaddr_un *);
int bind(int, struct sockaddr *, size_t);
int errno;
int listen(int, int);
int /*<<< orphan*/ remove(char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int create_server_socket(const char *filename, int *new_sock) {
  struct sockaddr_un name;
  size_t size;
  int sock;
  int ret;
  int err;
  sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (sock < 0) {
    err = errno;
    goto out;
  }
  name.sun_family = AF_LOCAL;
  strncpy(name.sun_path, filename, sizeof(name.sun_path));
  name.sun_path[sizeof(name.sun_path) - 1] = '\0';
  size = SUN_LEN(&name);
  remove(filename);
  ret = bind(sock, (struct sockaddr *)&name, size);
  if (ret < 0) {
    err = errno;
    goto out;
  }
  ret = listen(sock, 2);
  if (ret < 0) {
    err = errno;
    goto out;
  }
  *new_sock = sock;
  err = 0;
out:
  return err;
}


x86 assembly (clang, -O0):
<create_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11a7 <create_server_socket+0x47>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    124c <create_server_socket+0xec>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,0x7(%rax)
lea    -0x20(%rbp),%rdi
call   1070 <SUN_LEN@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <bind@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1211 <create_server_socket+0xb1>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    124c <create_server_socket+0xec>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1050 <listen@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    123c <create_server_socket+0xdc>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    124c <create_server_socket+0xec>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server_socket>:
push   %rbx
mov    %rdi,%rbx
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
test   %eax,%eax
jns    114c <create_server_socket+0x2c>
mov    0x2e90(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@AF_LOCAL = global i32 0
define i32 @create_server_socket(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.sockaddr_un
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%10 = load i32, i32* @PF_LOCAL
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32, i32* @errno
store i32 %16, i32* %9
br label %48
17: ; preds = %2
%18 = load i32, i32* @AF_LOCAL
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = load i8*, i8** %3
%23 = call i32 @strncpy(i8* %21, i8* %22, i32 8)
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 7
store i8 0, i8* %26
%27 = call i64 @SUN_LEN(%struct.sockaddr_un* %5)
store i64 %27, i64* %6
%28 = load i8*, i8** %3
%29 = call i32 @remove(i8* %28)
%30 = load i32, i32* %7
%31 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%32 = load i64, i64* %6
%33 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i64 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %17
%37 = load i32, i32* @errno
store i32 %37, i32* %9
br label %48
38: ; preds = %17
%39 = load i32, i32* %7
%40 = call i32 @listen(i32 %39, i32 2)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = load i32, i32* @errno
store i32 %44, i32* %9
br label %48
45: ; preds = %38
%46 = load i32, i32* %7
%47 = load i32*, i32** %4
store i32 %46, i32* %47
store i32 0, i32* %9
br label %48
48: ; preds = %45, %43, %36, %15
%49 = load i32, i32* %9
ret i32 %49
}

LLVM IR (-O2):
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@AF_LOCAL = global i32 0
define i32 @create_server_socket(i8* %0, i32* nocapture writeonly %1) {
%3 = load i32, i32* @PF_LOCAL
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = load i32, i32* @errno
ret i32 %8
9: ; preds = %2
%10 = tail call i32 @strncpy(i8* undef, i8* %0, i32 8)
unreachable
}