// File: /workspace/github_cpp_makefile_star2_O2/joakimkistowski[P]amd-disable-c6/amd-disable-c6.cpp
// Function: write_msr(long, unsigned long, int)
unsigned int write_msr(long int msr_addr, uint64_t value, int cpu_id = -1) {
    if (cpu_id < 0) {
        unsigned int ret = 0;
        std::vector<int> ids;
        get_cpu_ids(ids);
        for (const int& id : ids) {
            if (id > 0) {
                ret = ret | write_msr(msr_addr, value, id);
            }
        }
        return ret;
    } else {
        std::string path = std::string("/dev/cpu/") + std::to_string(cpu_id) + std::string("/msr");
        FILE * f = fopen(path.c_str(), "w");
        if (!f) {
            std::cerr << "Failed writing of C6 state; msr module is not loaded in kernel" << std::endl;
            return 1;
        }
        int err = fseek (f, msr_addr, SEEK_SET);
        if (err) {
            std::cerr << "Failed writing of C6 state; error writing to msr" << std::endl;
            fclose(f);
            return 1;
        }
        fwrite(&value, 8, 1, f);
        fclose(f);
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xc0, %rsp
movq %rdi, %rbx
movq %rsi, 0x38(%rsp)
testl %edx, %edx
js 0x2876
movl %edx, %ebp
leaq 0x18c2(%rip), %rsi      # 0x404e
leaq 0x80(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x3224
leaq 0x60(%rsp), %rdi
movl %ebp, %esi
callq 0x32f7
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x3290
leaq 0x18b5(%rip), %rsi      # 0x4080
leaq 0x40(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x3224
leaq 0x10(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x3290
leaq 0x40(%rsp), %rdi
callq 0x22f8
leaq 0xa0(%rsp), %rdi
callq 0x22f8
leaq 0x60(%rsp), %rdi
callq 0x22f8
leaq 0x80(%rsp), %rdi
callq 0x22f8
movq 0x10(%rsp), %rdi
leaq 0x185a(%rip), %rsi      # 0x4085
callq 0x21d0
testq %rax, %rax
je 0x28c5
movq %rax, %r14
xorl %ebp, %ebp
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x21b0
testl %eax, %eax
je 0x28e5
movq 0x4796(%rip), %rdi      # 0x6ff0
leaq 0x1865(%rip), %rsi      # 0x40c6
callq 0x2170
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbp
jmp 0x28f8
movq %rsi, %r14
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
callq 0x255c
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
xorl %ebp, %ebp
cmpq %r12, %r15
je 0x28b9
movl (%r15), %edx
testl %edx, %edx
jle 0x28b3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2764
orl %eax, %ebp
addq $0x4, %r15
jmp 0x289a
leaq 0x10(%rsp), %rdi
callq 0x3736
jmp 0x290a
movq 0x4724(%rip), %rdi      # 0x6ff0
leaq 0x17b4(%rip), %rsi      # 0x4087
callq 0x2170
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbp
jmp 0x2900
leaq 0x38(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x22c0
movq %r14, %rdi
callq 0x2180
leaq 0x10(%rsp), %rdi
callq 0x22f8
movl %ebp, %eax
addq $0xc0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2971
jmp 0x2920
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x2965
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x22f8
jmp 0x293c
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x22f8
jmp 0x294e
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x22f8
jmp 0x295d
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x22f8
jmp 0x297e
movq %rax, %rbx
jmp 0x297e
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3736
movq %rbx, %rdi
callq 0x2290