#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*/ O_RDWR;
int /*<<< orphan*/ VT_ACTIVATE;
int /*<<< orphan*/ VT_WAITACTIVE;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
bool chvt(int vtnum) {
  int fd;
  bool rv = false;
  fd = open("/dev/console", O_RDWR);
  if (fd < 0)
    goto out;
  if (ioctl(fd, VT_ACTIVATE, vtnum))
    goto out;
  if (ioctl(fd, VT_WAITACTIVE, vtnum))
    goto out;
  rv = true;
out:
  if (fd >= 0)
    close(fd);
  return rv;
}


x86 assembly (clang, -O0):
<chvt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
lea    0xeae(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1169 <chvt+0x39>
jmp    11b6 <chvt+0x86>
mov    -0x8(%rbp),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    -0x4(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     118c <chvt+0x5c>
jmp    11b6 <chvt+0x86>
mov    -0x8(%rbp),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    -0x4(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11af <chvt+0x7f>
jmp    11b6 <chvt+0x86>
movl   $0x1,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11c8 <chvt+0x98>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chvt>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0xeb9(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1191 <chvt+0x61>
mov    %eax,%ebx
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <ioctl@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1188 <chvt+0x58>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <ioctl@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1193 <chvt+0x63>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@VT_ACTIVATE = global i32 0
@VT_WAITACTIVE = global i32 0
define i32 @chvt(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%5 = load i32, i32* @O_RDWR
%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 = %1
br label %25
10: ; preds = %1
%11 = load i32, i32* %3
%12 = load i32, i32* @VT_ACTIVATE
%13 = load i32, i32* %2
%14 = call i64 @ioctl(i32 %11, i32 %12, i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10
br label %25
17: ; preds = %10
%18 = load i32, i32* %3
%19 = load i32, i32* @VT_WAITACTIVE
%20 = load i32, i32* %2
%21 = call i64 @ioctl(i32 %18, i32 %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
br label %25
24: ; preds = %17
store i32 1, i32* %4
br label %25
25: ; preds = %24, %23, %16, %9
%26 = load i32, i32* %3
%27 = icmp sge i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %3
%30 = call i32 @close(i32 %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@VT_ACTIVATE = global i32 0
@VT_WAITACTIVE = global i32 0
define i32 @chvt(i32 %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = load i32, i32* @VT_ACTIVATE
%7 = tail call i64 @ioctl(i32 %3, i32 %6, i32 %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = load i32, i32* @VT_WAITACTIVE
%11 = tail call i64 @ioctl(i32 %3, i32 %10, i32 %0)
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
br label %14
14: ; preds = %9, %5
%15 = phi i32 [ %13, %9 ], [ 0, %5 ]
%16 = tail call i32 @close(i32 %3)
br label %17
17: ; preds = %1, %14
%18 = phi i32 [ %15, %14 ], [ 0, %1 ]
ret i32 %18
}