#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENODEV;
int /*<<< orphan*/ ENOTTY;
int O_NONBLOCK;
int O_RDWR;
int R_OK;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int W_OK;
scalar_t__ access(char const *, int);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int open(char const *, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
scalar_t__ stat(char const *, struct stat *);
char *strrchr(char const *, char);
int wattsup_open(const char *filename, int *fd) {
  assert(filename != NULL);
  assert(fd != NULL);
  struct stat s;
  char buf[32];
  const char *shortname;
  if (stat(filename, &s) < 0) {
    perror(filename);
    return -1;
  }
  if (!S_ISCHR(s.st_mode)) {
    errno = ENOTTY;
    perror("wattsup_open: Not a TTY character device");
    return -1;
  }
  if (access(filename, R_OK | W_OK)) {
    perror(filename);
    return -1;
  }
  if (!(shortname = strrchr(filename, '/'))) {
    errno = EINVAL;
    perror(filename);
    return -1;
  }
  shortname++;
  snprintf(buf, sizeof(buf), "/sys/class/tty/%s", shortname);
  if (stat(buf, &s) < 0) {
    perror(buf);
    return -1;
  }
  if (!S_ISDIR(s.st_mode)) {
    errno = ENODEV;
    perror("wattsup_open: Not a TTY device");
    return -1;
  }
  *fd = open(filename, O_RDWR | O_NONBLOCK);
  if (*fd < 0) {
    perror(filename);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<wattsup_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11f0 <wattsup_open+0x60>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <wattsup_open+0x1f0>
mov    -0x20(%rbp),%edi
call   1080 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    122b <wattsup_open+0x9b>
mov    0x2dd0(%rip),%rax
mov    (%rax),%ecx
mov    0x2daf(%rip),%rax
mov    %ecx,(%rax)
lea    0xde6(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <wattsup_open+0x1f0>
mov    -0x10(%rbp),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
or     (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%rax
je     1265 <wattsup_open+0xd5>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <wattsup_open+0x1f0>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    12a8 <wattsup_open+0x118>
mov    0x2d30(%rip),%rax
mov    (%rax),%ecx
mov    0x2d2f(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <wattsup_open+0x1f0>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    $0x20,%esi
lea    0xd61(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    12f9 <wattsup_open+0x169>
lea    -0x40(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <wattsup_open+0x1f0>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1334 <wattsup_open+0x1a4>
mov    0x2c97(%rip),%rax
mov    (%rax),%ecx
mov    0x2ca6(%rip),%rax
mov    %ecx,(%rax)
lea    0xd18(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <wattsup_open+0x1f0>
mov    -0x10(%rbp),%rdi
mov    0x2ca9(%rip),%rax
mov    (%rax),%esi
mov    0x2c98(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1379 <wattsup_open+0x1e9>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <wattsup_open+0x1f0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wattsup_open>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
js     12ae <wattsup_open+0x11e>
mov    0x8(%rsp),%edi
call   1080 <S_ISCHR@plt>
test   %eax,%eax
je     1281 <wattsup_open+0xf1>
mov    0x2dc7(%rip),%rax
mov    0x2de0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   10a0 <access@plt>
test   %rax,%rax
jne    12ae <wattsup_open+0x11e>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     129c <wattsup_open+0x10c>
add    $0x1,%rax
lea    0xe03(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x20,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1070 <stat@plt>
test   %rax,%rax
js     12c5 <wattsup_open+0x135>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %eax,%eax
je     12cc <wattsup_open+0x13c>
mov    0x2d87(%rip),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ecx
mov    %eax,(%r14)
xor    %eax,%eax
test   %ecx,%ecx
jns    12bb <wattsup_open+0x12b>
jmp    12ae <wattsup_open+0x11e>
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    0x2d2f(%rip),%rcx
mov    %eax,(%rcx)
lea    0xd66(%rip),%rdi
jmp    12b1 <wattsup_open+0x121>
mov    0x2d15(%rip),%rax
mov    (%rax),%eax
mov    0x2d14(%rip),%rcx
mov    %eax,(%rcx)
mov    %rbx,%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0x10(%rsp),%rdi
jmp    12b1 <wattsup_open+0x121>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    0x2ce4(%rip),%rcx
mov    %eax,(%rcx)
lea    0xd56(%rip),%rdi
jmp    12b1 <wattsup_open+0x121>

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOTTY = global i32 0
@errno = global i32 0
@.str = [41 x i8] c"wattsup_open: Not a TTY character device\00"
@R_OK = global i32 0
@W_OK = global i32 0
@EINVAL = global i32 0
@.str.1 = [18 x i8] c"/sys/class/tty/%s\00"
@ENODEV = global i32 0
@.str.2 = [31 x i8] c"wattsup_open: Not a TTY device\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
define i32 @wattsup_open(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.stat
%7 = alloca [32 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %4
%18 = call i64 @stat(i8* %17, %struct.stat* %6)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32 @perror(i8* %21)
store i32 -1, i32* %3
br label %83
23: ; preds = %2
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISCHR(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32, i32* @ENOTTY
store i32 %29, i32* @errno
%30 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
31: ; preds = %23
%32 = load i8*, i8** %4
%33 = load i32, i32* @R_OK
%34 = load i32, i32* @W_OK
%35 = or i32 %33, %34
%36 = call i64 @access(i8* %32, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load i8*, i8** %4
%40 = call i32 @perror(i8* %39)
store i32 -1, i32* %3
br label %83
41: ; preds = %31
%42 = load i8*, i8** %4
%43 = call i8* @strrchr(i8* %42, i8 signext 47)
store i8* %43, i8** %8
%44 = icmp ne i8* %43, null
br i1 %44, label %49, label %45
45: ; preds = %41
%46 = load i32, i32* @EINVAL
store i32 %46, i32* @errno
%47 = load i8*, i8** %4
%48 = call i32 @perror(i8* %47)
store i32 -1, i32* %3
br label %83
49: ; preds = %41
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %8
%52 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%53 = load i8*, i8** %8
%54 = call i32 @snprintf(i8* %52, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %53)
%55 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%56 = call i64 @stat(i8* %55, %struct.stat* %6)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %49
%59 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%60 = call i32 @perror(i8* %59)
store i32 -1, i32* %3
br label %83
61: ; preds = %49
%62 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i32 @S_ISDIR(i32 %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %61
%67 = load i32, i32* @ENODEV
store i32 %67, i32* @errno
%68 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
69: ; preds = %61
%70 = load i8*, i8** %4
%71 = load i32, i32* @O_RDWR
%72 = load i32, i32* @O_NONBLOCK
%73 = or i32 %71, %72
%74 = call i32 @open(i8* %70, i32 %73)
%75 = load i32*, i32** %5
store i32 %74, i32* %75
%76 = load i32*, i32** %5
%77 = load i32, i32* %76
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %69
%80 = load i8*, i8** %4
%81 = call i32 @perror(i8* %80)
store i32 -1, i32* %3
br label %83
82: ; preds = %69
store i32 0, i32* %3
br label %83
83: ; preds = %82, %79, %66, %58, %45, %38, %28, %20
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOTTY = global i32 0
@errno = global i32 0
@.str = [41 x i8] c"wattsup_open: Not a TTY character device\00"
@R_OK = global i32 0
@W_OK = global i32 0
@EINVAL = global i32 0
@.str.1 = [18 x i8] c"/sys/class/tty/%s\00"
@ENODEV = global i32 0
@.str.2 = [31 x i8] c"wattsup_open: Not a TTY device\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
define i32 @wattsup_open(i8* %0, i32* writeonly %1) {
%3 = alloca %struct.stat
%4 = alloca [32 x i8]
%5 = icmp ne i8* %0, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp ne i32* %1, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = bitcast %struct.stat* %3 to i8*
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%13 = call i64 @stat(i8* %0, %struct.stat* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* %0)
br label %61
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @S_ISCHR(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @ENOTTY
store i32 %23, i32* @errno
%24 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %61
25: ; preds = %17
%26 = load i32, i32* @R_OK
%27 = load i32, i32* @W_OK
%28 = or i32 %27, %26
%29 = call i64 @access(i8* %0, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = call i32 @perror(i8* %0)
br label %61
33: ; preds = %25
%34 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* @EINVAL
store i32 %37, i32* @errno
%38 = call i32 @perror(i8* %0)
br label %61
39: ; preds = %33
%40 = getelementptr inbounds i8, i8* %34, i64 1
%41 = call i32 @snprintf(i8* %12, i32 32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i64 @stat(i8* %12, %struct.stat* %3)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @perror(i8* %12)
br label %61
46: ; preds = %39
%47 = load i32, i32* %18
%48 = call i32 @S_ISDIR(i32 %47)
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* @ENODEV
store i32 %51, i32* @errno
%52 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %61
53: ; preds = %46
%54 = load i32, i32* @O_RDWR
%55 = load i32, i32* @O_NONBLOCK
%56 = or i32 %55, %54
%57 = call i32 @open(i8* %0, i32 %56)
store i32 %57, i32* %1
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %53
%60 = call i32 @perror(i8* %0)
br label %61
61: ; preds = %53, %59, %50, %44, %36, %31, %22, %15
%62 = phi i32 [ -1, %15 ], [ -1, %31 ], [ -1, %44 ], [ -1, %59 ], [ -1, %50 ], [ -1, %36 ], [ -1, %22 ], [ 0, %53 ]
ret i32 %62
}