#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;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sockdata;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, unsigned int);
int /*<<< orphan*/ unlink(char *);
int create_unix_socket(const char *sockname, unsigned int namelen,
                       int *unix_sck) {
  int connection;
  socklen_t remote_size;
  struct sockaddr_un sockdata;
  struct sockaddr_un remotedata;
  if ((*unix_sck = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("cannot create unix socket");
    exit(1);
  }
  sockdata.sun_family = AF_UNIX;
  strncpy(sockdata.sun_path, sockname, namelen);
  sockdata.sun_path[namelen] = '\0';
  unlink(sockdata.sun_path);
  if ((bind(*unix_sck, (struct sockaddr *)&sockdata, sizeof(sockdata))) < 0) {
    perror("cannot bind unix socket");
    exit(1);
  }
  if ((listen(*unix_sck, 1)) < 0) {
    perror("cannot listen on unix socket");
    exit(1);
  }
  remote_size = sizeof(struct sockaddr_un);
  connection = accept(*unix_sck, (struct sockaddr *)&remotedata, &remote_size);
  if (connection < 0) {
    perror("error accepting unix socket connection");
    exit(1);
  }
  return connection;
}


x86 assembly (clang, -O0):
<create_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    11d1 <create_unix_socket+0x51>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1235 <create_unix_socket+0xb5>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    $0x1,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    1265 <create_unix_socket+0xe5>
lea    0xddc(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x10,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12a2 <create_unix_socket+0x122>
lea    0xdbc(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_unix_socket>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,0x0(%rbp)
test   %eax,%eax
jns    1183 <create_unix_socket+0x43>
lea    0xe8c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>

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

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"cannot create unix socket\00"
define i32 @create_unix_socket(i8* %0, i32 %1, i32* nocapture %2) {
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i32 @strncpy(i8* undef, i8* %0, i32 %1)
unreachable
}