#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 scalar_t__ time_t;
struct stat {
  int st_mode;
  scalar_t__ st_atime;
  scalar_t__ st_mtime;
  scalar_t__ st_ctime;
};
struct sockaddr_un {
  char *sun_path;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ EPERM;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ S_ISSOCK(int);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
int accept_client(int servfd) {
  struct sockaddr_un client_addr;
  socklen_t client_addr_sz = sizeof(client_addr);
  static char buff[sizeof(client_addr.sun_path) + 1];
  int clientfd;
  if ((clientfd = accept(servfd, (struct sockaddr *)&client_addr,
                         &client_addr_sz)) < 0) {
    return -1;
  }
  sprintf(buff, "%s", client_addr.sun_path);
  struct stat statbuff;
  if (stat(buff, &statbuff) < 0)
    goto errout;
  unlink(buff);
  if (!S_ISSOCK(statbuff.st_mode)) {
    errno = EINVAL;
    goto errout;
  }
  if ((statbuff.st_mode & (S_IRWXG | S_IRWXO)) ||
      ((statbuff.st_mode & S_IRWXU) != S_IRWXU)) {
    errno = EPERM;
    goto errout;
  }
  time_t lim = time(NULL) - 10;
  if (statbuff.st_atime < lim || statbuff.st_mtime < lim ||
      statbuff.st_ctime < lim) {
    errno = EPERM;
    goto errout;
  }
  return clientfd;
errout:
  close(clientfd);
  return -1;
}


x86 assembly (clang, -O0):
<accept_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x8,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11aa <accept_client+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <accept_client+0x162>
mov    -0x10(%rbp),%rdx
lea    0x2ea7(%rip),%rdi
lea    0xe44(%rip),%rsi
call   1090 <sprintf@plt>
lea    0x2e94(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11e0 <accept_client+0x70>
jmp    12c3 <accept_client+0x153>
lea    0x2e75(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x38(%rbp),%edi
call   1040 <S_ISSOCK@plt>
cmp    $0x0,%eax
jne    1214 <accept_client+0xa4>
mov    0x2dbc(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbb(%rip),%rax
mov    %ecx,(%rax)
jmp    12c3 <accept_client+0x153>
mov    -0x38(%rbp),%eax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dc9(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    124f <accept_client+0xdf>
mov    -0x38(%rbp),%eax
mov    0x2da2(%rip),%rcx
and    (%rcx),%eax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%eax
je     1266 <accept_client+0xf6>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d69(%rip),%rax
mov    %ecx,(%rax)
jmp    12c3 <accept_client+0x153>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
sub    $0xa,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x40(%rbp),%rax
jl     12a1 <accept_client+0x131>
mov    -0x28(%rbp),%rax
cmp    -0x40(%rbp),%rax
jl     12a1 <accept_client+0x131>
mov    -0x20(%rbp),%rax
cmp    -0x40(%rbp),%rax
jge    12b8 <accept_client+0x148>
mov    0x2d08(%rip),%rax
mov    (%rax),%ecx
mov    0x2d17(%rip),%rax
mov    %ecx,(%rax)
jmp    12c3 <accept_client+0x153>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d2 <accept_client+0x162>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_client>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1090 <accept@plt>
test   %eax,%eax
js     1241 <accept_client+0xd1>
mov    %eax,%ebx
mov    0x10(%rsp),%rsi
lea    0x2eb8(%rip),%r14
mov    %r14,%rdi
call   1040 <strcpy@plt>
lea    0x18(%rsp),%rsi
mov    %r14,%rdi
call   1070 <stat@plt>
test   %rax,%rax
js     123a <accept_client+0xca>
lea    0x2e97(%rip),%rdi
call   1030 <unlink@plt>
mov    0x18(%rsp),%edi
call   1050 <S_ISSOCK@plt>
test   %eax,%eax
je     1227 <accept_client+0xb7>
mov    0x18(%rsp),%eax
mov    0x2df6(%rip),%rcx
mov    0x2e07(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dbc(%rip),%r14
test   %eax,%edx
jne    122e <accept_client+0xbe>
mov    0x2de1(%rip),%rcx
not    %eax
test   %eax,(%rcx)
jne    122e <accept_client+0xbe>
xor    %edi,%edi
call   1080 <time@plt>
add    $0xfffffffffffffff6,%rax
cmp    %rax,0x20(%rsp)
jl     122e <accept_client+0xbe>
cmp    %rax,0x28(%rsp)
jl     122e <accept_client+0xbe>
cmp    %rax,0x30(%rsp)
jge    1246 <accept_client+0xd6>
jmp    122e <accept_client+0xbe>
mov    0x2d92(%rip),%r14
mov    (%r14),%eax
mov    0x2d90(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8* }
%struct.stat = type { i32, i64, i64, i64 }
%struct.sockaddr = type { i32 }
@accept_client.buff = internal global [9 x i8] zeroinitializer
@.str = [3 x i8] c"%s\00"
@EINVAL = global i32 0
@errno = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@S_IRWXU = global i32 0
@EPERM = global i32 0
define i32 @accept_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i64
store i32 %0, i32* %3
store i32 8, i32* %5
%9 = load i32, i32* %3
%10 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %5)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %70
14: ; preds = %1
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @accept_client.buff, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i64 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @accept_client.buff, i64 0, i64 0), %struct.stat* %7)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
br label %67
21: ; preds = %14
%22 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @accept_client.buff, i64 0, i64 0))
%23 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @S_ISSOCK(i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %21
%28 = load i32, i32* @EINVAL
store i32 %28, i32* @errno
br label %67
29: ; preds = %21
%30 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @S_IRWXG
%33 = load i32, i32* @S_IRWXO
%34 = or i32 %32, %33
%35 = and i32 %31, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %29
%38 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @S_IRWXU
%41 = and i32 %39, %40
%42 = load i32, i32* @S_IRWXU
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %37, %29
%45 = load i32, i32* @EPERM
store i32 %45, i32* @errno
br label %67
46: ; preds = %37
%47 = call i64 @time(i32* null)
%48 = sub nsw i64 %47, 10
store i64 %48, i64* %8
%49 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%50 = load i64, i64* %49
%51 = load i64, i64* %8
%52 = icmp slt i64 %50, %51
br i1 %52, label %63, label %53
53: ; preds = %46
%54 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 2
%55 = load i64, i64* %54
%56 = load i64, i64* %8
%57 = icmp slt i64 %55, %56
br i1 %57, label %63, label %58
58: ; preds = %53
%59 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 3
%60 = load i64, i64* %59
%61 = load i64, i64* %8
%62 = icmp slt i64 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %58, %53, %46
%64 = load i32, i32* @EPERM
store i32 %64, i32* @errno
br label %67
65: ; preds = %58
%66 = load i32, i32* %6
store i32 %66, i32* %2
br label %70
67: ; preds = %63, %44, %27, %20
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %2
br label %70
70: ; preds = %67, %65, %13
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8* }
%struct.stat = type { i32, i64, i64, i64 }
%struct.sockaddr = type { i32 }
@accept_client.buff = internal global [9 x i8] zeroinitializer
@EINVAL = global i32 0
@errno = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@S_IRWXU = global i32 0
@EPERM = global i32 0
define i32 @accept_client(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 8, i32* %3
%7 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%8 = call i32 @accept(i32 %0, %struct.sockaddr* %7, i32* %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %55, label %10
10: ; preds = %1
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = call i8* @strcpy(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @accept_client.buff, i64 0, i64 0), i8* dereferenceable(1) %12)
%14 = bitcast %struct.stat* %4 to i8*
%15 = call i64 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @accept_client.buff, i64 0, i64 0), %struct.stat* %4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %51, label %17
17: ; preds = %10
%18 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @accept_client.buff, i64 0, i64 0))
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @S_ISSOCK(i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %48, label %23
23: ; preds = %17
%24 = load i32, i32* %19
%25 = load i32, i32* @S_IRWXG
%26 = load i32, i32* @S_IRWXO
%27 = or i32 %26, %25
%28 = and i32 %27, %24
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %48
30: ; preds = %23
%31 = load i32, i32* @S_IRWXU
%32 = and i32 %31, %24
%33 = icmp eq i32 %32, %31
br i1 %33, label %34, label %48
34: ; preds = %30
%35 = call i64 @time(i32* null)
%36 = add nsw i64 %35, -10
%37 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%38 = load i64, i64* %37
%39 = icmp slt i64 %38, %36
%40 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 2
%41 = load i64, i64* %40
%42 = icmp slt i64 %41, %36
%43 = select i1 %39, i1 true, i1 %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 3
%45 = load i64, i64* %44
%46 = icmp slt i64 %45, %36
%47 = select i1 %43, i1 true, i1 %46
br i1 %47, label %48, label %53
48: ; preds = %34, %23, %30, %17
%49 = phi i32* [ @EINVAL, %17 ], [ @EPERM, %30 ], [ @EPERM, %23 ], [ @EPERM, %34 ]
%50 = load i32, i32* %49
store i32 %50, i32* @errno
br label %51
51: ; preds = %48, %10
%52 = call i32 @close(i32 %8)
br label %53
53: ; preds = %34, %51
%54 = phi i32 [ -1, %51 ], [ %8, %34 ]
br label %55
55: ; preds = %1, %53
%56 = phi i32 [ %54, %53 ], [ -1, %1 ]
ret i32 %56
}