extern void check_mode(void) {
  unsigned long val;
  asm volatile("mov %%cr0, %0" : "=r"(val));
  monitor_write("\n Check CR0 / EFLAGS: ");
  monitor_write("\n ------------------- ");
  monitor_write("\n Protected Mode: ");
  monitor_write_hex(val & 1);
  monitor_write("\n Paging: ");
  monitor_write_hex(val >> 31 & (1));
  monitor_write("\n Cache diasble: ");
  monitor_write_hex(val >> 30 & (1));
  monitor_write("\n Not-write through: ");
  monitor_write_hex(val >> 29 & (1));
  monitor_write("\n Write protection: ");
  monitor_write_hex(val >> 16 & (1));
  asm volatile("pushf\n\t pop %0" : "=g"(val));
  monitor_write("\n Interrupt enabled: ");
  monitor_write_hex(val >> 9 & (1));
}


x86 assembly (clang, -O0):
<check_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %cr0,%rax
mov    %rax,-0x8(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
lea    0xed3(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
lea    0xedc(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
mov    -0x8(%rbp),%rdi
and    $0x1,%rdi
mov    $0x0,%al
call   1030 <monitor_write_hex@plt>
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
mov    -0x8(%rbp),%rdi
shr    $0x1f,%rdi
and    $0x1,%rdi
mov    $0x0,%al
call   1030 <monitor_write_hex@plt>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
mov    -0x8(%rbp),%rdi
shr    $0x1e,%rdi
and    $0x1,%rdi
mov    $0x0,%al
call   1030 <monitor_write_hex@plt>
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
mov    -0x8(%rbp),%rdi
shr    $0x1d,%rdi
and    $0x1,%rdi
mov    $0x0,%al
call   1030 <monitor_write_hex@plt>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
mov    -0x8(%rbp),%rdi
shr    $0x10,%rdi
and    $0x1,%rdi
mov    $0x0,%al
call   1030 <monitor_write_hex@plt>
pushf
pop    -0x8(%rbp)
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <monitor_write@plt>
mov    -0x8(%rbp),%rdi
shr    $0x9,%rdi
and    $0x1,%rdi
mov    $0x0,%al
call   1030 <monitor_write_hex@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_mode>:
push   %rbx
sub    $0x10,%rsp
mov    %cr0,%rbx
mov    %rbx,0x8(%rsp)
lea    0xecc(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
lea    0xed5(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
lea    0xede(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
mov    %ebx,%edi
and    $0x1,%edi
xor    %eax,%eax
call   1030 <monitor_write_hex@plt>
lea    0xed7(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
mov    %ebx,%edi
shr    $0x1f,%edi
xor    %eax,%eax
call   1030 <monitor_write_hex@plt>
lea    0xec8(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
mov    %ebx,%edi
shr    $0x1e,%edi
and    $0x1,%edi
xor    %eax,%eax
call   1030 <monitor_write_hex@plt>
lea    0xebd(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
mov    %ebx,%edi
shr    $0x1d,%edi
and    $0x1,%edi
xor    %eax,%eax
call   1030 <monitor_write_hex@plt>
lea    0xeb6(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
shr    $0x10,%ebx
and    $0x1,%ebx
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <monitor_write_hex@plt>
pushf
pop    0x8(%rsp)
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1040 <monitor_write@plt>
mov    0x8(%rsp),%edi
shr    $0x9,%edi
and    $0x1,%edi
xor    %eax,%eax
call   1030 <monitor_write_hex@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"\0A Check CR0 / EFLAGS: \00"
@.str.1 = [23 x i8] c"\0A ------------------- \00"
@.str.2 = [19 x i8] c"\0A Protected Mode: \00"
@.str.3 = [11 x i8] c"\0A Paging: \00"
@.str.4 = [18 x i8] c"\0A Cache diasble: \00"
@.str.5 = [22 x i8] c"\0A Not-write through: \00"
@.str.6 = [21 x i8] c"\0A Write protection: \00"
@.str.7 = [22 x i8] c"\0A Interrupt enabled: \00"
define void @check_mode() {
%1 = alloca i64
%2 = call i64 asm sideeffect "mov %cr0, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i64 %2, i64* %1
%3 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%6 = load i64, i64* %1
%7 = and i64 %6, 1
%8 = call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %7)
%9 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%10 = load i64, i64* %1
%11 = lshr i64 %10, 31
%12 = and i64 %11, 1
%13 = call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %12)
%14 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%15 = load i64, i64* %1
%16 = lshr i64 %15, 30
%17 = and i64 %16, 1
%18 = call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %17)
%19 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%20 = load i64, i64* %1
%21 = lshr i64 %20, 29
%22 = and i64 %21, 1
%23 = call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %22)
%24 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%25 = load i64, i64* %1
%26 = lshr i64 %25, 16
%27 = and i64 %26, 1
%28 = call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %27)
%29 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%30 = load i64, i64* %1
%31 = lshr i64 %30, 9
%32 = and i64 %31, 1
%33 = call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %32)
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"\0A Check CR0 / EFLAGS: \00"
@.str.1 = [23 x i8] c"\0A ------------------- \00"
@.str.2 = [19 x i8] c"\0A Protected Mode: \00"
@.str.3 = [11 x i8] c"\0A Paging: \00"
@.str.4 = [18 x i8] c"\0A Cache diasble: \00"
@.str.5 = [22 x i8] c"\0A Not-write through: \00"
@.str.6 = [21 x i8] c"\0A Write protection: \00"
@.str.7 = [22 x i8] c"\0A Interrupt enabled: \00"
define void @check_mode() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i64 asm sideeffect "mov %cr0, $0", "=r,~{dirflag},~{fpsr},~{flags}"()
store i64 %3, i64* %1
%4 = tail call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%7 = and i64 %3, 1
%8 = tail call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %7)
%9 = tail call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%10 = lshr i64 %3, 31
%11 = and i64 %10, 1
%12 = tail call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %11)
%13 = tail call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%14 = lshr i64 %3, 30
%15 = and i64 %14, 1
%16 = tail call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %15)
%17 = tail call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%18 = lshr i64 %3, 29
%19 = and i64 %18, 1
%20 = tail call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %19)
%21 = tail call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%22 = lshr i64 %3, 16
%23 = and i64 %22, 1
%24 = tail call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %23)
%25 = call i32 (i8*, ...) bitcast (i32 (...)* @monitor_write to i32 (i8*, ...)*)(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%26 = load i64, i64* %1
%27 = lshr i64 %26, 9
%28 = and i64 %27, 1
%29 = call i32 (i64, ...) bitcast (i32 (...)* @monitor_write_hex to i32 (i64, ...)*)(i64 %28)
ret void
}