// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x1
popq %rbx
cmpl $0x5, %eax
je 0x3012
cmpl $0x4, %eax
jne 0x2f58
xorl %ebx, %ebx
jmp 0x3012
movq %rdi, %r13
movl $0xc0010292, %r15d      # imm = 0xC0010292
leaq 0x4(%r15), %rdi
xorl %esi, %esi
callq 0x2986
movq %rax, %r14
movl $0xc0010292, %edi       # imm = 0xC0010292
xorl %esi, %esi
callq 0x2986
movq %rax, %r12
testq %r14, %r14
setne %al
testq %r12, %r12
setne %cl
testb %cl, %al
je 0x2ff7
movl (%r13), %eax
testl %eax, %eax
je 0x3090
cmpl $0x3, %eax
je 0x3023
cmpl $0x2, %eax
jne 0x2f51
leaq 0x3ffefd6e(%r15), %rax
orq %rax, %r12
pushq $-0x1
popq %rbp
movl $0xc0010292, %edi       # imm = 0xC0010292
movq %r12, %rsi
movl %ebp, %edx
callq 0x2764
movl %eax, %r12d
orq $0x404040, %r14         # imm = 0x404040
addq $0x4, %r15
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x2764
orl %r12d, %eax
jne 0x3012
movq 0x3fdd(%rip), %rdi      # 0x6fc8
leaq 0x1333(%rip), %rsi      # 0x4325
jmp 0x30e2
movq 0x3ff2(%rip), %rdi      # 0x6ff0
leaq 0x12fb(%rip), %rsi      # 0x4300
callq 0x2170
movq %rax, %rdi
callq 0x20d0
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3f9e(%rip), %rbx      # 0x6fc8
leaq 0x1317(%rip), %rsi      # 0x4348
movq %rbx, %rdi
callq 0x2170
addq $0x3ffefd6e, %r15       # imm = 0x3FFEFD6E
testq %r15, %r12
leaq 0x131c(%rip), %rax      # 0x4366
leaq 0x130d(%rip), %rsi      # 0x435e
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0x2170
movq %rax, %rdi
callq 0x20d0
leaq 0x1303(%rip), %rsi      # 0x436f
movq %rbx, %rdi
callq 0x2170
notl %r14d
movq 0x3f4a(%rip), %rdi      # 0x6fc8
testl $0x404040, %r14d        # imm = 0x404040
jne 0x30db
leaq 0x12d0(%rip), %rsi      # 0x435e
jmp 0x30e2
btrq $0x20, %r12
pushq $-0x1
popq %rbp
movl $0xc0010292, %edi       # imm = 0xC0010292
movq %r12, %rsi
movl %ebp, %edx
callq 0x2764
movl %eax, %r12d
andq $-0x404041, %r14        # imm = 0xFFBFBFBF
addq $0x4, %r15
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x2764
orl %r12d, %eax
jne 0x3012
movq 0x3ef6(%rip), %rdi      # 0x6fc8
leaq 0x125d(%rip), %rsi      # 0x4336
jmp 0x30e2
leaq 0x1284(%rip), %rsi      # 0x4366
callq 0x2170
movq %rax, %rdi
callq 0x20d0
jmp 0x2f51