#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 sun_path;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr const *, int);
int close(int);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int unlink(int);
int /*<<< orphan*/ unused(char *, ...);
int un_listen(const struct sockaddr_un *sa) {
  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd == -1) {
    do {
      unused("socket() failed");
    } while (0);
    goto _fail_socket;
  }
  if (bind(fd, (const struct sockaddr *)sa, sizeof(*sa)) == -1) {
    do {
      unused("bind(%d, %s) failed", fd, sa->sun_path);
    } while (0);
    goto _fail_bind;
  }
  if (listen(fd, 1) == -1) {
    do {
      unused("listen(%d) failed", fd);
    } while (0);
    goto _fail_listen;
  }
  return fd;
_fail_listen:
  if (unlink(sa->sun_path) == -1)
    do {
      unused("unlink(%s) failed", sa->sun_path);
    } while (0);
_fail_bind:
  if (close(fd) == -1)
    do {
      unused("close(%d) failed", fd);
    } while (0);
_fail_socket:
  return -1;
}


x86 assembly (clang, -O0):
<un_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11aa <un_listen+0x4a>
jmp    1197 <un_listen+0x37>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    1289 <un_listen+0x129>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    11e5 <un_listen+0x85>
jmp    11c9 <un_listen+0x69>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    1258 <un_listen+0xf8>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1216 <un_listen+0xb6>
jmp    1200 <un_listen+0xa0>
mov    -0x14(%rbp),%esi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    1221 <un_listen+0xc1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1290 <un_listen+0x130>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    1253 <un_listen+0xf3>
jmp    123a <un_listen+0xda>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    1253 <un_listen+0xf3>
jmp    1258 <un_listen+0xf8>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1284 <un_listen+0x124>
jmp    126e <un_listen+0x10e>
mov    -0x14(%rbp),%esi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <unused@plt>
jmp    1284 <un_listen+0x124>
jmp    1289 <un_listen+0x129>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<un_listen>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11b0 <un_listen+0x50>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     11c5 <un_listen+0x65>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     11da <un_listen+0x7a>
mov    %ebx,%ebp
jmp    1229 <un_listen+0xc9>
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1050 <unused@plt>
mov    $0xffffffff,%ebp
jmp    1229 <un_listen+0xc9>
mov    (%r14),%edx
lea    0xe41(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <unused@plt>
jmp    1208 <un_listen+0xa8>
lea    0xe43(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <unused@plt>
mov    (%r14),%edi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    1208 <un_listen+0xa8>
mov    (%r14),%esi
lea    0xe35(%rip),%rdi
xor    %eax,%eax
call   1050 <unused@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
jne    1229 <un_listen+0xc9>
lea    0xe28(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <unused@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [20 x i8] c"bind(%d, %s) failed\00"
@.str.2 = [18 x i8] c"listen(%d) failed\00"
@.str.3 = [18 x i8] c"unlink(%s) failed\00"
@.str.4 = [17 x i8] c"close(%d) failed\00"
define i32 @un_listen(%struct.sockaddr_un* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_un*
%4 = alloca i32
store %struct.sockaddr_un* %0, %struct.sockaddr_un** %3
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %1
br label %11
11: ; preds = %10
%12 = call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11
br label %63
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%17 = bitcast %struct.sockaddr_un* %16 to %struct.sockaddr*
%18 = call i32 @bind(i32 %15, %struct.sockaddr* %17, i32 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %28
20: ; preds = %14
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %25)
br label %27
27: ; preds = %21
br label %53
28: ; preds = %14
%29 = load i32, i32* %4
%30 = call i32 @listen(i32 %29, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = load i32, i32* %4
%35 = call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %33
br label %39
37: ; preds = %28
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %64
39: ; preds = %36
%40 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%41 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @unlink(i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %52
45: ; preds = %39
br label %46
46: ; preds = %45
%47 = load %struct.sockaddr_un*, %struct.sockaddr_un** %3
%48 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %46
br label %52
52: ; preds = %51, %39
br label %53
53: ; preds = %52, %27
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %62
57: ; preds = %53
br label %58
58: ; preds = %57
%59 = load i32, i32* %4
%60 = call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %58
br label %62
62: ; preds = %61, %53
br label %63
63: ; preds = %62, %13
store i32 -1, i32* %2
br label %64
64: ; preds = %63, %37
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [20 x i8] c"bind(%d, %s) failed\00"
@.str.2 = [18 x i8] c"listen(%d) failed\00"
@.str.3 = [18 x i8] c"unlink(%s) failed\00"
@.str.4 = [17 x i8] c"close(%d) failed\00"
define i32 @un_listen(%struct.sockaddr_un* %0) {
%2 = load i32, i32* @AF_UNIX
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %33
8: ; preds = %1
%9 = bitcast %struct.sockaddr_un* %0 to %struct.sockaddr*
%10 = tail call i32 @bind(i32 %4, %struct.sockaddr* %9, i32 4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %14)
br label %28
16: ; preds = %8
%17 = tail call i32 @listen(i32 %4, i32 1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %33
19: ; preds = %16
%20 = tail call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @unlink(i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* %21
%27 = tail call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %19, %25, %12
%29 = tail call i32 @close(i32 %4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = tail call i32 (i8*, ...) @unused(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %4)
br label %33
33: ; preds = %6, %31, %28, %16
%34 = phi i32 [ %4, %16 ], [ -1, %28 ], [ -1, %31 ], [ -1, %6 ]
ret i32 %34
}