// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]MQSim/src/ssd/TSU_Priority_OutOfOrder.cpp
// Function: SSD_Components::TSU_Priority_OutOfOrder::get_next_write_service_queue(NVM::FlashMemory::Flash_Chip*)
Flash_Transaction_Queue *TSU_Priority_OutOfOrder::get_next_write_service_queue(NVM::FlashMemory::Flash_Chip *chip)
{
    if (UserWriteTRQueue[chip->ChannelID][chip->ChipID][IO_Flow_Priority_Class::URGENT].size() > 0)
    {
        return &UserWriteTRQueue[chip->ChannelID][chip->ChipID][IO_Flow_Priority_Class::URGENT];
    }

    int numberOfTries = 0;
    while (numberOfTries < IO_Flow_Priority_Class::get_scheduling_weight(IO_Flow_Priority_Class::HIGH))
    {
        nextPriorityClassWrite[chip->ChannelID][chip->ChipID] = IO_Flow_Priority_Class::to_priority((nextPriorityClassWrite[chip->ChannelID][chip->ChipID] + 1) % IO_Flow_Priority_Class::NUMBER_OF_PRIORITY_LEVELS);
        if (nextPriorityClassWrite[chip->ChannelID][chip->ChipID] == IO_Flow_Priority_Class::URGENT)
        {
            nextPriorityClassWrite[chip->ChannelID][chip->ChipID] = IO_Flow_Priority_Class::HIGH;
        }
        if (nextPriorityClassWrite[chip->ChannelID][chip->ChipID] == IO_Flow_Priority_Class::HIGH)
        {
            numberOfTries++;
            currentWeightWrite[chip->ChannelID][chip->ChipID]--;
            if (currentWeightWrite[chip->ChannelID][chip->ChipID] <= 0)
            {
                currentWeightWrite[chip->ChannelID][chip->ChipID] = IO_Flow_Priority_Class::get_scheduling_weight(IO_Flow_Priority_Class::HIGH);
            }
        }
        if (IO_Flow_Priority_Class::get_scheduling_weight(nextPriorityClassWrite[chip->ChannelID][chip->ChipID]) >= currentWeightWrite[chip->ChannelID][chip->ChipID]
            && UserWriteTRQueue[chip->ChannelID][chip->ChipID][nextPriorityClassWrite[chip->ChannelID][chip->ChipID]].size() > 0)
        {
            return &UserWriteTRQueue[chip->ChannelID][chip->ChipID][nextPriorityClassWrite[chip->ChannelID][chip->ChipID]];
        }
    }

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq 0xb8(%rdi), %rax
movl 0x2c(%rsi), %ecx
movl 0x30(%rsi), %edx
movq (%rax,%rcx,8), %rax
movq (%rax,%rdx,8), %rax
cmpq $0x0, 0x18(%rax)
jne 0x51fc0
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x89d0(%rip), %r15      # 0x5a8bc
xorl %ebp, %ebp
movq 0xf0(%r14), %rax
movl 0x2c(%rbx), %ecx
movl 0x30(%rbx), %edx
movq (%rax,%rcx,8), %rax
movl (%rax,%rdx,4), %eax
leal 0x1(%rax), %ecx
leal 0x4(%rax), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
andl $-0x4, %edx
negl %edx
leal (%rax,%rdx), %edi
incl %edi
callq 0x52176
cmpl $0x1, %eax
movl %eax, %edi
adcl $0x0, %edi
movq 0xf0(%r14), %rcx
movl 0x2c(%rbx), %edx
movl 0x30(%rbx), %esi
movq (%rcx,%rdx,8), %r8
movl %edi, (%r8,%rsi,4)
cmpl $0x1, %eax
ja 0x51f65
incl %ebp
movq 0x100(%r14), %rdi
movq (%rdi,%rdx,8), %rax
decl (%rax,%rsi,4)
movl 0x2c(%rbx), %edx
movl 0x30(%rbx), %eax
movq (%rdi,%rdx,8), %rdx
cmpl $0x0, (%rdx,%rax,4)
jg 0x51f65
movl $0x4, (%rdx,%rax,4)
movl 0x2c(%rbx), %eax
movl 0x30(%rbx), %edx
movq (%rcx,%rax,8), %rcx
movl (%rcx,%rdx,4), %ecx
xorl %esi, %esi
cmpq $0x3, %rcx
ja 0x51f7e
movl (%r15,%rcx,4), %esi
movq 0x100(%r14), %rdi
movq (%rdi,%rax,8), %rdi
cmpl (%rdi,%rdx,4), %esi
jl 0x51fb0
movq 0xb8(%r14), %rsi
movq (%rsi,%rax,8), %rax
movq (%rax,%rdx,8), %rax
movq %rcx, %rdx
shlq $0x8, %rdx
leaq (%rdx,%rcx,8), %rcx
cmpq $0x0, 0x18(%rax,%rcx)
jne 0x51fbd
cmpl $0x4, %ebp
jl 0x51eee
xorl %eax, %eax
jmp 0x51fc0
addq %rcx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop