#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);
void dynamic_mtc0(unsigned reg, unsigned sel, uint32_t val) {
  uint32_t code[3] = {
      0x40840000,
      0x03e00008,
      0x00000000,
  };
  void (*fn)(uint32_t val) = (void *)code;
  code[0] |= reg << 11;
  code[0] |= sel << 0;
  flush_dcache((uintptr_t)code, sizeof(code));
  flush_icache((uintptr_t)code, sizeof(code));
  fn(val);
}


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

x86 assembly (clang, -O2):
<dynamic_mtc0>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    0xed1(%rip),%rax
mov    %rax,0x8(%rsp)
mov    0xece(%rip),%eax
mov    %eax,0x10(%rsp)
shl    $0xb,%edi
or     %esi,%edi
or     $0x40840000,%edi
mov    %edi,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1030 <flush_dcache@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1040 <flush_icache@plt>
mov    %ebp,%edi
call   *%rbx
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

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