// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq %rsi, 0xa0(%rsp)
testl %edx, %edx
js 0x2b13
movl %edx, %ebp
leaq 0x90(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x25a4(%rip), %rsi      # 0x5048
leaq 0x25a6(%rip), %rdx      # 0x5051
leaq 0x80(%rsp), %rdi
callq 0x3f94
movl $0x1, %r14d
cmpl $0xa, %ebp
jb 0x2b7f
movl $0x4, %r14d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x2b76
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x2b7c
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x2b7f
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r14d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x2ad4
addl $-0x3, %r14d
jmp 0x2b7f
movq %rsi, %r14
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x267c
movq (%rsp), %r15
movq 0x8(%rsp), %r12
xorl %ebp, %ebp
cmpq %r12, %r15
je 0x2b5d
movl (%r15), %edx
testl %edx, %edx
jle 0x2b50
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a6b
orl %eax, %ebp
addq $0x4, %r15
cmpq %r12, %r15
jne 0x2b3c
movq (%rsp), %r15
testq %r15, %r15
je 0x2e83
movq 0x10(%rsp), %rsi
subq %r15, %rsi
movq %r15, %rdi
jmp 0x2e7e
addl $-0x2, %r14d
jmp 0x2b7f
decl %r14d
movl %r14d, %esi
leaq 0x50(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x40(%rsp), %rdi
movl $0x2d, %edx
callq 0x21f0
movq 0x40(%rsp), %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x3f26
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r8
movq 0x48(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
cmpq %r15, %rcx
je 0x2bd5
movq 0x90(%rsp), %rdi
movq 0x40(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x2bf3
movl $0xf, %edi
cmpq %r12, %rsi
je 0x2bee
movq 0x50(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x2c02
leaq 0x80(%rsp), %rdi
callq 0x2090
jmp 0x2c10
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21d0
leaq 0x30(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x2c34
movq %rsi, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x2c3a
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2413(%rip), %rsi      # 0x507a
leaq 0x2410(%rip), %rdx      # 0x507e
leaq 0x60(%rsp), %rdi
callq 0x3f94
leaq 0x30(%rsp), %r13
movq -0x10(%r13), %rcx
movq -0x8(%r13), %r8
movq 0x68(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
cmpq %r13, %rcx
je 0x2c9d
movq 0x30(%rsp), %rdi
movq 0x60(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x2cbb
movl $0xf, %edi
cmpq %r14, %rsi
je 0x2cb6
movq 0x70(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x2cc7
leaq 0x20(%rsp), %rdi
callq 0x2090
jmp 0x2cd5
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21d0
leaq 0x10(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x2cf8
movq %rsi, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x2cfe
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d32
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x2d49
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x40(%rsp), %rdi
cmpq %r12, %rdi
je 0x2d60
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x80(%rsp), %rdi
cmpq %r15, %rdi
je 0x2d7d
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x21a0
movq (%rsp), %rdi
leaq 0x22f7(%rip), %rsi      # 0x507f
callq 0x2200
testq %rax, %rax
je 0x2df9
movq %rax, %r14
xorl %ebp, %ebp
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x21c0
testl %eax, %eax
je 0x2e46
movq 0x423d(%rip), %rbx      # 0x6ff0
leaq 0x2306(%rip), %rsi      # 0x50c0
movl $0x30, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x420b(%rip), %rdi      # 0x6ff0
callq 0x2030
movl $0x1, %ebp
movq %rax, %rdi
callq 0x2110
jmp 0x2e60
movq 0x41f0(%rip), %rbx      # 0x6ff0
leaq 0x227a(%rip), %rsi      # 0x5081
movl $0x3e, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x41be(%rip), %rdi      # 0x6ff0
callq 0x2030
movl $0x1, %ebp
movq %rax, %rdi
callq 0x2110
jmp 0x2e68
leaq 0xa0(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2300
movq %r14, %rdi
callq 0x2180
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e83
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21a0
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2f30
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x2ebb
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2ebb
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2edb
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2edb
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r12, %rdi
je 0x2ef7
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2ef7
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r15, %rdi
je 0x2f49
movq 0x90(%rsp), %rsi
jmp 0x2f2b
movq %rax, %rbx
jmp 0x2f49
jmp 0x2f15
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2f49
movq 0x10(%rsp), %rsi
incq %rsi
jmp 0x2f44
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2f49
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22c0