#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*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
int uds_open(char *path) {
  struct sockaddr_un address;
  int socket_fd;
  int rv;
  unlink(path);
  memset(&address, 0, sizeof(address));
  address.sun_family = AF_UNIX;
  strcpy(address.sun_path, path);
  socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    perror("socket failed");
    return -1;
  }
  rv = bind(socket_fd, (struct sockaddr *)&address, sizeof(address));
  if (rv != 0) {
    perror("bind failed");
    return -1;
  }
  rv = listen(socket_fd, 5);
  if (rv != 0) {
    perror("listen failed");
    return -1;
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<uds_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11eb <uds_open+0x7b>
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <uds_open+0xe9>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1221 <uds_open+0xb1>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <uds_open+0xe9>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1253 <uds_open+0xe3>
lea    0xdd8(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <uds_open+0xe9>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uds_open>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <unlink@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11f3 <uds_open+0x83>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %eax,%eax
jne    11fc <uds_open+0x8c>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %eax,%eax
jne    1205 <uds_open+0x95>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe06(%rip),%rdi
jmp    120c <uds_open+0x9c>
lea    0xe0b(%rip),%rdi
jmp    120c <uds_open+0x9c>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11eb <uds_open+0x7b>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@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"
define i32 @uds_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @unlink(i8* %7)
%9 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i8*, i8** %3
%15 = call i32 @strcpy(i32 %13, i8* %14)
%16 = load i32, i32* @PF_UNIX
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
23: ; preds = %1
%24 = load i32, i32* %5
%25 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
31: ; preds = %23
%32 = load i32, i32* %5
%33 = call i32 @listen(i32 %32, i32 5)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
38: ; preds = %31
%39 = load i32, i32* %5
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %36, %29, %21
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@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"
define i32 @uds_open(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 @unlink(i8* %0)
%5 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @strcpy(i32 %9, i8* %0)
%11 = load i32, i32* @PF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %1
%16 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%17 = call i32 @bind(i32 %13, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @listen(i32 %13, i32 5)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19, %15, %1
%23 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %15 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %19 ]
%24 = call i32 @perror(i8* %23)
br label %25
25: ; preds = %22, %19
%26 = phi i32 [ %13, %19 ], [ -1, %22 ]
ret i32 %26
}