#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_SEQPACKET;
int /*<<< orphan*/ assert(int);
int bind(int, struct sockaddr *, int);
int listen(int, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int ipc_bind(const char *socket_path) {
  int err;
  unlink(socket_path);
  int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  struct sockaddr_un addr = {
      .sun_family = AF_UNIX,
  };
  snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
  err = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
  assert(err == 0);
  err = listen(sock, 3);
  assert(err == 0);
  return sock;
}


x86 assembly (clang, -O0):
<ipc_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rcx
mov    $0x4,%esi
lea    0xe49(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%edi
mov    $0x3,%esi
call   1060 <listen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_bind>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <unlink@plt>
mov    0x2e71(%rip),%r14
mov    (%r14),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
movl   $0x0,0x8(%rsp)
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0xe5e(%rip),%rdx
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    %ebp,%edi
mov    $0x3,%esi
call   1060 <listen@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
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 }
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @ipc_bind(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @unlink(i8* %6)
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_SEQPACKET
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
%13 = load i32, i32* @AF_UNIX
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %2
%17 = call i32 @snprintf(i32 %15, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %4
%19 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%20 = call i32 @bind(i32 %18, %struct.sockaddr* %19, i32 8)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* %4
%26 = call i32 @listen(i32 %25, i32 3)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @ipc_bind(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = tail call i32 @unlink(i8* %0)
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_SEQPACKET
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = bitcast %struct.sockaddr_un* %2 to i8*
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
%10 = load i32, i32* @AF_UNIX
store i32 %10, i32* %9
%11 = tail call i32 @snprintf(i32 0, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%13 = call i32 @bind(i32 %6, %struct.sockaddr* %12, i32 8)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = call i32 @listen(i32 %6, i32 3)
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
ret i32 %6
}