#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
int bind_socket(char *path) {
  int sock, res, len;
  struct sockaddr_un local;
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  local.sun_family = AF_UNIX;
  strcpy(local.sun_path, path);
  len = sizeof(local.sun_path) + sizeof(local.sun_family);
  if (sock == -1) {
    return 0;
  }
  unlink(path);
  res = bind(sock, (struct sockaddr *)&local, len);
  listen(sock, 16);
  if (res == -1) {
    return 0;
  }
  return sock;
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x8,-0x1c(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ad <bind_socket+0x5d>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <bind_socket+0xa1>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <bind@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x10,%esi
call   1050 <listen@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11eb <bind_socket+0x9b>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <bind_socket+0xa1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e87(%rip),%r15
mov    (%r15),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rsi
call   1040 <strcpy@plt>
cmp    $0xffffffff,%ebx
je     11bb <bind_socket+0x6b>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
mov    %eax,%r14d
mov    %ebx,%edi
mov    $0x10,%esi
call   1050 <listen@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%r14d
cmovne %ebx,%ebp
mov    %ebp,%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_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @bind_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load i8*, i8** %3
%16 = call i32 @strcpy(i32 %14, i8* %15)
store i32 8, i32* %6
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %1
store i32 0, i32* %2
br label %34
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = call i32 @unlink(i8* %21)
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%25 = load i32, i32* %6
%26 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 %25)
store i32 %26, i32* %5
%27 = load i32, i32* %4
%28 = call i32 @listen(i32 %27, i32 16)
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %20
store i32 0, i32* %2
br label %34
32: ; preds = %20
%33 = load i32, i32* %4
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %31, %19
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
define i32 @bind_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = load i32, i32* @AF_UNIX
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %7, i32* %8
%9 = tail call i32 @strcpy(i32 undef, i8* %0)
%10 = icmp eq i32 %6, -1
br i1 %10, label %18, label %11
11: ; preds = %1
%12 = tail call i32 @unlink(i8* %0)
%13 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%14 = call i32 @bind(i32 %6, %struct.sockaddr* %13, i32 8)
%15 = call i32 @listen(i32 %6, i32 16)
%16 = icmp eq i32 %14, -1
%17 = select i1 %16, i32 0, i32 %6
br label %18
18: ; preds = %11, %1
%19 = phi i32 [ 0, %1 ], [ %17, %11 ]
ret i32 %19
}