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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int ipc_bind(const char *sock) {
  int afd;
  struct sockaddr_un addr;
  socklen_t addr_len = sizeof(addr);
  memset(&(addr), 0, sizeof(addr));
  if ((afd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
    return -1;
  addr.sun_family = AF_LOCAL;
  snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", sock);
  unlink(addr.sun_path);
  if (bind(afd, (struct sockaddr *)&addr, addr_len) < 0 ||
      listen(afd, 100) < 0) {
    close(afd);
    return -1;
  }
  return afd;
}


x86 assembly (clang, -O0):
<ipc_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x8,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11c4 <ipc_bind+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <ipc_bind+0xca>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe1d(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x20(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jl     1220 <ipc_bind+0xb0>
mov    -0x14(%rbp),%edi
mov    $0x64,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1234 <ipc_bind+0xc4>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <ipc_bind+0xca>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e58(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1204 <ipc_bind+0x94>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0xe42(%rip),%rdx
mov    $0x4,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    (%rsp),%edi
call   1030 <unlink@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     11fd <ipc_bind+0x8d>
mov    %ebp,%edi
mov    $0x64,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     11fd <ipc_bind+0x8d>
mov    %ebp,%r15d
jmp    1204 <ipc_bind+0x94>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @ipc_bind(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%5 = load i32, i32* @AF_LOCAL
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %1
%10 = load i32, i32* @AF_LOCAL
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @snprintf(i32 %13, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%15 = load i32, i32* %12
%16 = call i32 @unlink(i32 %15)
%17 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%18 = call i64 @bind(i32 %7, %struct.sockaddr* %17, i32 8)
%19 = icmp slt i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %9
%21 = call i64 @listen(i32 %7, i32 100)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20, %9
%24 = call i32 @close(i32 %7)
br label %25
25: ; preds = %20, %1, %23
%26 = phi i32 [ -1, %23 ], [ -1, %1 ], [ %7, %20 ]
ret i32 %26
}