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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int listen(int, int);
int /*<<< orphan*/ prepare_path(int /*<<< orphan*/);
int /*<<< orphan*/ service_path(char const *, int /*<<< orphan*/,
                                int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int service_listen(const char *app) {
  int s = socket(AF_UNIX, SOCK_STREAM, 0);
  if (s == -1)
    return -1;
  struct sockaddr_un addr = {AF_UNIX};
  if (!service_path(app, 0, addr.sun_path, 108 - 1))
    goto err;
  if (!prepare_path(addr.sun_path))
    goto err;
  const int yes = 1;
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
    goto err;
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    goto err;
  if (listen(s, 127) == -1)
    goto err;
  return s;
  int saved_errno;
err:
  saved_errno = errno;
  close(s);
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<service_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ae <service_listen+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <service_listen+0x12d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%edx
xor    %esi,%esi
mov    $0x6b,%ecx
call   1030 <service_path@plt>
cmp    $0x0,%eax
jne    11e2 <service_listen+0x72>
jmp    1276 <service_listen+0x106>
mov    -0x20(%rbp),%edi
call   1060 <prepare_path@plt>
cmp    $0x0,%eax
jne    11f8 <service_listen+0x88>
jmp    1276 <service_listen+0x106>
movl   $0x1,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1231 <service_listen+0xc1>
jmp    1276 <service_listen+0x106>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1250 <service_listen+0xe0>
jmp    1276 <service_listen+0x106>
mov    -0x14(%rbp),%edi
mov    $0x7f,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    126b <service_listen+0xfb>
jmp    1276 <service_listen+0x106>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129d <service_listen+0x12d>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%ecx
mov    0x2d34(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<service_listen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e5e(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     123b <service_listen+0xcb>
mov    %eax,%ebx
mov    0x0(%rbp),%ebp
mov    %ebp,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
mov    $0x6b,%ecx
call   1030 <service_path@plt>
test   %eax,%eax
je     1227 <service_listen+0xb7>
mov    %ebp,%edi
call   1060 <prepare_path@plt>
test   %eax,%eax
je     1227 <service_listen+0xb7>
movl   $0x1,0x4(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1227 <service_listen+0xb7>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1227 <service_listen+0xb7>
mov    %ebx,%edi
mov    $0x7f,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1240 <service_listen+0xd0>
mov    0x2d9a(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,(%r14)
mov    $0xffffffff,%ebx
mov    %ebx,%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_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @service_listen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca i32
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* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %55
14: ; preds = %1
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%16 = load i32, i32* @AF_UNIX
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 0, i32* %17
%18 = load i8*, i8** %3
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @service_path(i8* %18, i32 0, i32 %20, i32 107)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %14
br label %50
24: ; preds = %14
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @prepare_path(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %24
br label %50
30: ; preds = %24
store i32 1, i32* %6
%31 = load i32, i32* %4
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i32 @setsockopt(i32 %31, i32 %32, i32 %33, i32* %6, i32 4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %30
br label %50
37: ; preds = %30
%38 = load i32, i32* %4
%39 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%40 = call i32 @bind(i32 %38, %struct.sockaddr* %39, i32 8)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %37
br label %50
43: ; preds = %37
%44 = load i32, i32* %4
%45 = call i32 @listen(i32 %44, i32 127)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %43
br label %50
48: ; preds = %43
%49 = load i32, i32* %4
store i32 %49, i32* %2
br label %55
50: ; preds = %47, %42, %36, %29, %23
%51 = load i32, i32* @errno
store i32 %51, i32* %7
%52 = load i32, i32* %4
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %7
store i32 %54, i32* @errno
store i32 -1, i32* %2
br label %55
55: ; preds = %50, %48, %13
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @service_listen(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %35, label %8
8: ; preds = %1
%9 = bitcast %struct.sockaddr_un* %2 to i8*
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%11 = load i32, i32* @AF_UNIX
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 0, i32* %12
%13 = tail call i32 @service_path(i8* %0, i32 0, i32 %11, i32 107)
%14 = icmp eq i32 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %8
%16 = tail call i32 @prepare_path(i32 %11)
%17 = icmp eq i32 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %15
store i32 1, i32* %3
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %6, i32 %19, i32 %20, i32* %3, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %30, label %23
23: ; preds = %18
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i32 @bind(i32 %6, %struct.sockaddr* %24, i32 8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i32 @listen(i32 %6, i32 127)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27, %23, %18, %15, %8
%31 = load i32, i32* @errno
%32 = call i32 @close(i32 %6)
store i32 %31, i32* @errno
br label %33
33: ; preds = %27, %30
%34 = phi i32 [ -1, %30 ], [ %6, %27 ]
br label %35
35: ; preds = %1, %33
%36 = phi i32 [ %34, %33 ], [ -1, %1 ]
ret i32 %36
}