Fixed loop boundary check
Added result_element_ptr_2 for nested loop