#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 */

/* Variables and functions */
int /*<<< orphan*/ KDGETMODE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int main(void) {
  int mode = 0;
  int tty = open("/dev/console", O_RDONLY);
  if (tty < 0) {
    return 1;
  }
  int status = ioctl(tty, KDGETMODE, &mode);
  close(tty);
  if (status != 0) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1174 <main+0x44>
movl   $0x1,-0x4(%rbp)
jmp    11b1 <main+0x81>
mov    -0xc(%rbp),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x10(%rbp)
je     11aa <main+0x7a>
movl   $0x1,-0x4(%rbp)
jmp    11b1 <main+0x81>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xeb5(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     117d <main+0x4d>
mov    %eax,%ebx
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
test   %ebp,%ebp
setne  %al
jmp    1182 <main+0x52>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/console\00"
@O_RDONLY = global i32 0
@KDGETMODE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 1, i32* %1
br label %20
10: ; preds = %0
%11 = load i32, i32* %3
%12 = load i32, i32* @KDGETMODE
%13 = call i32 @ioctl(i32 %11, i32 %12, i32* %2)
store i32 %13, i32* %4
%14 = load i32, i32* %3
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 1, i32* %1
br label %20
19: ; preds = %10
store i32 0, i32* %1
br label %20
20: ; preds = %19, %18, %9
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/console\00"
@O_RDONLY = global i32 0
@KDGETMODE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %0
%7 = load i32, i32* @KDGETMODE
%8 = call i32 @ioctl(i32 %4, i32 %7, i32* %1)
%9 = call i32 @close(i32 %4)
%10 = icmp ne i32 %8, 0
%11 = zext i1 %10 to i32
br label %12
12: ; preds = %0, %6
%13 = phi i32 [ %11, %6 ], [ 1, %0 ]
ret i32 %13
}