// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/Matrix.cpp
// Function: Matrix::MaxInSectionWithPadding(unsigned long, unsigned long, unsigned long, unsigned long, double)
double Matrix::MaxInSectionWithPadding(size_t i_start, size_t i_end, size_t j_start, size_t j_end, double padding_value)
{
    // find max element in section [i_start, i_end), and [j_start, j_end)
    // in this code, we all follow the rule: close front and open end
    // and all counters start from 0
    //
    // if range exceeded matirx dimension, then use padding value 
    auto dim = Dimension();
    bool at_least_one_element_in_range = false;

    assert(dim.first > 0 && dim.second > 0);
    assert((int)i_start >= 0 && i_end > i_start);
    assert((int)j_start >= 0 && j_end > j_start);

    auto in_range = [&](size_t ii, size_t jj) -> bool
    {
        if((int)ii>=0 && ii<dim.first && (int)jj>=0 && jj<dim.second)
            return true;
        return false;
    };

    double res = 0.;
    if(in_range(i_start, j_start))
        res = __M[i_start][j_start];
    else
        res = padding_value;

    for(size_t i=i_start;i<i_end;i++) {
        for(size_t j=j_start;j<j_end;j++)
        {
            if(! in_range(i, j)) continue;
            at_least_one_element_in_range = true;

            if(res < __M[i][j])
                res = __M[i][j];
        }
    }
    if(!at_least_one_element_in_range)
    {
        std::cout<<__func__<<"() Error: all elements exceeded range."<<std::endl;
        exit(0);
    }

    return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, (%rsp)
movq %r8, %rbx
movq %rcx, %rbp
movq %rdx, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %r13
callq 0x17a6a
movq %rax, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
testq %rax, %rax
je 0x19ae6
testq %rdx, %rdx
je 0x19ae6
testl %r12d, %r12d
js 0x19b05
cmpq %r12, 0x10(%rsp)
jbe 0x19b05
testl %ebp, %ebp
js 0x19b24
cmpq %rbp, %rbx
jbe 0x19b24
leaq 0x28(%rsp), %rax
leaq 0x20(%rsp), %rdi
movq %rax, (%rdi)
movq %r12, %rsi
movq %rbp, %rdx
callq 0x19b74
movq (%r13), %rcx
movq %rcx, 0x8(%rsp)
testb %al, %al
movsd (%rsp), %xmm0
je 0x19a68
imulq $0x18, %r12, %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax), %rax
movsd (%rax,%rbp,8), %xmm0
xorl %r14d, %r14d
leaq 0x20(%rsp), %r13
movq %rbp, 0x18(%rsp)
cmpq 0x10(%rsp), %r12
jae 0x19ad1
imulq $0x18, %r12, %r15
addq 0x8(%rsp), %r15
cmpq %rbx, %rbp
jae 0x19ac7
movsd %xmm0, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x19b74
testb %al, %al
je 0x19abd
movq (%r15), %rax
movsd (%rax,%rbp,8), %xmm0
movb $0x1, %r14b
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
ja 0x19ac2
movapd %xmm1, %xmm0
jmp 0x19ac2
movsd (%rsp), %xmm0
incq %rbp
jmp 0x19a85
incq %r12
movq 0x18(%rsp), %rbp
jmp 0x19a75
testb $0x1, %r14b
je 0x19b43
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa9da(%rip), %rdi      # 0x244c7
leaq 0xa4a7(%rip), %rsi      # 0x23f9b
leaq 0xa9ec(%rip), %rcx      # 0x244e7
movl $0x459, %edx            # imm = 0x459
callq 0x4180
leaq 0xaa2a(%rip), %rdi      # 0x24536
leaq 0xa488(%rip), %rsi      # 0x23f9b
leaq 0xa9cd(%rip), %rcx      # 0x244e7
movl $0x45a, %edx            # imm = 0x45A
callq 0x4180
leaq 0xaa30(%rip), %rdi      # 0x2455b
leaq 0xa469(%rip), %rsi      # 0x23f9b
leaq 0xa9ae(%rip), %rcx      # 0x244e7
movl $0x45b, %edx            # imm = 0x45B
callq 0x4180
movq 0x1948e(%rip), %rdi     # 0x32fd8
leaq 0xaa2f(%rip), %rsi      # 0x24580
callq 0x4240
leaq 0xaa3b(%rip), %rsi      # 0x24598
movq %rax, %rdi
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260