#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 dummy;
};

/* Variables and functions */
int O_NOCTTY;
int O_WRONLY;
int /*<<< orphan*/ VT_GETSTATE;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct vt_stat *);
int open(char const *, int, int /*<<< orphan*/);
bool try_vtstate(const char *dev, struct vt_stat *vtstat) {
  bool res = false;
  int fd = open(dev, O_WRONLY | O_NOCTTY, 0);
  if (fd < 0)
    goto cleanup;
  if (ioctl(fd, VT_GETSTATE, vtstat) < 0)
    goto cleanup;
  res = true;
cleanup:
  if (fd >= 0)
    close(fd);
  return res;
}


x86 assembly (clang, -O0):
<try_vtstate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e85(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1176 <try_vtstate+0x46>
jmp    11a1 <try_vtstate+0x71>
mov    -0x18(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    119a <try_vtstate+0x6a>
jmp    11a1 <try_vtstate+0x71>
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     11b3 <try_vtstate+0x83>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_vtstate>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e8a(%rip),%rax
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     117c <try_vtstate+0x4c>
mov    %eax,%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
mov    %rax,%rbx
shr    $0x3f,%rbx
xor    $0x1,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.vt_stat = type { i32 }
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
@VT_GETSTATE = global i32 0
define i32 @try_vtstate(i8* %0, %struct.vt_stat* %1) {
%3 = alloca i8*
%4 = alloca %struct.vt_stat*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store %struct.vt_stat* %1, %struct.vt_stat** %4
store i32 0, i32* %5
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
br label %23
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* @VT_GETSTATE
%18 = load %struct.vt_stat*, %struct.vt_stat** %4
%19 = call i64 @ioctl(i32 %16, i32 %17, %struct.vt_stat* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %23
22: ; preds = %15
store i32 1, i32* %5
br label %23
23: ; preds = %22, %21, %14
%24 = load i32, i32* %6
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %26, %23
%30 = load i32, i32* %5
ret i32 %30
}

LLVM IR (-O2):
%struct.vt_stat = type { i32 }
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
@VT_GETSTATE = global i32 0
define i32 @try_vtstate(i8* nocapture %0, %struct.vt_stat* %1) {
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* %0, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = load i32, i32* @VT_GETSTATE
%10 = tail call i64 @ioctl(i32 %6, i32 %9, %struct.vt_stat* %1)
%11 = lshr i64 %10, 63
%12 = trunc i64 %11 to i32
%13 = xor i32 %12, 1
%14 = tail call i32 @close(i32 %6)
br label %15
15: ; preds = %2, %8
%16 = phi i32 [ %13, %8 ], [ 0, %2 ]
ret i32 %16
}