#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint32_t;
typedef int /*<<< orphan*/ code;

/* Variables and functions */
int /*<<< orphan*/ flush_dcache(uintptr_t, int);
int /*<<< orphan*/ flush_icache(uintptr_t, int);
uint32_t dynamic_mfc0(unsigned reg, unsigned sel) {
  uint32_t code[3] = {
      0x40020000,
      0x03e00008,
      0x00000000,
  };
  uint32_t (*fn)(void) = (void *)code;
  code[0] |= reg << 11;
  code[0] |= sel << 0;
  flush_dcache((uintptr_t)code, sizeof(code));
  flush_icache((uintptr_t)code, sizeof(code));
  return fn();
}


x86 assembly (clang, -O0):
<dynamic_mfc0>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0xecb(%rip),%rax
mov    %rax,-0x14(%rbp)
mov    0xec9(%rip),%eax
mov    %eax,-0xc(%rbp)
lea    -0x14(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
shl    $0xb,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x0,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x14(%rbp),%rdi
mov    $0xc,%esi
call   1030 <flush_dcache@plt>
lea    -0x14(%rbp),%rdi
mov    $0xc,%esi
call   1040 <flush_icache@plt>
call   *-0x20(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dynamic_mfc0>:
push   %rbx
sub    $0x10,%rsp
mov    0xed4(%rip),%rax
mov    %rax,(%rsp)
mov    0xed2(%rip),%eax
mov    %eax,0x8(%rsp)
shl    $0xb,%edi
or     %esi,%edi
or     $0x40020000,%edi
mov    %edi,(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1030 <flush_dcache@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <flush_icache@plt>
call   *%rbx
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.dynamic_mfc0.code = [3 x i32] [i32 1073872896, i32 65011720, i32 0]
define i32 @dynamic_mfc0(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [3 x i32]
%6 = alloca i32 ()*
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = bitcast [3 x i32]* %5 to i8*
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%9 = bitcast i32* %8 to i8*
%10 = bitcast i8* %9 to i32 ()*
store i32 ()* %10, i32 ()** %6
%11 = load i32, i32* %3
%12 = shl i32 %11, 11
%13 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%14 = load i32, i32* %13
%15 = or i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* %4
%17 = shl i32 %16, 0
%18 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%19 = load i32, i32* %18
%20 = or i32 %19, %17
store i32 %20, i32* %18
%21 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%22 = ptrtoint i32* %21 to i64
%23 = call i32 @flush_dcache(i64 %22, i32 12)
%24 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%25 = ptrtoint i32* %24 to i64
%26 = call i32 @flush_icache(i64 %25, i32 12)
%27 = load i32 ()*, i32 ()** %6
%28 = call i32 %27()
ret i32 %28
}

LLVM IR (-O2):
@__const.dynamic_mfc0.code = [3 x i32] [i32 1073872896, i32 65011720, i32 0]
define i32 @dynamic_mfc0(i32 %0, i32 %1) {
%3 = alloca [3 x i32]
%4 = bitcast [3 x i32]* %3 to i8*
%5 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%6 = bitcast [3 x i32]* %3 to i32 ()*
%7 = shl i32 %0, 11
%8 = or i32 %7, %1
%9 = or i32 %8, 1073872896
store i32 %9, i32* %5
%10 = ptrtoint [3 x i32]* %3 to i64
%11 = call i32 @flush_dcache(i64 %10, i32 12)
%12 = call i32 @flush_icache(i64 %10, i32 12)
%13 = call i32 %6()
ret i32 %13
}