#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 timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_un {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int unix_accept_t(int sock, struct timeval wait, struct sockaddr_un *client) {
  fd_set rset;
  struct timeval tv;
  int ret = -1;
  static socklen_t client_len = sizeof(struct sockaddr_un);
  int csock = -1;
  tv.tv_sec = wait.tv_sec;
  tv.tv_usec = wait.tv_usec;
RE_DO:
  if (tv.tv_sec || tv.tv_usec) {
    FD_ZERO(&rset);
    FD_SET(sock, &rset);
    ret = select(sock + 1, &rset, (fd_set *)0, (fd_set *)0, &tv);
    if (ret == -1) {
      if (errno == EINTR) {
        goto RE_DO;
      }
      return -1;
    } else if (ret == 0) {
      return 0;
    }
  }
  csock = accept(sock, (struct sockaddr *)client, &client_len);
  if (csock < 0) {
    return -1;
  }
  return csock;
}


x86 assembly (clang, -O0):
<unix_accept_t>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %edi,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    118b <unix_accept_t+0x4b>
cmpq   $0x0,-0x40(%rbp)
je     1212 <unix_accept_t+0xd2>
lea    -0x2c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    11f2 <unix_accept_t+0xb2>
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <unix_accept_t+0xa6>
jmp    1175 <unix_accept_t+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <unix_accept_t+0x104>
cmpl   $0x0,-0x44(%rbp)
jne    1208 <unix_accept_t+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    1244 <unix_accept_t+0x104>
jmp    120d <unix_accept_t+0xcd>
jmp    1212 <unix_accept_t+0xd2>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
lea    0x2e20(%rip),%rdx
call   1050 <accept@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    123e <unix_accept_t+0xfe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <unix_accept_t+0x104>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_accept_t>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x18(%rsp)
mov    %edi,%ebx
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
or     %rsi,%rdx
je     11c4 <unix_accept_t+0x84>
lea    0x1(%rbx),%r15d
lea    0x24(%rsp),%rbp
lea    0x8(%rsp),%r12
mov    0x2e67(%rip),%r13
mov    0x2e58(%rip),%r14
mov    %rbp,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1040 <select@plt>
cmp    $0xffffffff,%eax
jne    11c0 <unix_accept_t+0x80>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    11f2 <unix_accept_t+0xb2>
mov    0x10(%rsp),%rax
or     0x8(%rsp),%rax
jne    1180 <unix_accept_t+0x40>
jmp    11c4 <unix_accept_t+0x84>
test   %eax,%eax
je     11e3 <unix_accept_t+0xa3>
lea    0x2e75(%rip),%rdx
mov    %ebx,%edi
mov    0x18(%rsp),%rsi
call   1050 <accept@plt>
mov    %eax,%ecx
test   %eax,%eax
mov    $0xffffffff,%eax
cmovns %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    11e3 <unix_accept_t+0xa3>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@unix_accept_t.client_len = internal global i32 4
@errno = global i64 0
@EINTR = global i64 0
define i32 @unix_accept_t(i32 %0, i64 %1, i64 %2, %struct.sockaddr_un* %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca %struct.sockaddr_un*
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i32
%13 = bitcast %struct.timeval* %6 to { i64, i64 }*
%14 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %13, i32 0, i32 0
store i64 %1, i64* %14
%15 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %13, i32 0, i32 1
store i64 %2, i64* %15
store i32 %0, i32* %7
store %struct.sockaddr_un* %3, %struct.sockaddr_un** %8
store i32 -1, i32* %11
store i32 -1, i32* %12
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 %17, i64* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i64 %20, i64* %21
br label %22
22: ; preds = %43, %4
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%24 = load i64, i64* %23
%25 = icmp ne i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%28 = load i64, i64* %27
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %51
30: ; preds = %26, %22
%31 = call i32 @FD_ZERO(i32* %9)
%32 = load i32, i32* %7
%33 = call i32 @FD_SET(i32 %32, i32* %9)
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
%36 = call i32 @select(i32 %35, i32* %9, i32* null, i32* null, %struct.timeval* %10)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %30
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
br label %22
44: ; preds = %39
store i32 -1, i32* %5
br label %61
45: ; preds = %30
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
store i32 0, i32* %5
br label %61
49: ; preds = %45
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50, %26
%52 = load i32, i32* %7
%53 = load %struct.sockaddr_un*, %struct.sockaddr_un** %8
%54 = bitcast %struct.sockaddr_un* %53 to %struct.sockaddr*
%55 = call i32 @accept(i32 %52, %struct.sockaddr* %54, i32* @unix_accept_t.client_len)
store i32 %55, i32* %12
%56 = load i32, i32* %12
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %51
store i32 -1, i32* %5
br label %61
59: ; preds = %51
%60 = load i32, i32* %12
store i32 %60, i32* %5
br label %61
61: ; preds = %59, %58, %48, %44
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@unix_accept_t.client_len = internal global i32 4
@errno = global i64 0
@EINTR = global i64 0
define i32 @unix_accept_t(i32 %0, i64 %1, i64 %2, %struct.sockaddr_un* %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 %2, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i64 %1, i64* %10
%11 = add nsw i32 %0, 1
%12 = icmp ne i64 %2, 0
%13 = icmp ne i64 %1, 0
%14 = select i1 %12, i1 true, i1 %13
br i1 %14, label %15, label %29
15: ; preds = %4, %23
%16 = call i32 @FD_ZERO(i32* %5)
%17 = call i32 @FD_SET(i32 %0, i32* %5)
%18 = call i32 @select(i32 %11, i32* %5, i32* null, i32* null, %struct.timeval* %6)
switch i32 %18, label %29 [
i32 -1, label %19
i32 0, label %34
]
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i64, i64* %9
%25 = load i64, i64* %10
%26 = icmp ne i64 %24, 0
%27 = icmp ne i64 %25, 0
%28 = select i1 %26, i1 true, i1 %27
br i1 %28, label %15, label %29
29: ; preds = %23, %15, %4
%30 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%31 = call i32 @accept(i32 %0, %struct.sockaddr* %30, i32* @unix_accept_t.client_len)
%32 = icmp sgt i32 %31, -1
%33 = select i1 %32, i32 %31, i32 -1
br label %34
34: ; preds = %15, %19, %29
%35 = phi i32 [ %33, %29 ], [ %18, %15 ], [ -1, %19 ]
ret i32 %35
}