// File: /workspace/github_c_makefile_star2_O0/hobo0xcc[P]rve/src/cpu.c
// Function: PlicTick
void PlicTick(State *state, bool virtio_int, bool uart_int) {
    uint32_t virtio_irq = VIRTIO_IRQ;
    uint32_t uart_irq = UART_IRQ;
    uint32_t virtio_priority = state->plic->priority[virtio_irq];
    uint32_t uart_priority = state->plic->priority[uart_irq];
    bool virtio_enable = state->plic->enable_bits >> virtio_irq & 1;
    bool uart_enable = state->plic->enable_bits >> uart_irq & 1;

    bool ints[2] = {virtio_int, uart_int};
    uint32_t irqs[2] = {virtio_irq, uart_irq};
    uint32_t priorities[2] = {virtio_priority, uart_priority};
    bool enables[2] = {virtio_enable, uart_enable};

    int priority = 0;
    int irq = 0;
    for (int i = 0; i < 2; i++) {
        // if (ints[i])
        //     printf("irqs[i]: %x, ints[i]: %d, enables[i]: %d, priorities[i]: %d, priority: %d, threshold: %d\n", irqs[i], ints[i], enables[i], priorities[i], priority, state->plic->threshold);
        if (ints[i] && enables[i] && priorities[i] > priority && priorities[i] > state->plic->threshold) {
            irq = irqs[i];
            priority = priorities[i];
        }
    }

    if (irq != 0) {
        // printf("Interrupted: irq: %d, virtio_int: %d\n", irq, virtio_int);
        state->plic->irq = irq;
        if (state->mode == MACHINE) {
            WriteCSR(state, MIP, 9, 9, 1);
        } else if (state->mode == SUPERVISOR) {
            WriteCSR(state, SIP, 9, 9, 1);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %dl, %al
movb %sil, %cl
movq %rdi, -0x8(%rbp)
andb $0x1, %cl
movb %cl, -0x9(%rbp)
andb $0x1, %al
movb %al, -0xa(%rbp)
movl $0x1, -0x10(%rbp)
movl $0xa, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq 0x8128(%rax), %rax
movl -0x10(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x8128(%rax), %rax
movl -0x14(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq 0x8128(%rax), %rax
movq 0x1008(%rax), %rax
movl -0x10(%rbp), %ecx
shrq %cl, %rax
andq $0x1, %rax
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movb %al, -0x1d(%rbp)
movq -0x8(%rbp), %rax
movq 0x8128(%rax), %rax
movq 0x1008(%rax), %rax
movl -0x14(%rbp), %ecx
shrq %cl, %rax
andq $0x1, %rax
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movb %al, -0x1e(%rbp)
movb -0x9(%rbp), %al
andb $0x1, %al
movb %al, -0x20(%rbp)
movb -0xa(%rbp), %al
andb $0x1, %al
movb %al, -0x1f(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movb -0x1d(%rbp), %al
andb $0x1, %al
movb %al, -0x32(%rbp)
movb -0x1e(%rbp), %al
andb $0x1, %al
movb %al, -0x31(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
cmpl $0x2, -0x40(%rbp)
jge 0x1e26
movslq -0x40(%rbp), %rax
testb $0x1, -0x20(%rbp,%rax)
je 0x1e19
movslq -0x40(%rbp), %rax
testb $0x1, -0x32(%rbp,%rax)
je 0x1e19
movslq -0x40(%rbp), %rax
movl -0x30(%rbp,%rax,4), %eax
cmpl -0x38(%rbp), %eax
jbe 0x1e19
movslq -0x40(%rbp), %rax
movl -0x30(%rbp,%rax,4), %eax
movq -0x8(%rbp), %rcx
movq 0x8128(%rcx), %rcx
cmpl 0x1010(%rcx), %eax
jbe 0x1e19
movslq -0x40(%rbp), %rax
movl -0x28(%rbp,%rax,4), %eax
movl %eax, -0x3c(%rbp)
movslq -0x40(%rbp), %rax
movl -0x30(%rbp,%rax,4), %eax
movl %eax, -0x38(%rbp)
jmp 0x1e1b
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1dbf
cmpl $0x0, -0x3c(%rbp)
je 0x1e9c
movl -0x3c(%rbp), %ecx
movq -0x8(%rbp), %rax
movq 0x8128(%rax), %rax
movl %ecx, 0x1000(%rax)
movq -0x8(%rbp), %rax
movzbl 0x8148(%rax), %eax
cmpl $0x3, %eax
jne 0x1e6d
movq -0x8(%rbp), %rdi
movl $0x344, %esi            # imm = 0x344
movl $0x9, %ecx
movl $0x1, %r8d
movl %ecx, %edx
callq 0x1690
jmp 0x1e9a
movq -0x8(%rbp), %rax
movzbl 0x8148(%rax), %eax
cmpl $0x1, %eax
jne 0x1e98
movq -0x8(%rbp), %rdi
movl $0x144, %esi            # imm = 0x144
movl $0x9, %ecx
movl $0x1, %r8d
movl %ecx, %edx
callq 0x1690
jmp 0x1e9a
jmp 0x1e9c
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)