#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 h;

/* Variables and functions */
int locore_readtbr();
int mvtlolevelirqinstall(int irqnr, void (*handler)()) {
  unsigned long h = (unsigned long)handler;
  unsigned long *addr =
      (unsigned long *)((locore_readtbr() & ~0xFFF) + 0x100 + (16 * irqnr));
  if (irqnr == 0 || irqnr >= 15) {
    return 0;
  }
  addr[0] = ((h >> 10) & 0x3fffff) | 0x29000000;
  addr[1] = ((h)&0x3ff) | 0x81c52000;
  addr[2] = 0x01000000;
  addr[3] = 0x01000000;
  return 1;
}


x86 assembly (clang, -O0):
<mvtlolevelirqinstall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <locore_readtbr@plt>
and    $0xfffff000,%eax
add    $0x100,%eax
mov    -0x8(%rbp),%ecx
shl    $0x4,%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     115a <mvtlolevelirqinstall+0x4a>
cmpl   $0xf,-0x8(%rbp)
jl     1166 <mvtlolevelirqinstall+0x56>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <mvtlolevelirqinstall+0xb2>
mov    -0x18(%rbp),%rcx
shr    $0xa,%rcx
and    $0x3fffff,%rcx
or     $0x29000000,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
and    $0x3ff,%rcx
movabs $0x81c52000,%rax
or     %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
movq   $0x1000000,0x10(%rax)
01
mov    -0x20(%rbp),%rax
movq   $0x1000000,0x18(%rax)
01
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mvtlolevelirqinstall>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <locore_readtbr@plt>
test   %ebx,%ebx
je     116f <mvtlolevelirqinstall+0x5f>
cmp    $0xe,%ebx
jg     116f <mvtlolevelirqinstall+0x5f>
and    $0xfffff000,%eax
shl    $0x4,%ebx
add    %ebx,%eax
add    $0x100,%eax
cltq
mov    %r14d,%ecx
shr    $0xa,%ecx
or     $0x29000000,%rcx
mov    %rcx,(%rax)
and    $0x3ff,%r14d
mov    $0x81c52000,%ecx
or     %r14,%rcx
mov    %rcx,0x8(%rax)
movaps 0xe9a(%rip),%xmm0
movaps %xmm0,0x10(%rax)
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mvtlolevelirqinstall(i32 %0, void (...)* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca void (...)*
%6 = alloca i64
%7 = alloca i64*
store i32 %0, i32* %4
store void (...)* %1, void (...)** %5
%8 = load void (...)*, void (...)** %5
%9 = ptrtoint void (...)* %8 to i64
store i64 %9, i64* %6
%10 = call i32 (...) @locore_readtbr()
%11 = and i32 %10, -4096
%12 = add nsw i32 %11, 256
%13 = load i32, i32* %4
%14 = mul nsw i32 16, %13
%15 = add nsw i32 %12, %14
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i64*
store i64* %17, i64** %7
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %2
%21 = load i32, i32* %4
%22 = icmp sge i32 %21, 15
br i1 %22, label %23, label %24
23: ; preds = %20, %2
store i32 0, i32* %3
br label %40
24: ; preds = %20
%25 = load i64, i64* %6
%26 = lshr i64 %25, 10
%27 = and i64 %26, 4194303
%28 = or i64 %27, 687865856
%29 = load i64*, i64** %7
%30 = getelementptr inbounds i64, i64* %29, i64 0
store i64 %28, i64* %30
%31 = load i64, i64* %6
%32 = and i64 %31, 1023
%33 = or i64 %32, 2177179648
%34 = load i64*, i64** %7
%35 = getelementptr inbounds i64, i64* %34, i64 1
store i64 %33, i64* %35
%36 = load i64*, i64** %7
%37 = getelementptr inbounds i64, i64* %36, i64 2
store i64 16777216, i64* %37
%38 = load i64*, i64** %7
%39 = getelementptr inbounds i64, i64* %38, i64 3
store i64 16777216, i64* %39
store i32 1, i32* %3
br label %40
40: ; preds = %24, %23
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
define i32 @mvtlolevelirqinstall(i32 %0, void (...)* %1) {
%3 = ptrtoint void (...)* %1 to i64
%4 = tail call i32 (...) @locore_readtbr()
%5 = and i32 %4, -4096
%6 = shl nsw i32 %0, 4
%7 = add i32 %6, 256
%8 = add i32 %7, %5
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i64*
%11 = icmp eq i32 %0, 0
%12 = icmp sgt i32 %0, 14
%13 = or i1 %11, %12
br i1 %13, label %23, label %14
14: ; preds = %2
%15 = lshr i64 %3, 10
%16 = and i64 %15, 4194303
%17 = or i64 %16, 687865856
store i64 %17, i64* %10
%18 = and i64 %3, 1023
%19 = or i64 %18, 2177179648
%20 = getelementptr inbounds i64, i64* %10, i64 1
store i64 %19, i64* %20
%21 = getelementptr inbounds i64, i64* %10, i64 2
%22 = bitcast i64* %21 to <2 x i64>*
store <2 x i64> <i64 16777216, i64 16777216>, <2 x i64>* %22
br label %23
23: ; preds = %2, %14
%24 = phi i32 [ 1, %14 ], [ 0, %2 ]
ret i32 %24
}