tableswitch :: BranchMap -> Maybe Instr -> Instr
tableswitch = switches OP.tableswitch header
  where header ~(defaultLabel:_:labels) branchMap relOffsetToLabel = do
          debug $ liftIO $ print ("tableswitch", branchMap, low, high)
          writeI32 low
          writeI32 high
          go labels [low..high]
          where go ls@(l:ls') (x:xs)
                  | IntMap.member x branchMap = do
                    relOffsetToLabel l >>= writeI32
                    go ls' xs
                  | otherwise = do
                    relOffsetToLabel defaultLabel >>= writeI32
                    go ls xs
                go _ _ = return ()
                low  = fst . IntMap.findMin $ branchMap
                high = fst . IntMap.findMax $ branchMap
tableswitch :: BranchMap -> Maybe Instr -> Instr
tableswitch = switches OP.tableswitch header
  where header ~(defaultLabel:_:labels) branchMap relOffsetToLabel = do
          debug $ liftIO $ print ("tableswitch", branchMap, low, high)
          writeI32 low
          writeI32 high
          go labels [low..high]
          where go ls@(l:ls') (x:xs)
                  | IntMap.member x branchMap = do
                    relOffsetToLabel l >>= writeI32
                    go ls' xs
                  | otherwise = do
                    relOffsetToLabel defaultLabel >>= writeI32
                    go ls xs
                go _ _ = return ()
                low  = fst . IntMap.findMin $ branchMap
                high = fst . IntMap.findMax $ branchMap
tableswitch = switches OP.tableswitch header
  where header ~(defaultLabel:_:labels) branchMap relOffsetToLabel = do
          debug $ liftIO $ print ("tableswitch", branchMap, low, high)
          writeI32 low
          writeI32 high
          go labels [low..high]
          where go ls@(l:ls') (x:xs)
                  | IntMap.member x branchMap = do
                    relOffsetToLabel l >>= writeI32
                    go ls' xs
                  | otherwise = do
                    relOffsetToLabel defaultLabel >>= writeI32
                    go ls xs
                go _ _ = return ()
                low  = fst . IntMap.findMin $ branchMap
                high = fst . IntMap.findMax $ branchMap