#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ debug(char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ klogctl(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void redirect_klog(void) {
  debug("Disabling printk() to console...");
  klogctl(6, NULL, 0);
  FILE *klog_fh = fopen("/dev/tty2", "a");
  if (!klog_fh) {
    debug("Error opening "
          "/dev/tty2"
          ": %s",
          strerror(errno));
    return;
  }
  FILE *kmsg_fh = fopen("/proc/kmsg", "r");
  if (!kmsg_fh) {
    debug("Error opening /proc/kmsg: %s", strerror(errno));
    return;
  }
  if (fork()) {
    return;
  }
  char buf[256];
  while (fgets(buf, 256, kmsg_fh)) {
    fputs(buf, klog_fh);
  }
  if (ferror(kmsg_fh)) {
    debug("Error reading /proc/kmsg: %s", strerror(errno));
  }
  debug("The /proc/kmsg read loop has exited!");
  exit(0);
}


x86 assembly (clang, -O0):
<redirect_klog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
mov    $0x6,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <klogctl@plt>
lea    0xe61(%rip),%rdi
lea    0xe64(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11fe <redirect_klog+0x6e>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
jmp    12d8 <redirect_klog+0x148>
lea    0xe44(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1243 <redirect_klog+0xb3>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
jmp    12d8 <redirect_klog+0x148>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
je     1259 <redirect_klog+0xc9>
jmp    12d8 <redirect_klog+0x148>
jmp    125e <redirect_klog+0xce>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1292 <redirect_klog+0x102>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <fputs@plt>
jmp    125e <redirect_klog+0xce>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12c3 <redirect_klog+0x133>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <debug@plt>
xor    %edi,%edi
call   1090 <exit@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_klog>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1040 <debug@plt>
mov    $0x6,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <klogctl@plt>
lea    0xe62(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1207 <redirect_klog+0x77>
mov    %rax,%rbx
lea    0xe6f(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1210 <redirect_klog+0x80>
mov    %rax,%r14
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     123d <redirect_klog+0xad>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe1f(%rip),%rbx
jmp    1217 <redirect_klog+0x87>
lea    0xe3f(%rip),%rbx
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <debug@plt>
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1275 <redirect_klog+0xe5>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1255 <redirect_klog+0xc5>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     12a0 <redirect_klog+0x110>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xddc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <debug@plt>
lea    0xde9(%rip),%rdi
xor    %eax,%eax
call   1040 <debug@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Disabling printk() to console...\00"
@.str.1 = [10 x i8] c"/dev/tty2\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [28 x i8] c"Error opening /dev/tty2: %s\00"
@errno = global i32 0
@.str.4 = [11 x i8] c"/proc/kmsg\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [29 x i8] c"Error opening /proc/kmsg: %s\00"
@.str.7 = [29 x i8] c"Error reading /proc/kmsg: %s\00"
@.str.8 = [37 x i8] c"The /proc/kmsg read loop has exited!\00"
define void @redirect_klog() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @klogctl(i32 6, i32* null, i32 0)
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %6, i32** %1
%7 = load i32*, i32** %1
%8 = icmp ne i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %11)
br label %46
13: ; preds = %0
%14 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %14, i32** %2
%15 = load i32*, i32** %2
%16 = icmp ne i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %19)
br label %46
21: ; preds = %13
%22 = call i64 (...) @fork()
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %46
25: ; preds = %21
br label %26
26: ; preds = %31, %25
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%28 = load i32*, i32** %2
%29 = call i64 @fgets(i8* %27, i32 256, i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%33 = load i32*, i32** %1
%34 = call i32 @fputs(i8* %32, i32* %33)
br label %26
35: ; preds = %26
%36 = load i32*, i32** %2
%37 = call i64 @ferror(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %39, %35
%44 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %24, %17, %9
ret void
}

LLVM IR (-O2):
@.str = [33 x i8] c"Disabling printk() to console...\00"
@.str.1 = [10 x i8] c"/dev/tty2\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [28 x i8] c"Error opening /dev/tty2: %s\00"
@errno = global i32 0
@.str.4 = [11 x i8] c"/proc/kmsg\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [29 x i8] c"Error opening /proc/kmsg: %s\00"
@.str.7 = [29 x i8] c"Error reading /proc/kmsg: %s\00"
@.str.8 = [37 x i8] c"The /proc/kmsg read loop has exited!\00"
define void @redirect_klog() {
%1 = alloca [256 x i8]
%2 = tail call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @klogctl(i32 6, i32* null, i32 0)
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %30, label %6
6: ; preds = %0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %30, label %9
9: ; preds = %6
%10 = tail call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %9
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%14 = call i64 @fgets(i8* %13, i32 256, i32* %7)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12, %16
%17 = call i32 @fputs(i8* %13, i32* %4)
%18 = call i64 @fgets(i8* %13, i32 256, i32* %7)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %16
20: ; preds = %16, %12
%21 = call i64 @ferror(i32* %7)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %20
%28 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %6, %0
%31 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), %0 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), %6 ]
%32 = load i32, i32* @errno
%33 = tail call i32 @strerror(i32 %32)
%34 = tail call i32 (i8*, ...) @debug(i8* %31, i32 %33)
br label %35
35: ; preds = %30, %9
ret void
}