// File: /workspace/github_cpp_makefile_star2_O3/joakimkistowski[P]amd-disable-c6/amd-disable-c6.cpp
// Function: direct_execution_main(op_mode const&)
int direct_execution_main(const op_mode &mode) {
    switch(mode) {
        case ERROR:
            return 1;
        case NONE:
            return 0;
        default:
            uint64_t core_msr_value = read_msr(CORE_MSR_ADDR);
            uint64_t package_msr_value = read_msr(PACKAGE_MSR_ADDR);
            if (!core_msr_value || !package_msr_value) {
                std::cerr << "Error reading msr; got invalid value" << std::endl;
                return 1;
            }
            if (mode == ENABLE) {
                    unsigned int err = write_msr(PACKAGE_MSR_ADDR, package_msr_value | PACKAGE_MSR_ENABLED);
                    err = err | write_msr(CORE_MSR_ADDR, core_msr_value | CORE_MSR_ENABLED);
                    if (err) {
                        return 1;
                    }
                    std::cout << "Enabled C6 state" << std::endl;
                    return 0;
            } else if (mode == DISABLE) {
                    unsigned int err = write_msr(PACKAGE_MSR_ADDR, package_msr_value & PACKAGE_MSR_DISABLED);
                    err = err | write_msr(CORE_MSR_ADDR, core_msr_value & CORE_MSR_DISABLED);
                    if (err) {
                        return 1;
                    }
                    std::cout << "Disabled C6 state" << std::endl;
                    return 0;
            } else if (mode == LIST) {
                    std::cout << "C6 State - Package : ";
                    if (package_msr_value & PACKAGE_MSR_ENABLED) {
                        std:: cout << "Enabled" << std::endl;
                    } else {
                        std:: cout << "Disabled" << std::endl;
                    }
                    std::cout << "C6 State - Core    : ";
                    if ((core_msr_value & CORE_MSR_ENABLED) == CORE_MSR_ENABLED) {
                        std:: cout << "Enabled" << std::endl;
                    } else {
                        std:: cout << "Disabled" << std::endl;
                    }
                    return 0;
            }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl (%rdi), %eax
movl $0x1, %ebx
cmpl $0x5, %eax
je 0x3bc7
cmpl $0x4, %eax
jne 0x3ae0
xorl %ebx, %ebx
jmp 0x3bc7
movq %rdi, %r13
movl $0xc0010292, %r15d      # imm = 0xC0010292
leaq 0x4(%r15), %rdi
xorl %esi, %esi
callq 0x2f51
movq %rax, %r14
movl $0xc0010292, %edi       # imm = 0xC0010292
xorl %esi, %esi
callq 0x2f51
movq %rax, %r12
testq %r14, %r14
setne %al
testq %r12, %r12
setne %cl
testb %cl, %al
je 0x3b85
movl (%r13), %eax
testl %eax, %eax
je 0x3c14
cmpl $0x3, %eax
je 0x3bd8
cmpl $0x2, %eax
jne 0x3ad9
leaq 0x3ffefd6e(%r15), %rax
orq %rax, %r12
movl $0xc0010292, %edi       # imm = 0xC0010292
movq %r12, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2a6b
movl %eax, %ebp
orq $0x404040, %r14         # imm = 0x404040
addq $0x4, %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2a6b
orl %ebp, %eax
jne 0x3bc7
movq 0x345c(%rip), %rbx      # 0x6fd0
leaq 0x17a4(%rip), %rsi      # 0x531f
movl $0x10, %edx
jmp 0x3c63
movq 0x3464(%rip), %r14      # 0x6ff0
leaq 0x1767(%rip), %rsi      # 0x52fa
movl $0x24, %edx
movq %r14, %rdi
callq 0x21e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x33f1(%rip), %rbx      # 0x6fd0
leaq 0x175c(%rip), %rsi      # 0x5342
movl $0x15, %edx
movq %rbx, %rdi
callq 0x21e0
addq $0x3ffefd6e, %r15       # imm = 0x3FFEFD6E
movq 0x33cf(%rip), %rdi      # 0x6fd0
testq %r15, %r12
je 0x3c78
leaq 0x174b(%rip), %rsi      # 0x5358
movl $0x7, %edx
jmp 0x3c84
btrq $0x20, %r12
movl $0xc0010292, %edi       # imm = 0xC0010292
movq %r12, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2a6b
movl %eax, %ebp
andq $-0x404041, %r14        # imm = 0xFFBFBFBF
addq $0x4, %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x2a6b
orl %ebp, %eax
jne 0x3bc7
movq 0x3379(%rip), %rbx      # 0x6fd0
leaq 0x16d2(%rip), %rsi      # 0x5330
movl $0x11, %edx
movq %rbx, %rdi
callq 0x21e0
movq %rbx, %rdi
callq 0x20d0
jmp 0x3ad9
leaq 0x16e1(%rip), %rsi      # 0x5360
movl $0x8, %edx
callq 0x21e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
leaq 0x16b2(%rip), %rsi      # 0x5369
movl $0x15, %edx
movq %rbx, %rdi
callq 0x21e0
notl %r14d
testl $0x404040, %r14d        # imm = 0x404040
jne 0x3cde
leaq 0x1681(%rip), %rsi      # 0x5358
movl $0x7, %edx
jmp 0x3cea
leaq 0x167b(%rip), %rsi      # 0x5360
movl $0x8, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
jmp 0x3ad9