// File: joakimkistowski[P]amd-disable-c6/amd-disable-c6.cpp
// Function: read_msr(long, unsigned int)
uint64_t read_msr(long int msr_addr, unsigned int cpu_id = 0) {
    std::string path = std::string("/dev/cpu/") + std::to_string(cpu_id) + std::string("/msr");
    FILE * f = fopen(path.c_str(), "r");
    if (!f) {
        std::cerr << "Failed reading of C6 state; msr module is not loaded in kernel" << std::endl;
        return 0;
    }
    int err = fseek (f, msr_addr, SEEK_SET);
    if (err) {
        std::cerr << "Failed reading of C6 state; error reading msr" << std::endl;
        fclose(f);
        return 0;
    }
    uint64_t msr_value = 0;
    fread(&msr_value, 8, 1, f);
    fclose(f);
    return msr_value;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x23a0
movq -0xf8(%rbp), %rdx
leaq 0x2efe(%rip), %rsi      # 0x604e
leaq -0x78(%rbp), %rdi
callq 0x3ed0
jmp 0x315b
movl -0x14(%rbp), %esi
leaq -0xb0(%rbp), %rdi
callq 0x4380
jmp 0x316c
leaq -0x58(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x4190
jmp 0x3182
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x23a0
movq -0x100(%rbp), %rdx
leaq 0x2edd(%rip), %rsi      # 0x6080
leaq -0xd0(%rbp), %rdi
callq 0x3ed0
jmp 0x31b1
leaq -0x38(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x4190
jmp 0x31c7
leaq -0xd0(%rbp), %rdi
callq 0x2428
leaq -0xd1(%rbp), %rdi
callq 0x2210
leaq -0x58(%rbp), %rdi
callq 0x2428
leaq -0xb0(%rbp), %rdi
callq 0x2428
leaq -0x78(%rbp), %rdi
callq 0x2428
leaq -0x79(%rbp), %rdi
callq 0x2210
leaq -0x38(%rbp), %rdi
callq 0x20c0
movq %rax, %rdi
leaq 0x2f49(%rip), %rsi      # 0x6162
callq 0x2260
movq %rax, -0x108(%rbp)
jmp 0x3227
movq -0x108(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xe0(%rbp)
jne 0x3354
movq 0x5da6(%rip), %rdi      # 0x8ff0
leaq 0x2ea6(%rip), %rsi      # 0x60f7
callq 0x21b0
movq %rax, -0x110(%rbp)
jmp 0x325f
movq -0x110(%rbp), %rdi
movq 0x5d43(%rip), %rsi      # 0x8fb0
callq 0x21f0
jmp 0x3274
movq $0x0, -0x8(%rbp)
movl $0x1, -0xe4(%rbp)
jmp 0x3423
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x3326
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x331d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x3311
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x32fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x2428
leaq -0xd1(%rbp), %rdi
callq 0x2210
leaq -0x58(%rbp), %rdi
callq 0x2428
leaq -0xb0(%rbp), %rdi
callq 0x2428
leaq -0x78(%rbp), %rdi
callq 0x2428
leaq -0x79(%rbp), %rdi
callq 0x2210
jmp 0x3439
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2428
jmp 0x3439
movq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x2240
movl %eax, -0x114(%rbp)
jmp 0x336e
movl -0x114(%rbp), %eax
movl %eax, -0xe8(%rbp)
cmpl $0x0, -0xe8(%rbp)
je 0x33d6
movq 0x5c66(%rip), %rdi      # 0x8ff0
leaq 0x2da5(%rip), %rsi      # 0x6136
callq 0x21b0
movq %rax, -0x120(%rbp)
jmp 0x339f
movq -0x120(%rbp), %rdi
movq 0x5c03(%rip), %rsi      # 0x8fb0
callq 0x21f0
jmp 0x33b4
movq -0xe0(%rbp), %rdi
callq 0x21c0
jmp 0x33c2
movq $0x0, -0x8(%rbp)
movl $0x1, -0xe4(%rbp)
jmp 0x3423
movq $0x0, -0xf0(%rbp)
movq -0xe0(%rbp), %rcx
leaq -0xf0(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x22f0
jmp 0x3400
movq -0xe0(%rbp), %rdi
callq 0x21c0
jmp 0x340e
movq -0xf0(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0xe4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2428
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0x2390
nopw %cs:(%rax,%rax)