///|
fn graph_structure(
  graph : Graph,
  forward : Dominance,
) -> (Array[NaturalLoop], Array[Component]) {
  let n = graph.nodes.length()
  let (succ, pred) = adjacency(graph)

  // Finish an iterative DFS before traversing the reversed graph.
  let seen = Array::make(n, false)
  let finished : Array[Int32] = []
  for start in 0..= 0; index = index - 1 {
    let start = finished[index]
    if assigned[start] {
      continue
    }
    let members = []
    let stack = [start]
    assigned[start] = true
    while !stack.is_empty() {
      let node = stack.pop().unwrap()
      members.push(node)
      for previous in pred[node] {
        if forward.reachable[previous] && !assigned[previous] {
          assigned[previous] = true
          stack.push(previous)
        }
      }
    }
    members.sort()
    groups.push(members)
  }
  groups.sort_by(fn(left, right) { left[0].compare(right[0]) })
  let group_index = Array::make(n, -1)
  for index, members in groups {
    for node in members {
      group_index[node] = index
    }
  }
  let components : Array[Component] = []
  for index, members in groups {
    let entry_nodes = []
    for node in members {
      if node == graph.entry ||
        pred[node].any(fn(previous) {
          forward.reachable[previous] && group_index[previous] != index
        }) {
        entry_nodes.push(node)
      }
    }
    components.push({
      members,
      entry_nodes,
      cyclic: members.length() > 1 || succ[members[0]].contains(members[0]),
      multiple_entries: entry_nodes.length() > 1,
    })
  }

  let loops = natural_loops(graph, pred, forward.parent_tree(), nest=true)
  (loops, components)
}

///|
fn collect_natural_loop(
  header : Int32,
  latches : Array[Int32],
  pred : Array[Array[Int32]],
  reachable : Array[Bool],
) -> (NaturalLoop, Array[Bool]) {
  let n = pred.length()
  latches.sort()
  let included = Array::make(n, false)
  let stack : Array[Int32] = []
  // Each latch is dominated by the header. A reachable predecessor of a
  // dominated node other than the header is also dominated: a path avoiding
  // the header could otherwise extend to that node. The header stops the walk.
  included[header] = true
  for latch in latches {
    if !included[latch] {
      included[latch] = true
      stack.push(latch)
    }
  }
  while !stack.is_empty() {
    let node = stack.pop().unwrap()
    for previous in pred[node] {
      if !included[previous] && reachable[previous] {
        included[previous] = true
        stack.push(previous)
      }
    }
  }
  let members = []
  for node in 0.. Array[NaturalLoop] {
  let n = graph.nodes.length()
  // Collect all back edges for a header before taking predecessor closure.
  let by_header : Array[Array[Int32]] = Array::makei(n, _ => [])
  for (latch, header) in graph.edges {
    if forward.dominates(header, latch) {
      by_header[header].push(latch)
    }
  }
  let loops : Array[NaturalLoop] = []
  let loop_membership : Array[Array[Bool]] = []
  for header, latches in by_header {
    if latches.is_empty() {
      continue
    }
    let (natural_loop, included) = collect_natural_loop(
      header,
      latches,
      pred,
      forward.reachable,
    )
    loops.push(natural_loop)
    if nest {
      loop_membership.push(included)
    }
  }
  if !nest {
    return loops
  }
  for index, natural_loop in loops {
    let mut parent : Int32? = None
    let mut parent_size = n + 1
    for candidate, outer in loops {
      if outer.members.length() <= natural_loop.members.length() ||
        outer.members.length() >= parent_size {
        continue
      }
      if natural_loop.members.all(fn(node) { loop_membership[candidate][node] }) {
        parent = Some(candidate)
        parent_size = outer.members.length()
      }
    }
    loops[index] = { ..natural_loop, parent, }
  }
  loops
}