def fetch_execute(instructions):
    """
    Read a list of machine instructions until we read an instruction for a second time (infinite loop)
    or jump to a instruction index outside the bounds of the input list, at which point we return
    the accumulator value (modified by 'acc' operations) and the index `i` which caused program halt
    """
    # Initialize variables for instruction line number and accumulator value
    i, acc = 0, 0
    # Initialize variable to record instructions we have already read in order to catch infinite loops
    visited = set()

    # Iterate through the instructions, updating the accumulator and following jump operations,
    # until we read the same instruction twice (infinite loop), or jump to instruction 
    # index out of bounds. Program terminated successfully if i = len(instructions).
    while 0 <= i < len(instructions):
        # Check for infinite loop
        if i in visited:
            # Infinite loop detected! Instruction `i` was read twice.
            break
        else:
            visited.add(i)

        op, arg = instructions[i]

        if op == "jmp":
            # Jump to next instruction according to relative position (not absolute index/address)
            i += arg
        else:
            # Update accumulator when op == "acc" and ignore when op == "nop"
            acc += arg if op == "acc" else 0
            # Move to next instruction immediately following
            i += 1
    # Infinite loop detected if 0 <= i < len(instructions)
    # Program terminated successfully if i == len(instructions)
    # Otherwise, we jumped to some other instruction outside the range (undefined behaviour)
    return i, acc