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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int create_socket() {
  const char *sock_name = "./ipc_socket";
  struct sockaddr_un addr;
  int socket_fd;
  socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    return -1;
  }
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, sock_name);
  unlink(addr.sun_path);
  if (bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    return -1;
  }
  if (listen(socket_fd, 5) == -1) {
    return -1;
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1195 <create_socket+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <create_socket+0xb3>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    11db <create_socket+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <create_socket+0xb3>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    11fd <create_socket+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <create_socket+0xb3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e89(%rip),%r14
mov    (%r14),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11bf <create_socket+0x6f>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0xe75(%rip),%rsi
call   1040 <strcpy@plt>
call   1030 <unlink@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11bf <create_socket+0x6f>
mov    %ebx,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"./ipc_socket\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @create_socket() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %2
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %33
11: ; preds = %0
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %2
%17 = call i32 @strcpy(i32 %15, i8* %16)
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @unlink(i32 %19)
%21 = load i32, i32* %4
%22 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %11
store i32 -1, i32* %1
br label %33
26: ; preds = %11
%27 = load i32, i32* %4
%28 = call i32 @listen(i32 %27, i32 5)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 -1, i32* %1
br label %33
31: ; preds = %26
%32 = load i32, i32* %4
store i32 %32, i32* %1
br label %33
33: ; preds = %31, %30, %25, %10
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"./ipc_socket\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @create_socket() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%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 %19, label %7
7: ; preds = %0
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @unlink(i32 undef)
%12 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%13 = call i32 @bind(i32 %5, %struct.sockaddr* %12, i32 8)
%14 = icmp eq i32 %13, -1
br i1 %14, label %19, label %15
15: ; preds = %7
%16 = call i32 @listen(i32 %5, i32 5)
%17 = icmp eq i32 %16, -1
%18 = select i1 %17, i32 -1, i32 %5
br label %19
19: ; preds = %15, %7, %0
%20 = phi i32 [ -1, %0 ], [ -1, %7 ], [ %18, %15 ]
ret i32 %20
}