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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int sprintf(char *, char *, char *, char *);
scalar_t__ strstr(char *, char *);
void check_cr3s(char *buffer, uint32_t *pos, uint32_t max) {
  FILE *fp = NULL;
  char line[0x1000];
  bool boot_args_set = false;
  fp = popen("nvram -p | grep \"boot-args\"", "r");
  while (fgets(line, sizeof(line), fp) != NULL) {
    if (strstr(line, "-no_shared_cr3")) {
      (*pos) +=
          sprintf(buffer + (*pos), "Non-Shared CR3 (boot-args):\t%sENABLED%s\n",
                  "\x1b[31m", "\x1b[0m");
    } else {
      (*pos) += sprintf(buffer + (*pos),
                        "Non-Shared CR3 (boot-args):\t%sDISABLED%s\n",
                        "\x1b[32m", "\x1b[0m");
    }
    boot_args_set = true;
  }
  if (!boot_args_set) {
    (*pos) +=
        sprintf(buffer + (*pos), "Non-Shared CR3 (boot-args):\t%sDISABLED%s\n",
                "\x1b[32m", "\x1b[0m");
  }
  pclose(fp);
}


x86 assembly (clang, -O0):
<check_cr3s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x1024(%rbp)
lea    0xe81(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1243 <check_cr3s+0xf3>
lea    -0x1020(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     1202 <check_cr3s+0xb2>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
lea    0xe4d(%rip),%rsi
lea    0xe6f(%rip),%rdx
lea    0xe6e(%rip),%rcx
call   1060 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
jmp    1234 <check_cr3s+0xe4>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
lea    0xe4a(%rip),%rsi
lea    0xe6d(%rip),%rdx
lea    0xe37(%rip),%rcx
call   1060 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x1,-0x1024(%rbp)
jmp    118f <check_cr3s+0x3f>
cmpl   $0x0,-0x1024(%rbp)
jne    1282 <check_cr3s+0x132>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
lea    0xdfc(%rip),%rsi
lea    0xe1f(%rip),%rdx
lea    0xde9(%rip),%rcx
call   1060 <sprintf@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <pclose@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_cr3s>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe92(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1203 <check_cr3s+0xb3>
lea    0xe85(%rip),%r12
mov    %rsp,%r13
lea    0xeb9(%rip),%rbp
jmp    11dd <check_cr3s+0x8d>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xeaa(%rip),%rsi
lea    0xecd(%rip),%rdx
mov    %rbp,%rcx
call   1060 <sprintf@plt>
add    %eax,(%rbx)
mov    %r13,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1228 <check_cr3s+0xd8>
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <strstr@plt>
movslq (%rbx),%rdi
add    %r15,%rdi
test   %rax,%rax
je     11b0 <check_cr3s+0x60>
lea    0xe33(%rip),%rsi
lea    0xe55(%rip),%rdx
jmp    11be <check_cr3s+0x6e>
movslq (%rbx),%rax
add    %rax,%r15
lea    0xe51(%rip),%rsi
lea    0xe74(%rip),%rdx
lea    0xe3e(%rip),%rcx
mov    %r15,%rdi
call   1060 <sprintf@plt>
add    %eax,(%rbx)
mov    %r14,%rdi
call   1030 <pclose@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"nvram -p | grep \22boot-args\22\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"-no_shared_cr3\00"
@.str.3 = [41 x i8] c"Non-Shared CR3 (boot-args):\09%sENABLED%s\0A\00"
@.str.4 = [6 x i8] c"\1B[31m\00"
@.str.5 = [5 x i8] c"\1B[0m\00"
@.str.6 = [42 x i8] c"Non-Shared CR3 (boot-args):\09%sDISABLED%s\0A\00"
@.str.7 = [6 x i8] c"\1B[32m\00"
define void @check_cr3s(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32* null, i32** %7
store i32 0, i32* %9
%10 = call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %7
br label %11
11: ; preds = %40, %3
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%13 = load i32*, i32** %7
%14 = call i32* @fgets(i8* %12, i32 4096, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %41
16: ; preds = %11
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%18 = call i64 @strstr(i8* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = load i32*, i32** %5
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%27 = load i32*, i32** %5
%28 = load i32, i32* %27
%29 = add nsw i32 %28, %26
store i32 %29, i32* %27
br label %40
30: ; preds = %16
%31 = load i8*, i8** %4
%32 = load i32*, i32** %5
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%37 = load i32*, i32** %5
%38 = load i32, i32* %37
%39 = add nsw i32 %38, %36
store i32 %39, i32* %37
br label %40
40: ; preds = %30, %20
store i32 1, i32* %9
br label %11
41: ; preds = %11
%42 = load i32, i32* %9
%43 = icmp ne i32 %42, 0
br i1 %43, label %54, label %44
44: ; preds = %41
%45 = load i8*, i8** %4
%46 = load i32*, i32** %5
%47 = load i32, i32* %46
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32*, i32** %5
%52 = load i32, i32* %51
%53 = add nsw i32 %52, %50
store i32 %53, i32* %51
br label %54
54: ; preds = %44, %41
%55 = load i32*, i32** %7
%56 = call i32 @pclose(i32* %55)
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"nvram -p | grep \22boot-args\22\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"-no_shared_cr3\00"
@.str.3 = [41 x i8] c"Non-Shared CR3 (boot-args):\09%sENABLED%s\0A\00"
@.str.4 = [6 x i8] c"\1B[31m\00"
@.str.5 = [5 x i8] c"\1B[0m\00"
@.str.6 = [42 x i8] c"Non-Shared CR3 (boot-args):\09%sDISABLED%s\0A\00"
@.str.7 = [6 x i8] c"\1B[32m\00"
define void @check_cr3s(i8* nocapture writeonly %0, i32* nocapture %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32* @fgets(i8* %5, i32 4096, i32* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %25, label %9
9: ; preds = %3, %19
%10 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
%12 = load i32, i32* %1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
br i1 %11, label %17, label %15
15: ; preds = %9
%16 = call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %19
17: ; preds = %9
%18 = call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %18, %17 ], [ %16, %15 ]
%21 = load i32, i32* %1
%22 = add nsw i32 %21, %20
store i32 %22, i32* %1
%23 = call i32* @fgets(i8* %5, i32 4096, i32* %6)
%24 = icmp eq i32* %23, null
br i1 %24, label %32, label %9
25: ; preds = %3
%26 = load i32, i32* %1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = call i32 @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* %1
%31 = add nsw i32 %30, %29
store i32 %31, i32* %1
br label %32
32: ; preds = %19, %25
%33 = call i32 @pclose(i32* %6)
ret void
}