(seq)
return seq
Remove all redundant jumps.

        :param SequenceNode seq:    The SequenceNode instance to handle.
        :return:                    A processed SequenceNode.
Remove all redundant jumps.
Remove all redundant jumps .
def _remove_redundant_jumps(seq):
        """
        Remove all redundant jumps.

        :param SequenceNode seq:    The SequenceNode instance to handle.
        :return:                    A processed SequenceNode.
        """

        def _handle_Sequence(node: SequenceNode, **kwargs):
            if len(node.nodes) > 1:
                for i in range(len(node.nodes) - 1):
                    this_node = node.nodes[i]
                    goto_stmt: Optional[ailment.Stmt.Jump] = None
                    if isinstance(this_node, ailment.Block) and \
                            this_node.statements and \
                            isinstance(this_node.statements[-1], ailment.Stmt.Jump):
                        goto_stmt: ailment.Stmt.Jump = this_node.statements[-1]
                    elif isinstance(this_node, MultiNode) and \
                            this_node.nodes and \
                            isinstance(this_node.nodes[-1], ailment.Block) and \
                            this_node.nodes[-1].statements and \
                            isinstance(this_node.nodes[-1].statements[-1], ailment.Stmt.Jump):
                        this_node = this_node.nodes[-1]
                        goto_stmt: ailment.Stmt.Jump = this_node.statements[-1]

                    if goto_stmt is not None:
                        next_node = node.nodes[i + 1]
                        if isinstance(goto_stmt.target, ailment.Expr.Const) and \
                                goto_stmt.target.value == next_node.addr:
                            # this goto is useless
                            this_node.statements = this_node.statements[:-1]

            return walker._handle_Sequence(node, **kwargs)

        def _handle_MultiNode(node: MultiNode, **kwargs):
            if len(node.nodes) > 1:
                for i in range(len(node.nodes) - 1):
                    this_node = node.nodes[i]
                    goto_stmt: Optional[ailment.Stmt.Jump] = None
                    if isinstance(this_node, ailment.Block) and \
                            this_node.statements and \
                            isinstance(this_node.statements[-1], ailment.Stmt.Jump):
                        goto_stmt: ailment.Stmt.Jump = this_node.statements[-1]
                    elif isinstance(this_node, MultiNode) and \
                            this_node.nodes and \
                            isinstance(this_node.nodes[-1], ailment.Block) and \
                            this_node.nodes[-1].statements and \
                            isinstance(this_node.nodes[-1].statements[-1], ailment.Stmt.Jump):
                        goto_stmt: ailment.Stmt.Jump = this_node.nodes[-1].statements[-1]
                        this_node = this_node.nodes[-1]

                    if goto_stmt is not None:
                        next_node = node.nodes[i + 1]
                        if isinstance(goto_stmt.target, ailment.Expr.Const) and \
                                goto_stmt.target.value == next_node.addr:
                            # this goto is useless
                            this_node.statements = this_node.statements[:-1]

            return walker._handle_MultiNode(node, **kwargs)

        handlers = {
            SequenceNode: _handle_Sequence,
            MultiNode: _handle_MultiNode,
        }

        walker = SequenceWalker(handlers=handlers)
        walker.walk(seq)

        return seq
def _remove_redundant_jumps ( seq ) : def _handle_Sequence ( node : SequenceNode , * * kwargs ) : if len ( node . nodes ) > 1 : for i in range ( len ( node . nodes ) - 1 ) : this_node = node . nodes [ i ] goto_stmt : Optional [ ailment . Stmt . Jump ] = None if isinstance ( this_node , ailment . Block ) and this_node . statements and isinstance ( this_node . statements [ - 1 ] , ailment . Stmt . Jump ) : goto_stmt : ailment . Stmt . Jump = this_node . statements [ - 1 ] elif isinstance ( this_node , MultiNode ) and this_node . nodes and isinstance ( this_node . nodes [ - 1 ] , ailment . Block ) and this_node . nodes [ - 1 ] . statements and isinstance ( this_node . nodes [ - 1 ] . statements [ - 1 ] , ailment . Stmt . Jump ) : this_node = this_node . nodes [ - 1 ] goto_stmt : ailment . Stmt . Jump = this_node . statements [ - 1 ] if goto_stmt is not None : next_node = node . nodes [ i + 1 ] if isinstance ( goto_stmt . target , ailment . Expr . Const ) and goto_stmt . target . value == next_node . addr : # this goto is useless this_node . statements = this_node . statements [ : - 1 ] return walker . _handle_Sequence ( node , * * kwargs ) def _handle_MultiNode ( node : MultiNode , * * kwargs ) : if len ( node . nodes ) > 1 : for i in range ( len ( node . nodes ) - 1 ) : this_node = node . nodes [ i ] goto_stmt : Optional [ ailment . Stmt . Jump ] = None if isinstance ( this_node , ailment . Block ) and this_node . statements and isinstance ( this_node . statements [ - 1 ] , ailment . Stmt . Jump ) : goto_stmt : ailment . Stmt . Jump = this_node . statements [ - 1 ] elif isinstance ( this_node , MultiNode ) and this_node . nodes and isinstance ( this_node . nodes [ - 1 ] , ailment . Block ) and this_node . nodes [ - 1 ] . statements and isinstance ( this_node . nodes [ - 1 ] . statements [ - 1 ] , ailment . Stmt . Jump ) : goto_stmt : ailment . Stmt . Jump = this_node . nodes [ - 1 ] . statements [ - 1 ] this_node = this_node . nodes [ - 1 ] if goto_stmt is not None : next_node = node . nodes [ i + 1 ] if isinstance ( goto_stmt . target , ailment . Expr . Const ) and goto_stmt . target . value == next_node . addr : # this goto is useless this_node . statements = this_node . statements [ : - 1 ] return walker . _handle_MultiNode ( node , * * kwargs ) handlers = { SequenceNode : _handle_Sequence , MultiNode : _handle_MultiNode , } walker = SequenceWalker ( handlers = handlers ) walker . walk ( seq ) return seq