#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*/ sockname;
typedef int /*<<< orphan*/ peer;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int umask(int);
int /*<<< orphan*/ unlink(char *);
void wait_for_unix_socket(int *fd) {
  struct sockaddr_un sockname, peer;
  int s;
  unsigned int addrlen;
  int prev_umask;
  unlink("/var/run/xf86-qubes-socket");
  s = socket(AF_UNIX, SOCK_STREAM, 0);
  memset(&sockname, 0, sizeof(sockname));
  sockname.sun_family = AF_UNIX;
  memcpy(sockname.sun_path, "/var/run/xf86-qubes-socket",
         strlen("/var/run/xf86-qubes-socket"));
  prev_umask = umask(077);
  if (bind(s, (struct sockaddr *)&sockname, sizeof(sockname)) == -1) {
    printf("bind() failed\n");
    close(s);
    exit(1);
  }
  umask(prev_umask);
  if (listen(s, 5) == -1) {
    perror("listen() failed\n");
    close(s);
    exit(1);
  }
  addrlen = sizeof(peer);
  fprintf(stderr, "Waiting on %s socket...\n", "/var/run/xf86-qubes-socket");
  *fd = accept(s, (struct sockaddr *)&peer, &addrlen);
  if (*fd == 1) {
    perror("unix accept");
    exit(1);
  }
  fprintf(stderr, "Ok, somebody connected.\n");
  close(s);
}


x86 assembly (clang, -O0):
<wait_for_unix_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe0d(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    0xdc3(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
lea    0xdb2(%rip),%rsi
call   10a0 <memcpy@plt>
mov    $0x3f,%edi
call   1090 <umask@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1298 <wait_for_unix_socket+0xb8>
lea    0xd9a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1090 <umask@plt>
mov    -0x1c(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12d4 <wait_for_unix_socket+0xf4>
lea    0xd6d(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
movl   $0x8,-0x20(%rbp)
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
lea    0xd0e(%rip),%rdx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x1,(%rax)
jne    1334 <wait_for_unix_socket+0x154>
lea    0xd2f(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
lea    0xd1c(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_unix_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
lea    0xe0a(%rip),%r15
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    0x2de3(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   10a0 <memcpy@plt>
mov    $0x3f,%edi
call   1090 <umask@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
je     12ea <wait_for_unix_socket+0x10a>
mov    %ebp,%edi
call   1090 <umask@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
je     12f8 <wait_for_unix_socket+0x118>
movl   $0x8,0xc(%rsp)
mov    0x2d44(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xd8e(%rip),%rsi
lea    0xd5b(%rip),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10e0 <accept@plt>
mov    %eax,(%r14)
cmp    $0x1,%eax
je     1315 <wait_for_unix_socket+0x135>
mov    0x0(%rbp),%edi
lea    0xd82(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd79(%rip),%rdi
call   1040 <puts@plt>
jmp    1304 <wait_for_unix_socket+0x124>
lea    0xd1c(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xd29(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"/var/run/xf86-qubes-socket\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"bind() failed\0A\00"
@.str.2 = [17 x i8] c"listen() failed\0A\00"
@stderr = global i32 0
@.str.3 = [25 x i8] c"Waiting on %s socket...\0A\00"
@.str.4 = [12 x i8] c"unix accept\00"
@.str.5 = [25 x i8] c"Ok, somebody connected.\0A\00"
define void @wait_for_unix_socket(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.sockaddr_un
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %2
%8 = call i32 @unlink(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @memcpy(i32 %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @umask(i32 63)
store i32 %19, i32* %7
%20 = load i32, i32* %5
%21 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %1
%25 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %1
%30 = load i32, i32* %7
%31 = call i32 @umask(i32 %30)
%32 = load i32, i32* %5
%33 = call i32 @listen(i32 %32, i32 5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %29
store i32 8, i32* %6
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* %5
%44 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %6)
%46 = load i32*, i32** %2
store i32 %45, i32* %46
%47 = load i32*, i32** %2
%48 = load i32, i32* %47
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %53
50: ; preds = %40
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %40
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%56 = load i32, i32* %5
%57 = call i32 @close(i32 %56)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"/var/run/xf86-qubes-socket\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"listen() failed\0A\00"
@stderr = global i32 0
@.str.3 = [25 x i8] c"Waiting on %s socket...\0A\00"
@.str.4 = [12 x i8] c"unix accept\00"
@.str.5 = [25 x i8] c"Ok, somebody connected.\0A\00"
@str = [14 x i8] c"bind() failed\00"
define void @wait_for_unix_socket(i32* nocapture writeonly %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = bitcast %struct.sockaddr_un* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 @unlink(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strlen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @memcpy(i32 %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = call i32 @umask(i32 63)
%20 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %11, %struct.sockaddr* %20, i32 8)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %1
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%25 = call i32 @close(i32 %11)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %1
%28 = call i32 @umask(i32 %19)
%29 = call i32 @listen(i32 %11, i32 5)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @close(i32 %11)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
store i32 8, i32* %4
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%38 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%39 = call i32 @accept(i32 %11, %struct.sockaddr* %38, i32* %4)
store i32 %39, i32* %0
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %35
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @close(i32 %11)
ret void
}