#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_RDONLY;
int /*<<< orphan*/ VT_ACTIVATE;
int /*<<< orphan*/ VT_WAITACTIVE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int switch_console(char new_console) {
  int newvt;
  int vt;
  if ((new_console < '1') || (new_console > '8')) {
    fprintf(stderr, "wrong console number\n");
    return -1;
  }
  newvt = new_console & 15;
  vt = open("/dev/tty1", O_RDONLY);
  if (vt == -1) {
    perror("open(/dev/tty1)");
    return -1;
  }
  if (ioctl(vt, VT_ACTIVATE, newvt)) {
    perror("ioctl(VT_ACTIVATE)");
    return -1;
  }
  if (ioctl(vt, VT_WAITACTIVE, newvt)) {
    perror("ioctl(VT_WAITACTIVE)");
    return -1;
  }
  close(vt);
  return 0;
}


x86 assembly (clang, -O0):
<switch_console>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x31,%eax
jl     1178 <switch_console+0x28>
movsbl -0x5(%rbp),%eax
cmp    $0x38,%eax
jle    1199 <switch_console+0x49>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <switch_console+0x108>
movsbl -0x5(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0xc(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11dd <switch_console+0x8d>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <switch_console+0x108>
mov    -0x10(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1213 <switch_console+0xc3>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <switch_console+0x108>
mov    -0x10(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1249 <switch_console+0xf9>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <switch_console+0x108>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_console>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x39(%rbx),%eax
cmp    $0xf7,%al
ja     1178 <switch_console+0x28>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11d1 <switch_console+0x81>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d8 <switch_console+0x88>
mov    %eax,%ebp
and    $0xf,%bl
movzbl %bl,%ebx
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11e1 <switch_console+0x91>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11ea <switch_console+0x9a>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe41(%rip),%rdi
jmp    11f1 <switch_console+0xa1>
lea    0xe48(%rip),%rdi
jmp    11f1 <switch_console+0xa1>
lea    0xe52(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11d1 <switch_console+0x81>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"wrong console number\0A\00"
@.str.1 = [10 x i8] c"/dev/tty1\00"
@O_RDONLY = global i32 0
@.str.2 = [16 x i8] c"open(/dev/tty1)\00"
@VT_ACTIVATE = global i32 0
@.str.3 = [19 x i8] c"ioctl(VT_ACTIVATE)\00"
@VT_WAITACTIVE = global i32 0
@.str.4 = [21 x i8] c"ioctl(VT_WAITACTIVE)\00"
define i32 @switch_console(i8 signext %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
store i8 %0, i8* %3
%6 = load i8, i8* %3
%7 = sext i8 %6 to i32
%8 = icmp slt i32 %7, 49
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8, i8* %3
%11 = sext i8 %10 to i32
%12 = icmp sgt i32 %11, 56
br i1 %12, label %13, label %16
13: ; preds = %9, %1
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
16: ; preds = %9
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
%19 = and i32 %18, 15
store i32 %19, i32* %4
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
26: ; preds = %16
%27 = load i32, i32* %5
%28 = load i32, i32* @VT_ACTIVATE
%29 = load i32, i32* %4
%30 = call i64 @ioctl(i32 %27, i32 %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
34: ; preds = %26
%35 = load i32, i32* %5
%36 = load i32, i32* @VT_WAITACTIVE
%37 = load i32, i32* %4
%38 = call i64 @ioctl(i32 %35, i32 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %45
42: ; preds = %34
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
store i32 0, i32* %2
br label %45
45: ; preds = %42, %40, %32, %24, %13
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"wrong console number\0A\00"
@.str.1 = [10 x i8] c"/dev/tty1\00"
@O_RDONLY = global i32 0
@.str.2 = [16 x i8] c"open(/dev/tty1)\00"
@VT_ACTIVATE = global i32 0
@.str.3 = [19 x i8] c"ioctl(VT_ACTIVATE)\00"
@VT_WAITACTIVE = global i32 0
@.str.4 = [21 x i8] c"ioctl(VT_WAITACTIVE)\00"
define i32 @switch_console(i8 signext %0) {
%2 = add i8 %0, -57
%3 = icmp ult i8 %2, -8
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %29
7: ; preds = %1
%8 = and i8 %0, 15
%9 = zext i8 %8 to i32
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %29
15: ; preds = %7
%16 = load i32, i32* @VT_ACTIVATE
%17 = tail call i64 @ioctl(i32 %11, i32 %16, i32 %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %29
21: ; preds = %15
%22 = load i32, i32* @VT_WAITACTIVE
%23 = tail call i64 @ioctl(i32 %11, i32 %22, i32 %9)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %29
27: ; preds = %21
%28 = tail call i32 @close(i32 %11)
br label %29
29: ; preds = %27, %25, %19, %13, %4
%30 = phi i32 [ -1, %4 ], [ -1, %13 ], [ -1, %19 ], [ -1, %25 ], [ 0, %27 ]
ret i32 %30
}