#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*/ time_t;
struct stat {
  int st_mode;
  int /*<<< orphan*/ st_atime;
};
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int MAXPATHLEN;
int S_IWRITE;
char *_PATH_DEV;
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ warn(char *, char *);
int term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror) {
  struct stat s;
  char path[MAXPATHLEN];
  (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
  if (stat(path, &s) < 0) {
    if (showerror)
      warn("%s", path);
    return (1);
  }
  *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0;
  *atimeP = s.st_atime;
  return (0);
}


x86 assembly (clang, -O0):
<term_chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    %eax,%esi
mov    0x2e6c(%rip),%rax
mov    (%rax),%rcx
mov    -0x10(%rbp),%r8
lea    0xe76(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x50(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    11d3 <term_chk+0xa3>
cmpl   $0x0,-0x24(%rbp)
je     11c0 <term_chk+0x90>
mov    -0x50(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1030 <warn@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    120c <term_chk+0xdc>
mov    -0x30(%rbp),%eax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%ecx
sar    $0x3,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
setne  %al
and    $0x1,%al
movzbl %al,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_chk>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r8
mov    %rsp,%r13
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e77(%rip),%rax
mov    (%rax),%rcx
lea    0xe85(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
js     11b7 <term_chk+0x87>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
sar    $0x3,%eax
xor    %ecx,%ecx
and    -0x30(%rbp),%eax
setne  %cl
mov    %ecx,(%r12)
mov    -0x2c(%rbp),%eax
mov    %eax,(%r14)
xor    %r14d,%r14d
jmp    11d1 <term_chk+0xa1>
mov    $0x1,%r14d
test   %r15d,%r15d
je     11d1 <term_chk+0xa1>
lea    0xe39(%rip),%rdi
mov    %rbx,%rsi
call   1030 <warn@plt>
mov    %r13,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@MAXPATHLEN = global i32 0
@.str = [5 x i8] c"%s%s\00"
@_PATH_DEV = global i8* null
@.str.1 = [3 x i8] c"%s\00"
@S_IWRITE = global i32 0
define i32 @term_chk(i8* %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%14 = load i32, i32* @MAXPATHLEN
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = trunc i64 %15 to i32
%19 = load i8*, i8** @_PATH_DEV
%20 = load i8*, i8** %6
%21 = call i32 @snprintf(i8* %17, i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20)
%22 = call i64 @stat(i8* %17, %struct.stat* %10)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %4
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %29
29: ; preds = %27, %24
store i32 1, i32* %5
store i32 1, i32* %13
br label %42
30: ; preds = %4
%31 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @S_IWRITE
%34 = ashr i32 %33, 3
%35 = and i32 %32, %34
%36 = icmp ne i32 %35, 0
%37 = zext i1 %36 to i32
%38 = load i32*, i32** %7
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load i32*, i32** %8
store i32 %40, i32* %41
store i32 0, i32* %5
store i32 1, i32* %13
br label %42
42: ; preds = %30, %29
%43 = load i8*, i8** %11
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@MAXPATHLEN = global i32 0
@.str = [5 x i8] c"%s%s\00"
@_PATH_DEV = global i8* null
@.str.1 = [3 x i8] c"%s\00"
@S_IWRITE = global i32 0
define i32 @term_chk(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2, i32 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = load i32, i32* @MAXPATHLEN
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = load i8*, i8** @_PATH_DEV
%12 = call i32 @snprintf(i8* %10, i32 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %0)
%13 = call i64 @stat(i8* %10, %struct.stat* %5)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = icmp eq i32 %3, 0
br i1 %16, label %29, label %17
17: ; preds = %15
%18 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %29
19: ; preds = %4
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @S_IWRITE
%23 = ashr i32 %22, 3
%24 = and i32 %23, %21
%25 = icmp ne i32 %24, 0
%26 = zext i1 %25 to i32
store i32 %26, i32* %1
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%28 = load i32, i32* %27
store i32 %28, i32* %2
br label %29
29: ; preds = %15, %17, %19
%30 = phi i32 [ 0, %19 ], [ 1, %17 ], [ 1, %15 ]
ret i32 %30
}