#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 */
typedef int /*<<< orphan*/ sun;
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ EADDRINUSE;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ chmod(int /*<<< orphan*/, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(scalar_t__);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int bus_init(char *path) {
  struct sockaddr_un sun;
  int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
  memset(&sun, 0, sizeof(struct sockaddr_un));
  sun.sun_family = AF_UNIX;
  strncpy(sun.sun_path, path, strlen(path));
  if (bind(connect_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
    if ((errno == EADDRINUSE)) {
      printf("Could not bind to socket '%s': %s\n", path, strerror(errno));
      syslog(LOG_ERR, "Could not bind to socket '%s': %s\n", path,
             strerror(errno));
      exit(-1);
    } else if (bind(connect_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
      printf("Could not bind to socket '%s' (second attempt): %s", path,
             strerror(errno));
      syslog(LOG_ERR, "Could not bind to socket '%s' (second attempt): %s",
             path, strerror(errno));
      exit(-1);
    }
  }
  chmod(sun.sun_path, 0777);
  if (listen(connect_fd, 1) < 0) {
    printf("Could not listen on fd %d: %s", connect_fd, strerror(errno));
    syslog(LOG_ERR, "Could not listen on fd %d: %s", connect_fd,
           strerror(errno));
    exit(-1);
  }
  return connect_fd;
}


x86 assembly (clang, -O0):
<bus_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1355 <bus_init+0x1a5>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%rax
jne    12bf <bus_init+0x10f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d63(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    134b <bus_init+0x19b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2cd7(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x58(%rbp),%rsi
mov    %rax,%rdx
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2c9f(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xce9(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    1350 <bus_init+0x1a0>
jmp    1355 <bus_init+0x1a5>
mov    -0x10(%rbp),%edi
mov    $0x1ff,%esi
call   1090 <chmod@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    13e4 <bus_init+0x234>
mov    -0x14(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    0x2c3a(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x64(%rbp),%esi
mov    %rax,%rdx
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2c05(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    -0x5c(%rbp),%edx
mov    %rax,%rcx
lea    0xc83(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bus_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1251 <bus_init+0xa1>
mov    0x2d9a(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2d98(%rip),%rax
cmp    (%rax),%rdi
je     12cd <bus_init+0x11d>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12db <bus_init+0x12b>
mov    (%rsp),%edi
mov    $0x1ff,%esi
call   1090 <chmod@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     127c <bus_init+0xcc>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d3d(%rip),%r15
mov    (%r15),%rdi
call   10c0 <strerror@plt>
lea    0xdc4(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%ebx
mov    (%r15),%rdi
call   10c0 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <syslog@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
call   10c0 <strerror@plt>
lea    0xd27(%rip),%r15
jmp    12ea <bus_init+0x13a>
mov    (%rbx),%rdi
call   10c0 <strerror@plt>
lea    0xd39(%rip),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2caf(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rdi
call   10c0 <strerror@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <syslog@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@.str = [35 x i8] c"Could not bind to socket '%s': %s\0A\00"
@LOG_ERR = global i32 0
@.str.1 = [51 x i8] c"Could not bind to socket '%s' (second attempt): %s\00"
@.str.2 = [30 x i8] c"Could not listen on fd %d: %s\00"
define i32 @bus_init(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%9 = load i32, i32* @AF_UNIX
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i8*, i8** %2
%14 = load i8*, i8** %2
%15 = call i32 @strlen(i8* %14)
%16 = call i32 @strncpy(i32 %12, i8* %13, i32 %15)
%17 = load i32, i32* %4
%18 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%19 = call i64 @bind(i32 %17, %struct.sockaddr* %18, i32 8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %54
21: ; preds = %1
%22 = load i64, i64* @errno
%23 = load i64, i64* @EADDRINUSE
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %36
25: ; preds = %21
%26 = load i8*, i8** %2
%27 = load i64, i64* @errno
%28 = call i8* @strerror(i64 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %28)
%30 = load i32, i32* @LOG_ERR
%31 = load i8*, i8** %2
%32 = load i64, i64* @errno
%33 = call i8* @strerror(i64 %32)
%34 = call i32 (i32, i8*, ...) @syslog(i32 %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %33)
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %21
%37 = load i32, i32* %4
%38 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 8)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %52
41: ; preds = %36
%42 = load i8*, i8** %2
%43 = load i64, i64* @errno
%44 = call i8* @strerror(i64 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %44)
%46 = load i32, i32* @LOG_ERR
%47 = load i8*, i8** %2
%48 = load i64, i64* @errno
%49 = call i8* @strerror(i64 %48)
%50 = call i32 (i32, i8*, ...) @syslog(i32 %46, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %47, i8* %49)
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %36
br label %53
53: ; preds = %52
br label %54
54: ; preds = %53, %1
%55 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @chmod(i32 %56, i32 511)
%58 = load i32, i32* %4
%59 = call i64 @listen(i32 %58, i32 1)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %72
61: ; preds = %54
%62 = load i32, i32* %4
%63 = load i64, i64* @errno
%64 = call i8* @strerror(i64 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %62, i8* %64)
%66 = load i32, i32* @LOG_ERR
%67 = load i32, i32* %4
%68 = load i64, i64* @errno
%69 = call i8* @strerror(i64 %68)
%70 = call i32 (i32, i8*, ...) @syslog(i32 %66, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %67, i8* %69)
%71 = call i32 @exit(i32 -1)
unreachable
72: ; preds = %54
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@errno = global i64 0
@EADDRINUSE = global i64 0
@.str = [35 x i8] c"Could not bind to socket '%s': %s\0A\00"
@LOG_ERR = global i32 0
@.str.1 = [51 x i8] c"Could not bind to socket '%s' (second attempt): %s\00"
@.str.2 = [30 x i8] c"Could not listen on fd %d: %s\00"
define i32 @bus_init(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @PF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @strlen(i8* %0)
%13 = call i32 @strncpy(i32 %11, i8* %0, i32 %12)
%14 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%15 = call i64 @bind(i32 %6, %struct.sockaddr* %14, i32 8)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %41
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @EADDRINUSE
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = call i8* @strerror(i64 %18)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %22)
%24 = load i32, i32* @LOG_ERR
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 (i32, i8*, ...) @syslog(i32 %24, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %26)
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %17
%30 = call i64 @bind(i32 %6, %struct.sockaddr* %14, i32 8)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = call i8* @strerror(i64 %33)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %34)
%36 = load i32, i32* @LOG_ERR
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 (i32, i8*, ...) @syslog(i32 %36, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %38)
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %29, %1
%42 = load i32, i32* %10
%43 = call i32 @chmod(i32 %42, i32 511)
%44 = call i64 @listen(i32 %6, i32 1)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %41
%47 = load i64, i64* @errno
%48 = call i8* @strerror(i64 %47)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %6, i8* %48)
%50 = load i32, i32* @LOG_ERR
%51 = load i64, i64* @errno
%52 = call i8* @strerror(i64 %51)
%53 = call i32 (i32, i8*, ...) @syslog(i32 %50, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %6, i8* %52)
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %41
ret i32 %6
}