# Buggy code
def lower_parfor_sequential(func_ir, typemap, calltypes):
    parfor_found = False
    new_blocks = {}
    for block_label, block in func_ir.blocks.items():
        scope = block.scope
        i = _find_first_parfor(block.body)
        while i != -1:
            parfor_found = True
            inst = block.body[i]
            loc = inst.init_block.loc
            # split block across parfor
            prev_block = ir.Block(scope, loc)
            prev_block.body = block.body[:i]
            block.body = block.body[i + 1 :]
            # previous block jump to parfor init block
            init_label = next_label()
            prev_block.body.append(ir.Jump(init_label, loc))
            new_blocks[init_label] = inst.init_block
            new_blocks[block_label] = prev_block
            block_label = next_label()

            ndims = len(inst.loop_nests)
            for i in range(ndims):
                loopnest = inst.loop_nests[i]
                # create range block for loop
                range_label = next_label()
                header_label = next_label()
                range_block = mk_range_block(
                    typemap,
                    loopnest.start,
                    loopnest.stop,
                    loopnest.step,
                    calltypes,
                    scope,
                    loc,
                )
                range_block.body[-1].target = header_label  # fix jump target
                phi_var = range_block.body[-2].target
                new_blocks[range_label] = range_block
                header_block = mk_loop_header(typemap, phi_var, calltypes, scope, loc)
                header_block.body[-2].target = loopnest.index_variable
                new_blocks[header_label] = header_block
                # jump to this new inner loop
                if i == 0:
                    inst.init_block.body.append(ir.Jump(range_label, loc))
                    header_block.body[-1].falsebr = block_label
                else:
                    new_blocks[prev_header_label].body[-1].truebr = range_label
                    header_block.body[-1].falsebr = prev_header_label
                prev_header_label = header_label  # to set truebr next loop

            # last body block jump to inner most header
            body_last_label = max(inst.loop_body.keys())
            inst.loop_body[body_last_label].body.append(ir.Jump(header_label, loc))
            # inner most header jumps to first body block
            body_first_label = min(inst.loop_body.keys())
            header_block.body[-1].truebr = body_first_label
            # add parfor body to blocks
            for l, b in inst.loop_body.items():
                new_blocks[l] = b
            i = _find_first_parfor(block.body)

        # old block stays either way
        new_blocks[block_label] = block
    func_ir.blocks = new_blocks
    # rename only if parfor found and replaced (avoid test_flow_control error)
    if parfor_found:
        func_ir.blocks = rename_labels(func_ir.blocks)
    dprint_func_ir(func_ir, "after parfor sequential lowering")
    return


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-3b23f7331ceb> in <module>()
----> 1 @jit(arg_types=[numba.double], ret_type=numba.double)
2 def is_REALLY_five(some_value):
3     for i in range(5):
4         if some_value == 5.0:
5             return 1.0

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/decorators.pyc in _jit(func)
77                   "garbage collected!" % (func,))
78         t = Translate(func, *args, **kws)
---> 79         t.translate()
80         __tr_map__[func] = t
81         return t.get_ctypes_func(llvm)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/translate.pyc in translate(self)
926                     # Ensure we are playing with locals that might
927                     # actually precede the next block.
--> 928                     self.check_locals(i)
929
930                 self.crnt_block = i

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/translate.pyc in check_locals(self, i)
1135                 else:
1136                     assert next_locals is not None, "Internal compiler error!"
-> 1137             self._locals = next_locals[:]
1138
1139     def get_ctypes_func(self, llvm=True):

TypeError: 'NoneType' object has no attribute '__getitem__'

# Fixed code
def lower_parfor_sequential(func_ir, typemap, calltypes):
    ir_utils._max_label = ir_utils.find_max_label(func_ir.blocks) + 1

    parfor_found = False
    new_blocks = {}
    for block_label, block in func_ir.blocks.items():
        block_label, parfor_found = _lower_parfor_sequential_block(
            block_label, block, new_blocks, typemap, calltypes, parfor_found
        )
        # old block stays either way
        new_blocks[block_label] = block
    func_ir.blocks = new_blocks
    dprint_func_ir(func_ir, "before rename")
    # rename only if parfor found and replaced (avoid test_flow_control error)
    if parfor_found:
        func_ir.blocks = rename_labels(func_ir.blocks)
    dprint_func_ir(func_ir, "after parfor sequential lowering")
    local_array_analysis = array_analysis.ArrayAnalysis(func_ir, typemap, calltypes)
    simplify(
        func_ir,
        typemap,
        local_array_analysis,
        calltypes,
        array_analysis.copy_propagate_update_analysis,
    )
    dprint_func_ir(func_ir, "after parfor sequential simplify")

    return