#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*/ TIOCLINUX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, char **);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void switch_printk_console(int new_console) {
  char newvt[2];
  int vt;
  if ((new_console < 0) || (new_console > 24)) {
    fprintf(stderr, "wrong console number\n");
    exit(1);
  }
  newvt[0] = 11;
  newvt[1] = new_console;
  vt = open("/dev/tty1", O_RDONLY);
  if (vt == -1) {
    perror("open(/dev/tty1)");
    exit(1);
  }
  if (ioctl(vt, TIOCLINUX, &newvt)) {
    exit(1);
  }
  close(vt);
}


x86 assembly (clang, -O0):
<switch_printk_console>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     117f <switch_printk_console+0x1f>
cmpl   $0x18,-0x4(%rbp)
jle    119e <switch_printk_console+0x3e>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movb   $0xb,-0x6(%rbp)
mov    -0x4(%rbp),%eax
mov    %al,-0x5(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11e0 <switch_printk_console+0x80>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    -0x6(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1209 <switch_printk_console+0xa9>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_printk_console>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x19,%edi
jae    11b7 <switch_printk_console+0x57>
movb   $0xb,0xe(%rsp)
mov    %dil,0xf(%rsp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe92(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d6 <switch_printk_console+0x76>
mov    %eax,%ebx
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11e2 <switch_printk_console+0x82>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe43(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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"
@TIOCLINUX = global i32 0
define void @switch_printk_console(i32 %0) {
%2 = alloca i32
%3 = alloca [2 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* %2
%9 = icmp sgt i32 %8, 24
br i1 %9, label %10, label %14
10: ; preds = %7, %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %7
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 11, i8* %15
%16 = load i32, i32* %2
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 %17, i8* %18
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %14
%27 = load i32, i32* %4
%28 = load i32, i32* @TIOCLINUX
%29 = bitcast [2 x i8]* %3 to i8**
%30 = call i64 @ioctl(i32 %27, i32 %28, i8** %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
ret void
}

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"
@TIOCLINUX = global i32 0
define void @switch_printk_console(i32 %0) {
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%4 = icmp ugt i32 %0, 24
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
store i8 11, i8* %3
%10 = trunc i32 %0 to i8
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 %10, i8* %11
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = load i32, i32* @TIOCLINUX
%20 = bitcast [2 x i8]* %2 to i8**
%21 = call i64 @ioctl(i32 %13, i32 %19, i8** %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = call i32 @close(i32 %13)
ret void
}