code: def uops_to_cstyle(
      lang: CStyleLanguage, function_name: str, uops: List[UOp]
  ) -> str:
      local_size: List[int] = []
      kernel, prekernel, bufs = [], [], []
      # pend_close = None
      depth = 1
  
      def kk(s):
          kernel.append("  " * depth + s)
  
      c: DefaultDict[str, int] = defaultdict(int)
      r: Dict[UOp, str] = {}
  
      def ssa(u, prefix="t"):
          nonlocal c, r
          ret = f"{prefix}{c[prefix]}"
          if u is not None:
              r[u] = ret
          c[prefix] += 1
          return ret
  
      child_count = Counter(v for ru in uops for v in ru.vin)
  
      for u in uops:
          uop, dtype, vin, args = u.uop, u.dtype, u.vin, u.arg
          # these four uops don't have output dtypes
          if uop == UOps.IF:
              kk(lang.render_if(r[vin[0]]))
              depth += 1
          elif uop == UOps.BARRIER:
              kk(lang.barrier)
          elif uop == UOps.END:
              depth -= 1
              kk("}")
          elif uop == UOps.STORE:
              assert (
                  vin[0].dtype is not None and vin[2].dtype is not None
              )
              if len(vin) > 3:
                  kk(lang.render_if(r[vin[3]]))
              kk(
                  lang.render_store(
                      r[vin[0]],
                      vin[0].dtype,
                      r[vin[2]],
                      vin[2].dtype,
                      strip_parens(r[vin[1]]),
                      vin[0].uop == UOps.DEFINE_LOCAL,
                  )
              )
              if len(vin) > 3:
                  kk("}")
          else:
              assert dtype is not None, f"None dtype for uop {uop}"
              if uop == UOps.LOOP:
                  kk(
                      lang.render_for(
                          ssa(u, "ridx"), r[vin[0]], r[vin[1]]
                      )
                  )
                  depth += 1
              elif uop == UOps.WMMA:
                  kk(
                      f"{lang.generic_var_prefix if lang.generic_var_prefix else dtype.name} {ssa(u, 'wmma')} = {args}({r[vin[0]]}, {r[vin[1]]}, {r[vin[2]]});"
                  )  # noqa: E501
              elif uop == UOps.ALU:
                  # remove parens if ALU types are the same. TODO: can do more here
                  if (
                      vin[0].uop == UOps.ALU
                      and vin[0].arg == args
                      and args
                      in {
                          BinaryOps.ADD,
                          BinaryOps.SUB,
                          BinaryOps.MUL,
                          BinaryOps.XOR,
                      }
                  ):
                      val = lang.code_for_op[args](
                          strip_parens(r[vin[0]]),
                          *[r[x] for x in vin[1:]],
                          dtype,
                      )
                  else:
                      val = lang.code_for_op[args](
                          *[r[x] for x in vin] + [dtype]
                      )
                  assert (
                      child_count[u] != 0
                  ), f"childless ALU op found {u}"
                  # TODO: fix index rendering issue. fix clang nested max macro issue
                  if (
                      child_count[u] <= 1
                      and args != BinaryOps.MAX
                      and not getenv("EXPAND_SSA")
                  ):
                      r[u] = val
                  else:
                      kk(
                          f"{lang.generic_var_prefix if lang.generic_var_prefix else dtype.name} {ssa(u,'alu')} = {val};"
                      )
              elif uop == UOps.DEFINE_ACC:
                  kk(
                      f"{lang.generic_var_prefix if lang.generic_var_prefix else dtype.name} {ssa(u,'acc')} = {lang.render_const(args, dtype)};"
                  )
              elif uop == UOps.SPECIAL:
                  kk(
                      f"{lang.size_prefix} {args[1]} = {lang.code_for_workitem[args[1][0]](args[0])}; /* {args[2]} */"
                  )
                  if args[1].startswith("l"):
                      local_size.append(args[2])
                  r[u] = args[1]
              elif uop == UOps.CONST:
                  r[u] = (
                      lang.render_const(args, dtype)
                      if args >= 0
                      else f"({lang.render_const(args, dtype)})"
                  )
              elif uop == UOps.LOAD:
                  val = lang.render_load(
                      dtype,
                      r[vin[0]],
                      vin[0].dtype,
                      strip_parens(r[vin[1]]),
                      vin[0].uop == UOps.DEFINE_LOCAL,
                  )
                  # NOTE: this relies on the load not happening if it's in the unselected branch
                  if len(vin) > 3:
                      val = lang.code_for_op[TernaryOps.WHERE](
                          r[vin[2]], val, r[vin[3]], dtype
                      )
                  kk(
                      f"{lang.generic_var_prefix if lang.generic_var_prefix else lang.render_dtype(dtype)} {ssa(u,'val')} = {val};"
                  )
              elif uop == UOps.PHI:
                  kk(f"{r[vin[0]]} = {r[vin[1]]};")
                  r[u] = r[vin[0]]
              elif uop == UOps.CAST:
                  if isinstance(args, tuple) and args[1]:  # bitcast
                      assert len(vin) == 1
                      precast = ssa(None, "precast")
                      kk(
                          f"{lang.generic_var_prefix if lang.generic_var_prefix else lang.render_dtype(cast(DType, vin[0].dtype))} {precast} = {r[vin[0]]};"
                      )
                      val = lang.render_cast(
                          [precast], dtype, bitcast=True
                      )
                  else:
                      val = lang.render_cast(
                          [r[x] for x in vin], dtype, bitcast=False
                      )
                  if child_count[u] <= 1:
                      r[u] = val
                  else:
                      kk(
                          f"{lang.generic_var_prefix if lang.generic_var_prefix else dtype.name} {ssa(u,'cast')} = {val};"
                      )
              elif uop == UOps.DEFINE_LOCAL:
                  if lang.external_local_bufs:
                      prekernel.append(
                          lang.render_local(args[0], dtype, args[1])
                      )
                  else:
                      kk(lang.render_local(args[0], dtype, args[1]))
                  r[u] = args[0]
              elif uop == UOps.DEFINE_GLOBAL:
                  bufs.append((args, dtype))
                  r[u] = args
              elif uop == UOps.GEP:
                  if cast(DType, vin[0].dtype).sz > 4:
                      r[
                          u
                      ] = f"({r[vin[0]]})[{args}]"  # this is correct for HIP
                  else:
                      r[u] = f"({r[vin[0]]}).{'xyzw'[args]}"
              else:
                  raise RuntimeError(f"failed to render {uop}")
  
      return lang.render_kernel(
          function_name, kernel, bufs, local_size, prekernel
      )
  
imports: from typing import Dict, List, Optional, NamedTuple, Tuple, Union, DefaultDict, cast, Literal, Callable
  import math, functools
  from collections import defaultdict, Counter
  from tinygrad.codegen.linearizer import UOps, UOp
  from tinygrad.ops import UnaryOps, BinaryOps, TernaryOps
  from tinygrad.helpers import prod, strip_parens, getenv
  from tinygrad.dtype import ImageDType, dtypes, DType, PtrDType
label_desc: Use image node_id 1 for calling a global function with example usage: uops_to_cstyle(lang, function_name, uops) and returns: lang, ret