#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;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror) {
  struct stat s;
  char path[MAXPATHLEN];
  if (strlen(tty) + 6 > sizeof(path))
    return (1);
  (void)sprintf(path, "/dev/%s", tty);
  if (stat(path, &s) < 0) {
    if (showerror)
      (void)fprintf(stderr, "write: %s: %s\n", path, strerror(errno));
    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    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%rcx
add    $0x6,%eax
cltq
cmp    %rcx,%rax
jbe    11c5 <term_chk+0x75>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1276 <term_chk+0x126>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe2c(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x58(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    123d <term_chk+0xed>
cmpl   $0x0,-0x24(%rbp)
je     122a <term_chk+0xda>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xde3(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1276 <term_chk+0x126>
mov    -0x30(%rbp),%eax
mov    0x2da1(%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
sub    $0x28,%rsp
mov    %ecx,-0x34(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,-0x30(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1030 <strlen@plt>
add    $0x6,%eax
cltq
mov    $0x1,%r12d
cmp    %r13,%rax
ja     1220 <term_chk+0xd0>
mov    -0x34(%rbp),%r13d
lea    0xe53(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <sprintf@plt>
lea    -0x40(%rbp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
js     11ee <term_chk+0x9e>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
sar    $0x3,%eax
xor    %ecx,%ecx
and    -0x40(%rbp),%eax
setne  %cl
mov    %ecx,(%r15)
mov    -0x3c(%rbp),%eax
mov    -0x48(%rbp),%rcx
mov    %eax,(%rcx)
xor    %r12d,%r12d
jmp    1220 <term_chk+0xd0>
test   %r13d,%r13d
je     1220 <term_chk+0xd0>
mov    0x2dd6(%rip),%rax
mov    (%rax),%r14d
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdf6(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %r12d,%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 = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"write: %s: %s\0A\00"
@errno = global i32 0
@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 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %19, 6
%21 = sext i32 %20 to i64
%22 = icmp ugt i64 %21, %15
br i1 %22, label %23, label %24
23: ; preds = %4
store i32 1, i32* %5
store i32 1, i32* %13
br label %50
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i64 @stat(i8* %17, %struct.stat* %10)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %24
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %35)
br label %37
37: ; preds = %32, %29
store i32 1, i32* %5
store i32 1, i32* %13
br label %50
38: ; preds = %24
%39 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @S_IWRITE
%42 = ashr i32 %41, 3
%43 = and i32 %40, %42
%44 = icmp ne i32 %43, 0
%45 = zext i1 %44 to i32
%46 = load i32*, i32** %7
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%48 = load i32, i32* %47
%49 = load i32*, i32** %8
store i32 %48, i32* %49
store i32 0, i32* %5
store i32 1, i32* %13
br label %50
50: ; preds = %38, %37, %23
%51 = load i8*, i8** %11
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@MAXPATHLEN = global i32 0
@.str = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"write: %s: %s\0A\00"
@errno = global i32 0
@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 = tail call i32 @strlen(i8* %0)
%12 = add nsw i32 %11, 6
%13 = sext i32 %12 to i64
%14 = icmp ugt i64 %13, %8
br i1 %14, label %36, label %15
15: ; preds = %4
%16 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = call i64 @stat(i8* %10, %struct.stat* %5)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = icmp eq i32 %3, 0
br i1 %20, label %36, label %21
21: ; preds = %19
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %24)
br label %36
26: ; preds = %15
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @S_IWRITE
%30 = ashr i32 %29, 3
%31 = and i32 %30, %28
%32 = icmp ne i32 %31, 0
%33 = zext i1 %32 to i32
store i32 %33, i32* %1
%34 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%35 = load i32, i32* %34
store i32 %35, i32* %2
br label %36
36: ; preds = %19, %21, %4, %26
%37 = phi i32 [ 0, %26 ], [ 1, %4 ], [ 1, %21 ], [ 1, %19 ]
ret i32 %37
}