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

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ VT_GETSTATE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int ioctl(int, int /*<<< orphan*/, struct vt_stat *);
int /*<<< orphan*/ isatty(int);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int video_vt_active(int vt) {
  int fd;
  char buf[16];
  struct vt_stat vtstat;
  int ret;
  ret = snprintf(buf, sizeof(buf), "/dev/tty%d", vt);
  if ((ret < 0) || (ret >= sizeof(buf)))
    return 1;
  fd = open(buf, O_RDWR);
  if ((fd < 0) && (errno == EACCES))
    fd = open(buf, O_RDONLY);
  if ((fd < 0) && (errno == EACCES))
    fd = open(buf, O_WRONLY);
  if (fd < 0)
    return 0;
  if (!isatty(fd)) {
    close(fd);
    return 0;
  }
  ret = ioctl(fd, VT_GETSTATE, &vtstat);
  close(fd);
  if (ret < 0)
    return 1;
  return (vt == vtstat.v_active);
}


x86 assembly (clang, -O0):
<video_vt_active>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x10,%esi
lea    0xe92(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     118e <video_vt_active+0x3e>
movslq -0x2c(%rbp),%rax
cmp    $0x10,%rax
jb     119a <video_vt_active+0x4a>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <video_vt_active+0x153>
lea    -0x20(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e8 <video_vt_active+0x98>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <video_vt_active+0x98>
lea    -0x20(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1221 <video_vt_active+0xd1>
mov    0x2dcf(%rip),%rax
mov    (%rax),%rax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <video_vt_active+0xd1>
lea    -0x20(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1237 <video_vt_active+0xe7>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <video_vt_active+0x153>
mov    -0xc(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    125c <video_vt_active+0x10c>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <video_vt_active+0x153>
mov    -0xc(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
jge    1292 <video_vt_active+0x142>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <video_vt_active+0x153>
mov    -0x8(%rbp),%eax
cmp    -0x28(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<video_vt_active>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r14d
lea    0xe9c(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
mov    $0x1,%ebx
cmp    $0xf,%eax
ja     1248 <video_vt_active+0xf8>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11e1 <video_vt_active+0x91>
mov    %ebp,%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     123f <video_vt_active+0xef>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%ebx
test   %r15d,%r15d
js     1248 <video_vt_active+0xf8>
xor    %ebx,%ebx
cmp    %r14d,0x8(%rsp)
sete   %bl
jmp    1248 <video_vt_active+0xf8>
mov    0x2de0(%rip),%r15
mov    (%r15),%rax
mov    0x2de6(%rip),%rbx
mov    (%rbx),%rcx
cmp    %rcx,%rax
jne    1219 <video_vt_active+0xc9>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    119d <video_vt_active+0x4d>
mov    (%r15),%rax
mov    (%rbx),%rcx
xor    %ebx,%ebx
cmp    %rcx,%rax
jne    1248 <video_vt_active+0xf8>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    119d <video_vt_active+0x4d>
jmp    1248 <video_vt_active+0xf8>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.vt_stat = type { i32 }
@.str = [11 x i8] c"/dev/tty%d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@VT_GETSTATE = global i32 0
define i32 @video_vt_active(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
%6 = alloca %struct.vt_stat
%7 = alloca i32
store i32 %0, i32* %3
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = icmp uge i64 %15, 16
br i1 %16, label %17, label %18
17: ; preds = %13, %1
store i32 1, i32* %2
br label %69
18: ; preds = %13
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %18
%25 = load i64, i64* @errno
%26 = load i64, i64* @EACCES
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %4
br label %32
32: ; preds = %28, %24, %18
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EACCES
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%41 = load i32, i32* @O_WRONLY
%42 = call i32 @open(i8* %40, i32 %41)
store i32 %42, i32* %4
br label %43
43: ; preds = %39, %35, %32
%44 = load i32, i32* %4
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 0, i32* %2
br label %69
47: ; preds = %43
%48 = load i32, i32* %4
%49 = call i32 @isatty(i32 %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %47
%52 = load i32, i32* %4
%53 = call i32 @close(i32 %52)
store i32 0, i32* %2
br label %69
54: ; preds = %47
%55 = load i32, i32* %4
%56 = load i32, i32* @VT_GETSTATE
%57 = call i32 @ioctl(i32 %55, i32 %56, %struct.vt_stat* %6)
store i32 %57, i32* %7
%58 = load i32, i32* %4
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %7
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %54
store i32 1, i32* %2
br label %69
63: ; preds = %54
%64 = load i32, i32* %3
%65 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %6, i32 0, i32 0
%66 = load i32, i32* %65
%67 = icmp eq i32 %64, %66
%68 = zext i1 %67 to i32
store i32 %68, i32* %2
br label %69
69: ; preds = %63, %62, %51, %46, %17
%70 = load i32, i32* %2
ret i32 %70
}

LLVM IR (-O2):
%struct.vt_stat = type { i32 }
@.str = [11 x i8] c"/dev/tty%d\00"
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@VT_GETSTATE = global i32 0
define i32 @video_vt_active(i32 %0) {
%2 = alloca [16 x i8]
%3 = alloca %struct.vt_stat
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.vt_stat* %3 to i8*
%6 = call i32 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp ugt i32 %6, 15
br i1 %7, label %47, label %8
8: ; preds = %1
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %4, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @EACCES
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %4, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EACCES
br label %23
23: ; preds = %20, %12
%24 = phi i64 [ %22, %20 ], [ %14, %12 ]
%25 = phi i64 [ %21, %20 ], [ %13, %12 ]
%26 = icmp eq i64 %25, %24
br i1 %26, label %27, label %47
27: ; preds = %23
%28 = load i32, i32* @O_WRONLY
%29 = call i32 @open(i8* %4, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %47, label %31
31: ; preds = %8, %16, %27
%32 = phi i32 [ %29, %27 ], [ %18, %16 ], [ %10, %8 ]
%33 = call i32 @isatty(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @close(i32 %32)
br label %47
37: ; preds = %31
%38 = load i32, i32* @VT_GETSTATE
%39 = call i32 @ioctl(i32 %32, i32 %38, %struct.vt_stat* %3)
%40 = call i32 @close(i32 %32)
%41 = icmp slt i32 %39, 0
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %3, i64 0, i32 0
%44 = load i32, i32* %43
%45 = icmp eq i32 %44, %0
%46 = zext i1 %45 to i32
br label %47
47: ; preds = %23, %37, %27, %1, %42, %35
%48 = phi i32 [ %46, %42 ], [ 0, %35 ], [ 1, %1 ], [ 0, %27 ], [ 1, %37 ], [ 0, %23 ]
ret i32 %48
}