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

/* Variables and functions */
int EAGAIN;
int EINTR;
int EWOULDBLOCK;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ Sactivate_thread();
int /*<<< orphan*/ Sdeactivate_thread();
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int ss_accept_unix_connection_impl(int sock) {
  struct sockaddr_un addr;
  memset(&addr, 0, sizeof(addr));
  socklen_t addr_len = sizeof(addr);
  Sdeactivate_thread();
  int connect_sock;
  do {
    connect_sock = accept(sock, (struct sockaddr *)&addr, &addr_len);
    if (connect_sock != -1) {
      fcntl(connect_sock, F_SETFD, fcntl(connect_sock, F_GETFD) | FD_CLOEXEC);
    }
  } while (connect_sock == -1 && errno == EINTR);
  int saved_errno = errno;
  Sactivate_thread();
  if (addr_len > sizeof(addr)) {
    close(connect_sock);
    errno = saved_errno;
    return -1;
  }
  if (connect_sock == -1) {
    errno = saved_errno;
    if (saved_errno == EAGAIN || saved_errno == EWOULDBLOCK)
      return -2;
    return -1;
  }
  return connect_sock;
}


x86 assembly (clang, -O0):
<ss_accept_unix_connection_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x4,-0x14(%rbp)
mov    $0x0,%al
call   1030 <Sdeactivate_thread@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     11e3 <ss_accept_unix_connection_impl+0x83>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    %eax,%edx
mov    0x2dfe(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
jmp    11e8 <ss_accept_unix_connection_impl+0x88>
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x25(%rbp)
jne    120f <ss_accept_unix_connection_impl+0xaf>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1189 <ss_accept_unix_connection_impl+0x29>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <Sactivate_thread@plt>
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jbe    125b <ss_accept_unix_connection_impl+0xfb>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d83(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <ss_accept_unix_connection_impl+0x153>
cmpl   $0xffffffff,-0x18(%rbp)
jne    12ad <ss_accept_unix_connection_impl+0x14d>
mov    -0x1c(%rbp),%ecx
mov    0x2d61(%rip),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%eax
je     1295 <ss_accept_unix_connection_impl+0x135>
mov    -0x1c(%rbp),%eax
mov    0x2d3b(%rip),%rcx
cmp    (%rcx),%eax
jne    12a1 <ss_accept_unix_connection_impl+0x141>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12b3 <ss_accept_unix_connection_impl+0x153>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <ss_accept_unix_connection_impl+0x153>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_accept_unix_connection_impl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r13d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x4,0xc(%rsp)
xor    %eax,%eax
call   1030 <Sdeactivate_thread@plt>
lea    0xc(%rsp),%r15
mov    0x2e18(%rip),%r12
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11fd <ss_accept_unix_connection_impl+0x9d>
mov    0x2e14(%rip),%rax
mov    (%rax),%ebp
cmp    (%r12),%ebp
je     11a0 <ss_accept_unix_connection_impl+0x40>
xor    %eax,%eax
call   1070 <Sactivate_thread@plt>
cmpl   $0x4,0xc(%rsp)
mov    0x2df9(%rip),%r14
ja     1246 <ss_accept_unix_connection_impl+0xe6>
mov    %ebp,(%r14)
mov    0x2dc5(%rip),%rax
cmp    (%rax),%ebp
sete   %al
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%ebp
sete   %cl
or     %al,%cl
movzbl %cl,%ebx
not    %ebx
jmp    1255 <ss_accept_unix_connection_impl+0xf5>
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebp
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2db9(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d9b(%rip),%r14
mov    (%r14),%ebp
xor    %eax,%eax
call   1070 <Sactivate_thread@plt>
cmpl   $0x5,0xc(%rsp)
jb     1255 <ss_accept_unix_connection_impl+0xf5>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,(%r14)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
define i32 @ss_accept_unix_connection_impl(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 4)
store i32 4, i32* %5
%9 = call i32 (...) @Sdeactivate_thread()
br label %10
10: ; preds = %33, %1
%11 = load i32, i32* %3
%12 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%13 = call i32 @accept(i32 %11, %struct.sockaddr* %12, i32* %5)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %25
16: ; preds = %10
%17 = load i32, i32* %6
%18 = load i32, i32* @F_SETFD
%19 = load i32, i32* %6
%20 = load i32, i32* @F_GETFD
%21 = call i32 (i32, i32, ...) @fcntl(i32 %19, i32 %20)
%22 = load i32, i32* @FD_CLOEXEC
%23 = or i32 %21, %22
%24 = call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %18, i32 %23)
br label %25
25: ; preds = %16, %10
br label %26
26: ; preds = %25
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = load i32, i32* @EINTR
%32 = icmp eq i32 %30, %31
br label %33
33: ; preds = %29, %26
%34 = phi i1 [ false, %26 ], [ %32, %29 ]
br i1 %34, label %10, label %35
35: ; preds = %33
%36 = load i32, i32* @errno
store i32 %36, i32* %7
%37 = call i32 (...) @Sactivate_thread()
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = icmp ugt i64 %39, 4
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %7
store i32 %44, i32* @errno
store i32 -1, i32* %2
br label %61
45: ; preds = %35
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %59
48: ; preds = %45
%49 = load i32, i32* %7
store i32 %49, i32* @errno
%50 = load i32, i32* %7
%51 = load i32, i32* @EAGAIN
%52 = icmp eq i32 %50, %51
br i1 %52, label %57, label %53
53: ; preds = %48
%54 = load i32, i32* %7
%55 = load i32, i32* @EWOULDBLOCK
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %53, %48
store i32 -2, i32* %2
br label %61
58: ; preds = %53
store i32 -1, i32* %2
br label %61
59: ; preds = %45
%60 = load i32, i32* %6
store i32 %60, i32* %2
br label %61
61: ; preds = %59, %58, %57, %41
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
define i32 @ss_accept_unix_connection_impl(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 4)
%6 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%7 = call i32 (...) @Sdeactivate_thread()
%8 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
br label %9
9: ; preds = %12, %1
%10 = call i32 @accept(i32 %0, %struct.sockaddr* %8, i32* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = load i32, i32* @EINTR
%15 = icmp eq i32 %13, %14
br i1 %15, label %9, label %16
16: ; preds = %12
%17 = call i32 (...) @Sactivate_thread()
%18 = load i32, i32* %3
%19 = icmp ugt i32 %18, 4
br i1 %19, label %31, label %34
20: ; preds = %9
%21 = load i32, i32* @F_SETFD
%22 = load i32, i32* @F_GETFD
%23 = call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %22)
%24 = load i32, i32* @FD_CLOEXEC
%25 = or i32 %24, %23
%26 = call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %21, i32 %25)
%27 = load i32, i32* @errno
%28 = call i32 (...) @Sactivate_thread()
%29 = load i32, i32* %3
%30 = icmp ugt i32 %29, 4
br i1 %30, label %31, label %41
31: ; preds = %20, %16
%32 = phi i32 [ %27, %20 ], [ %13, %16 ]
%33 = call i32 @close(i32 %10)
store i32 %32, i32* @errno
br label %41
34: ; preds = %16
store i32 %13, i32* @errno
%35 = load i32, i32* @EAGAIN
%36 = icmp eq i32 %13, %35
%37 = load i32, i32* @EWOULDBLOCK
%38 = icmp eq i32 %13, %37
%39 = select i1 %36, i1 true, i1 %38
%40 = select i1 %39, i32 -2, i32 -1
br label %41
41: ; preds = %20, %34, %31
%42 = phi i32 [ -1, %31 ], [ %40, %34 ], [ %10, %20 ]
ret i32 %42
}