/*
** Automatically generated from `file_util.m'
** by the Mercury compiler,
** version rotd-2023-01-21
** configured for x86_64-pc-linux-gnu.
** Do not edit.
**
** The autoconfigured grade settings governing
** the generation of this C file were
**
** TAG_BITS=2
** UNBOXED_FLOAT=no
** UNBOXED_INT64S=no
** PREGENERATED_DIST=yes
** HIGHLEVEL_CODE=yes
**
** END_OF_C_GRADE_INFO
*/


// :- module libs.file_util.
// :- implementation.

/*
INIT mercury__libs__file_util__init
ENDINIT
*/

#include "libs.file_util.mih"


#include "array.mih"
#include "assoc_list.mih"
#include "benchmarking.mih"
#include "bimap.mih"
#include "bitmap.mih"
#include "bool.mih"
#include "builtin.mih"
#include "char.mih"
#include "construct.mih"
#include "cord.mih"
#include "deconstruct.mih"
#include "dir.mih"
#include "enum.mih"
#include "exception.mih"
#include "getopt.mih"
#include "io.mih"
#include "libs.mih"
#include "list.mih"
#include "map.mih"
#include "maybe.mih"
#include "mdbcomp.mih"
#include "one_or_more.mih"
#include "one_or_more_map.mih"
#include "ops.mih"
#include "pair.mih"
#include "parse_tree.mih"
#include "pretty_printer.mih"
#include "private_builtin.mih"
#include "set.mih"
#include "set_ordlist.mih"
#include "stm_builtin.mih"
#include "store.mih"
#include "stream.mih"
#include "string.mih"
#include "term.mih"
#include "term_context.mih"
#include "time.mih"
#include "tree234.mih"
#include "type_desc.mih"
#include "unit.mih"
#include "univ.mih"
#include "varset.mih"
#include "io.file.mih"
#include "libs.compute_grade.mih"
#include "libs.globals.mih"
#include "libs.maybe_succeeded.mih"
#include "libs.op_mode.mih"
#include "libs.optimization_options.mih"
#include "libs.options.mih"
#include "libs.shell_util.mih"
#include "libs.trace_params.mih"
#include "mdbcomp.feedback.mih"
#include "mdbcomp.prim_data.mih"
#include "mdbcomp.sym_name.mih"
#include "parse_tree.error_spec.mih"
#include "parse_tree.prog_data.mih"
#include "parse_tree.prog_data_pragma.mih"
#include "parse_tree.prog_item.mih"
#include "parse_tree.set_of_var.mih"
#include "string.format.mih"
#include "string.parse_util.mih"




static const MR_FA_TypeInfo_Struct1 libs__file_util__list__ti_list_1builtin__type_ctor_info_string_0;

static const MR_VA_PseudoTypeInfo_Struct4 libs__file_util____vpti_pred_4__plain_io__type_ctor_info_output_stream_0__plain_list__ti_list_1builtin__type_ctor_info_string_0__plain_io__type_ctor_info_state_0__plain_io__type_ctor_info_state_0;

static const MR_FA_PseudoTypeInfo_Struct1 libs__file_util__list__pti_list_1__plain_builtin__type_ctor_info_string_0;

static void MR_CALL 
libs__file_util__IntroducedFrom__pred__output_to_file_stream__164__1_5_p_0(
  MR_Word Action0_11,
  MR_Word FileStream_19,
  MR_Word * LambdaHeadVar__1_44);

static void MR_CALL 
libs__file_util__copy_stream_5_p_0(
  MR_Word InputStream_6,
  MR_Word OutputStream_7,
  MR_Word * Res_8);

static MR_Box MR_CALL 
libs__file_util__make_install_dir_command_3_f_0_1(
  MR_Box closure_arg,
  MR_Box wrapper_arg_1);

static MR_Box MR_CALL 
libs__file_util__make_install_file_command_3_f_0_1(
  MR_Box closure_arg,
  MR_Box wrapper_arg_1);

static void MR_CALL 
libs__file_util__write_include_file_contents_5_p_0_1(
  MR_Box closure_arg,
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box * wrapper_arg_3);

static void MR_CALL 
libs__file_util__output_to_file_stream_7_p_0_2(
  MR_Box closure_arg,
  MR_Box wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box * wrapper_arg_3);

static void MR_CALL 
libs__file_util__output_to_file_stream_7_p_0_1(
  MR_Box closure_arg,
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box * wrapper_arg_3);

static MR_bool MR_CALL 
libs__file_util____Unify____dir_name_0_0_10001(
  MR_Box wrapper_arg_1,
  MR_Box wrapper_arg_2);

static void MR_CALL 
libs__file_util____Compare____dir_name_0_0_10001(
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box wrapper_arg_3);

static MR_bool MR_CALL 
libs__file_util____Unify____file_name_0_0_10001(
  MR_Box wrapper_arg_1,
  MR_Box wrapper_arg_2);

static void MR_CALL 
libs__file_util____Compare____file_name_0_0_10001(
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box wrapper_arg_3);


static /* final */ const MR_Box libs__file_util_scalar_common_1[1][2];

static /* final */ const MR_Box libs__file_util_scalar_common_2[2][8];

static /* final */ const MR_Box libs__file_util_scalar_common_3[1][7];

static /* final */ const MR_Box libs__file_util_scalar_common_4[1][5];

static /* final */ const MR_Box libs__file_util_scalar_common_5[2][3];




static /* final */ const MR_Box libs__file_util_scalar_common_1[1][2] = {
  /* row   0 */
  {
    ((MR_Box) (&mercury__list__list__type_ctor_info_list_1)),
    ((MR_Box) (&mercury__builtin__builtin__type_ctor_info_string_0))
  },
};

static /* final */ const MR_Box libs__file_util_scalar_common_2[2][8] = {
  /* row   0 */
  {
    NULL,
    ((MR_Box) (NULL)),
    ((MR_Box) ((MR_Integer) 5)),
    ((MR_Box) (&libs__file_util____vpti_pred_4__plain_io__type_ctor_info_output_stream_0__plain_list__ti_list_1builtin__type_ctor_info_string_0__plain_io__type_ctor_info_state_0__plain_io__type_ctor_info_state_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_output_stream_0)),
    ((MR_Box) (&libs__file_util__list__pti_list_1__plain_builtin__type_ctor_info_string_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_state_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_state_0))
  },
  /* row   1 */
  {
    NULL,
    ((MR_Box) (NULL)),
    ((MR_Box) ((MR_Integer) 5)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_input_stream_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_output_stream_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_res_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_state_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_state_0))
  },
};

static /* final */ const MR_Box libs__file_util_scalar_common_3[1][7] = {
  /* row   0 */
  {
    NULL,
    ((MR_Box) (NULL)),
    ((MR_Box) ((MR_Integer) 4)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_output_stream_0)),
    ((MR_Box) (&mercury__builtin__builtin__type_ctor_info_string_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_state_0)),
    ((MR_Box) (&mercury__io__io__type_ctor_info_state_0))
  },
};

static /* final */ const MR_Box libs__file_util_scalar_common_4[1][5] = {
  /* row   0 */
  {
    NULL,
    ((MR_Box) (NULL)),
    ((MR_Box) ((MR_Integer) 2)),
    ((MR_Box) (&mercury__builtin__builtin__type_ctor_info_string_0)),
    ((MR_Box) (&mercury__builtin__builtin__type_ctor_info_string_0))
  },
};

static /* final */ const MR_Box libs__file_util_scalar_common_5[2][3] = {
  /* row   0 */
  {
    ((MR_Box) (&libs__file_util_scalar_common_4[0])),
    ((MR_Box) (libs__file_util__make_install_file_command_3_f_0_1)),
    ((MR_Box) ((MR_Integer) 0))
  },
  /* row   1 */
  {
    ((MR_Box) (&libs__file_util_scalar_common_4[0])),
    ((MR_Box) (libs__file_util__make_install_dir_command_3_f_0_1)),
    ((MR_Box) ((MR_Integer) 0))
  },
};



#include "array.mh"
#include "bitmap.mh"
#include "dir.mh"
#include "io.mh"
#include "stm_builtin.mh"
#include "store.mh"
#include "string.mh"
#include "time.mh"
#include "io.stream_ops.mh"



static const MR_FA_TypeInfo_Struct1 libs__file_util__list__ti_list_1builtin__type_ctor_info_string_0 = {
  &mercury__list__list__type_ctor_info_list_1,
  {
    (MR_TypeInfo) (&mercury__builtin__builtin__type_ctor_info_string_0)
  }
};

static const MR_VA_PseudoTypeInfo_Struct4 libs__file_util____vpti_pred_4__plain_io__type_ctor_info_output_stream_0__plain_list__ti_list_1builtin__type_ctor_info_string_0__plain_io__type_ctor_info_state_0__plain_io__type_ctor_info_state_0 = {
  &mercury__builtin__builtin__type_ctor_info_pred_0,
  (MR_Integer) 4,
  {
    (MR_PseudoTypeInfo) (&mercury__io__io__type_ctor_info_output_stream_0),
    (MR_PseudoTypeInfo) (&libs__file_util__list__ti_list_1builtin__type_ctor_info_string_0),
    (MR_PseudoTypeInfo) (&mercury__io__io__type_ctor_info_state_0),
    (MR_PseudoTypeInfo) (&mercury__io__io__type_ctor_info_state_0)
  }
};

static const MR_FA_PseudoTypeInfo_Struct1 libs__file_util__list__pti_list_1__plain_builtin__type_ctor_info_string_0 = {
  &mercury__list__list__type_ctor_info_list_1,
  {
    (MR_PseudoTypeInfo) (&mercury__builtin__builtin__type_ctor_info_string_0)
  }
};

const MR_TypeCtorInfo_Struct libs__file_util__libs__file_util__type_ctor_info_dir_name_0 = {
  (MR_Integer) 0,
  UINT8_C(18),
  INT8_C(-1),
  MR_TYPECTOR_REP_EQUIV_GROUND,
  ((MR_Box) (libs__file_util____Unify____dir_name_0_0_10001)),
  ((MR_Box) (libs__file_util____Compare____dir_name_0_0_10001)),
  (MR_String) "libs.file_util",
  (MR_String) "dir_name",
  { NULL },
  { (MR_PseudoTypeInfo) (&mercury__builtin__builtin__type_ctor_info_string_0) },
  (MR_Integer) -1,
  UINT16_C(0),
  NULL,

};

const MR_TypeCtorInfo_Struct libs__file_util__libs__file_util__type_ctor_info_file_name_0 = {
  (MR_Integer) 0,
  UINT8_C(18),
  INT8_C(-1),
  MR_TYPECTOR_REP_EQUIV_GROUND,
  ((MR_Box) (libs__file_util____Unify____file_name_0_0_10001)),
  ((MR_Box) (libs__file_util____Compare____file_name_0_0_10001)),
  (MR_String) "libs.file_util",
  (MR_String) "file_name",
  { NULL },
  { (MR_PseudoTypeInfo) (&mercury__builtin__builtin__type_ctor_info_string_0) },
  (MR_Integer) -1,
  UINT16_C(0),
  NULL,

};

static void MR_CALL 
libs__file_util__IntroducedFrom__pred__output_to_file_stream__164__1_5_p_0(
  MR_Word Action0_11,
  MR_Word FileStream_19,
  MR_Word * LambdaHeadVar__1_44)
{
  void MR_CALL (* func_0)(MR_Box, MR_Box, MR_Box *, MR_Box, MR_Box *) = ((void MR_CALL (*)(MR_Box, MR_Box, MR_Box *, MR_Box, MR_Box *)) ((MR_hl_field(0, Action0_11, (MR_Integer) 1))));
  MR_Box conv2_LambdaHeadVar__1_44;
  MR_Box conv1_LambdaHeadVar__3_46;

  func_0(((MR_Box) (Action0_11)), ((MR_Box) (FileStream_19)), &conv2_LambdaHeadVar__1_44, ((MR_Box) ((MR_Integer) 0)), &conv1_LambdaHeadVar__3_46);
  *LambdaHeadVar__1_44 = ((MR_Word) (conv2_LambdaHeadVar__1_44));
}

void MR_CALL 
libs__file_util____Compare____file_name_0_0(
  MR_Word * HeadVar__1_1,
  MR_String HeadVar__2_2,
  MR_String HeadVar__3_3)
{
  MR_String Cast_HeadVar1_4 = HeadVar__2_2;
  MR_String Cast_HeadVar2_5 = HeadVar__3_3;

  mercury__private_builtin__builtin_compare_string_3_p_0(HeadVar__1_1, Cast_HeadVar1_4, Cast_HeadVar2_5);
}

MR_bool MR_CALL 
libs__file_util____Unify____file_name_0_0(
  MR_String HeadVar__1_1,
  MR_String HeadVar__2_2)
{
  MR_bool succeeded;
  MR_String Cast_HeadVar1_3 = HeadVar__1_1;
  MR_String Cast_HeadVar2_4 = HeadVar__2_2;

  succeeded = (strcmp(Cast_HeadVar1_3, Cast_HeadVar2_4) == 0);
  return succeeded;
}

void MR_CALL 
libs__file_util____Compare____dir_name_0_0(
  MR_Word * HeadVar__1_1,
  MR_String HeadVar__2_2,
  MR_String HeadVar__3_3)
{
  MR_String Cast_HeadVar1_4 = HeadVar__2_2;
  MR_String Cast_HeadVar2_5 = HeadVar__3_3;

  mercury__private_builtin__builtin_compare_string_3_p_0(HeadVar__1_1, Cast_HeadVar1_4, Cast_HeadVar2_5);
}

MR_bool MR_CALL 
libs__file_util____Unify____dir_name_0_0(
  MR_String HeadVar__1_1,
  MR_String HeadVar__2_2)
{
  MR_bool succeeded;
  MR_String Cast_HeadVar1_3 = HeadVar__1_1;
  MR_String Cast_HeadVar2_4 = HeadVar__2_2;

  succeeded = (strcmp(Cast_HeadVar1_3, Cast_HeadVar2_4) == 0);
  return succeeded;
}

static void MR_CALL 
libs__file_util__copy_stream_5_p_0(
  MR_Word InputStream_6,
  MR_Word OutputStream_7,
  MR_Word * Res_8)
{
  MR_Word ReadRes_10;

  mercury__io__read_file_as_string_4_p_0(InputStream_6, &ReadRes_10);
  if (((MR_tag((MR_Word) ReadRes_10)) == (MR_Integer) 1))
  {
    MR_Word Error_13 = ((MR_Word) ((MR_hl_field(1, ReadRes_10, (MR_Integer) 1))));

    {
      MR_Word base;
      base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
      *Res_8 = base;
      MR_hl_field(1, base, 0) = ((MR_Box) (Error_13));
    }
  }
  else
  {
    MR_String InputContents_11 = ((MR_String) ((MR_hl_field(0, ReadRes_10, (MR_Integer) 0))));

    mercury__io__write_string_4_p_0(OutputStream_7, InputContents_11);
    *Res_8 = (MR_Word) ((MR_Unsigned) 0U);
  }
}

void MR_CALL 
libs__file_util__open_temp_input_4_p_0(
  MR_Word * Result_5,
  MR_Word Pred_6)
{
  MR_Word TempFileResult_8;

  mercury__io__file__make_temp_file_3_p_0(&TempFileResult_8);
  if (((MR_tag((MR_Word) TempFileResult_8)) == (MR_Integer) 1))
  {
    MR_String Var_33;
    MR_String Var_37;
    MR_Word Error_39 = ((MR_Word) ((MR_hl_field(1, TempFileResult_8, (MR_Integer) 0))));

    Var_37 = mercury__io__error_message_1_f_0(Error_39);
    Var_33 = mercury__string__f_43_43_2_f_0((MR_String) "could not create temporary file: ", Var_37);
    {
      MR_Word base;
      base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
      *Result_5 = base;
      MR_hl_field(1, base, 0) = ((MR_Box) (Var_33));
    }
  }
  else
  {
    MR_String TempFileName_9 = ((MR_String) ((MR_hl_field(0, TempFileResult_8, (MR_Integer) 0))));
    MR_Word PredResult_10;
    void MR_CALL (* func_0)(MR_Box, MR_Box, MR_Box *, MR_Box, MR_Box *) = ((void MR_CALL (*)(MR_Box, MR_Box, MR_Box *, MR_Box, MR_Box *)) ((MR_hl_field(0, Pred_6, (MR_Integer) 1))));
    MR_Box conv2_PredResult_10;
    MR_Box conv1_STATE_VARIABLE_IO_20_20;

    func_0(((MR_Box) (Pred_6)), ((MR_Box) (TempFileName_9)), &conv2_PredResult_10, ((MR_Box) ((MR_Integer) 0)), &conv1_STATE_VARIABLE_IO_20_20);
    PredResult_10 = ((MR_Word) (conv2_PredResult_10));
    if ((PredResult_10 == (MR_Word) ((MR_Unsigned) 0U)))
    {
      MR_Word OpenResult_11;

      mercury__io__open_input_4_p_0(TempFileName_9, &OpenResult_11);
      if (((MR_tag((MR_Word) OpenResult_11)) == (MR_Integer) 1))
      {
        MR_Word Error_13 = ((MR_Word) ((MR_hl_field(1, OpenResult_11, (MR_Integer) 0))));
        MR_String Var_23;
        MR_String Var_29;
        MR_String Var_41;
        MR_String Var_42;
        MR_Word Var_14;

        Var_29 = mercury__io__error_message_1_f_0(Error_13);
        Var_41 = mercury__string__f_43_43_2_f_0((MR_String) "\': ", Var_29);
        Var_42 = mercury__string__f_43_43_2_f_0(TempFileName_9, Var_41);
        Var_23 = mercury__string__f_43_43_2_f_0((MR_String) "could not open \140", Var_42);
        {
          MR_Word base;
          base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
          *Result_5 = base;
          MR_hl_field(1, base, 0) = ((MR_Box) (Var_23));
        }
        mercury__io__file__remove_file_4_p_0(TempFileName_9, &Var_14);
      }
      else
      {
        MR_Word Stream_12 = ((MR_Word) ((MR_hl_field(0, OpenResult_11, (MR_Integer) 0))));
        MR_Tuple Var_22;

        {
          Var_22 = (MR_Tuple) MR_new_object(MR_Tuple, (2 * sizeof(MR_Word)), NULL, NULL);
          MR_hl_field(0, Var_22, 0) = ((MR_Box) (TempFileName_9));
          MR_hl_field(0, Var_22, 1) = ((MR_Box) (Stream_12));
        }
        {
          MR_Word base;
          base = (MR_Word) MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL);
          *Result_5 = base;
          MR_hl_field(0, base, 0) = ((MR_Box) (Var_22));
        }
      }
    }
    else
    {
      MR_String ErrorMessage_15 = ((MR_String) ((MR_hl_field(1, PredResult_10, (MR_Integer) 0))));
      MR_Word Var_16;

      mercury__io__file__remove_file_4_p_0(TempFileName_9, &Var_16);
      {
        MR_Word base;
        base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
        *Result_5 = base;
        MR_hl_field(1, base, 0) = ((MR_Box) (ErrorMessage_15));
      }
    }
  }
}

void MR_CALL 
libs__file_util__open_temp_output_3_p_0(
  MR_Word * Result_4)
{
  MR_Word TempFileResult_6;

  mercury__io__file__make_temp_file_3_p_0(&TempFileResult_6);
  if (((MR_tag((MR_Word) TempFileResult_6)) == (MR_Integer) 1))
  {
    MR_String Var_25;
    MR_String Var_29;
    MR_Word Error_31 = ((MR_Word) ((MR_hl_field(1, TempFileResult_6, (MR_Integer) 0))));

    Var_29 = mercury__io__error_message_1_f_0(Error_31);
    Var_25 = mercury__string__f_43_43_2_f_0((MR_String) "could not create temporary file: ", Var_29);
    {
      MR_Word base;
      base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
      *Result_4 = base;
      MR_hl_field(1, base, 0) = ((MR_Box) (Var_25));
    }
  }
  else
  {
    MR_String TempFileName_10 = ((MR_String) ((MR_hl_field(0, TempFileResult_6, (MR_Integer) 0))));
    MR_Word OpenResult_11;

    mercury__io__open_output_4_p_0(TempFileName_10, &OpenResult_11);
    if (((MR_tag((MR_Word) OpenResult_11)) == (MR_Integer) 1))
    {
      MR_Word Error_13 = ((MR_Word) ((MR_hl_field(1, OpenResult_11, (MR_Integer) 0))));
      MR_String Var_17;
      MR_String Var_23;
      MR_String Var_33;
      MR_String Var_34;
      MR_Word Var_14;

      mercury__io__file__remove_file_4_p_0(TempFileName_10, &Var_14);
      Var_23 = mercury__io__error_message_1_f_0(Error_13);
      Var_33 = mercury__string__f_43_43_2_f_0((MR_String) "\': ", Var_23);
      Var_34 = mercury__string__f_43_43_2_f_0(TempFileName_10, Var_33);
      Var_17 = mercury__string__f_43_43_2_f_0((MR_String) "could not open temporary file \140", Var_34);
      {
        MR_Word base;
        base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
        *Result_4 = base;
        MR_hl_field(1, base, 0) = ((MR_Box) (Var_17));
      }
    }
    else
    {
      MR_Word Stream_12 = ((MR_Word) ((MR_hl_field(0, OpenResult_11, (MR_Integer) 0))));
      MR_Tuple Var_16;

      {
        Var_16 = (MR_Tuple) MR_new_object(MR_Tuple, (2 * sizeof(MR_Word)), NULL, NULL);
        MR_hl_field(0, Var_16, 0) = ((MR_Box) (TempFileName_10));
        MR_hl_field(0, Var_16, 1) = ((MR_Box) (Stream_12));
      }
      {
        MR_Word base;
        base = (MR_Word) MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL);
        *Result_4 = base;
        MR_hl_field(0, base, 0) = ((MR_Box) (Var_16));
      }
    }
  }
}

void MR_CALL 
libs__file_util__open_temp_output_6_p_0(
  MR_String Dir_7,
  MR_String Prefix_8,
  MR_String Suffix_9,
  MR_Word * Result_10)
{
  MR_Word TempFileResult_12;

  mercury__io__file__make_temp_file_6_p_0(Dir_7, Prefix_8, Suffix_9, &TempFileResult_12);
  if (((MR_tag((MR_Word) TempFileResult_12)) == (MR_Integer) 1))
  {
    MR_String Var_31;
    MR_String Var_35;
    MR_Word Error_37 = ((MR_Word) ((MR_hl_field(1, TempFileResult_12, (MR_Integer) 0))));

    Var_35 = mercury__io__error_message_1_f_0(Error_37);
    Var_31 = mercury__string__f_43_43_2_f_0((MR_String) "could not create temporary file: ", Var_35);
    {
      MR_Word base;
      base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
      *Result_10 = base;
      MR_hl_field(1, base, 0) = ((MR_Box) (Var_31));
    }
  }
  else
  {
    MR_String TempFileName_16 = ((MR_String) ((MR_hl_field(0, TempFileResult_12, (MR_Integer) 0))));
    MR_Word OpenResult_17;

    mercury__io__open_output_4_p_0(TempFileName_16, &OpenResult_17);
    if (((MR_tag((MR_Word) OpenResult_17)) == (MR_Integer) 1))
    {
      MR_Word Error_19 = ((MR_Word) ((MR_hl_field(1, OpenResult_17, (MR_Integer) 0))));
      MR_String Var_23;
      MR_String Var_29;
      MR_String Var_39;
      MR_String Var_40;
      MR_Word Var_20;

      mercury__io__file__remove_file_4_p_0(TempFileName_16, &Var_20);
      Var_29 = mercury__io__error_message_1_f_0(Error_19);
      Var_39 = mercury__string__f_43_43_2_f_0((MR_String) "\': ", Var_29);
      Var_40 = mercury__string__f_43_43_2_f_0(TempFileName_16, Var_39);
      Var_23 = mercury__string__f_43_43_2_f_0((MR_String) "could not open temporary file \140", Var_40);
      {
        MR_Word base;
        base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
        *Result_10 = base;
        MR_hl_field(1, base, 0) = ((MR_Box) (Var_23));
      }
    }
    else
    {
      MR_Word Stream_18 = ((MR_Word) ((MR_hl_field(0, OpenResult_17, (MR_Integer) 0))));
      MR_Tuple Var_22;

      {
        Var_22 = (MR_Tuple) MR_new_object(MR_Tuple, (2 * sizeof(MR_Word)), NULL, NULL);
        MR_hl_field(0, Var_22, 0) = ((MR_Box) (TempFileName_16));
        MR_hl_field(0, Var_22, 1) = ((MR_Box) (Stream_18));
      }
      {
        MR_Word base;
        base = (MR_Word) MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL);
        *Result_10 = base;
        MR_hl_field(0, base, 0) = ((MR_Box) (Var_22));
      }
    }
  }
}

static MR_Box MR_CALL 
libs__file_util__make_install_dir_command_3_f_0_1(
  MR_Box closure_arg,
  MR_Box wrapper_arg_1)
{
  MR_Box wrapper_arg_2;
  MR_Box closure = closure_arg;
  MR_String conv0_HeadVar__2_2;

  conv0_HeadVar__2_2 = libs__shell_util__quote_shell_cmd_arg_1_f_0(((MR_String) (wrapper_arg_1)));
  wrapper_arg_2 = ((MR_Box) (conv0_HeadVar__2_2));
  return wrapper_arg_2;
}

MR_String MR_CALL 
libs__file_util__make_install_dir_command_3_f_0(
  MR_Word Globals_5,
  MR_String SourceDirName_6,
  MR_String InstallDir_7)
{
  MR_String Command_8;
  MR_Word FileInstallCmd_9;
  MR_String InstallCmd_10;
  MR_String InstallCmdDirOpt_11;
  MR_Word Var_13;
  MR_Word Var_15;
  MR_Word Var_16;
  MR_Word Var_17;
  MR_Word Var_18;

  libs__globals__get_file_install_cmd_2_p_0(Globals_5, &FileInstallCmd_9);
  if ((FileInstallCmd_9 == (MR_Word) ((MR_Unsigned) 0U)))
  {
    InstallCmd_10 = (MR_String) "cp";
    InstallCmdDirOpt_11 = (MR_String) "-R";
  }
  else
  {
    InstallCmd_10 = ((MR_String) ((MR_hl_field(1, FileInstallCmd_9, (MR_Integer) 0))));
    InstallCmdDirOpt_11 = ((MR_String) ((MR_hl_field(1, FileInstallCmd_9, (MR_Integer) 1))));
  }
  {
    Var_18 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (2 * sizeof(MR_Word)), NULL, NULL));
    MR_hl_field(1, Var_18, 0) = ((MR_Box) (InstallDir_7));
    MR_hl_field(1, Var_18, 1) = ((MR_Box) ((MR_Unsigned) 0U));
  }
  {
    Var_17 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (2 * sizeof(MR_Word)), NULL, NULL));
    MR_hl_field(1, Var_17, 0) = ((MR_Box) (SourceDirName_6));
    MR_hl_field(1, Var_17, 1) = ((MR_Box) (Var_18));
  }
  {
    Var_16 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (2 * sizeof(MR_Word)), NULL, NULL));
    MR_hl_field(1, Var_16, 0) = ((MR_Box) (InstallCmdDirOpt_11));
    MR_hl_field(1, Var_16, 1) = ((MR_Box) (Var_17));
  }
  {
    Var_15 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (2 * sizeof(MR_Word)), NULL, NULL));
    MR_hl_field(1, Var_15, 0) = ((MR_Box) (InstallCmd_10));
    MR_hl_field(1, Var_15, 1) = ((MR_Box) (Var_16));
  }
  Var_13 = mercury__list__map_2_f_0((MR_Word) (&mercury__builtin__builtin__type_ctor_info_string_0), (MR_Word) (&mercury__builtin__builtin__type_ctor_info_string_0), (MR_Word) (&libs__file_util_scalar_common_5[1]), Var_15);
  Command_8 = mercury__string__join_list_2_f_0((MR_String) "   ", Var_13);
  return Command_8;
}

static MR_Box MR_CALL 
libs__file_util__make_install_file_command_3_f_0_1(
  MR_Box closure_arg,
  MR_Box wrapper_arg_1)
{
  MR_Box wrapper_arg_2;
  MR_Box closure = closure_arg;
  MR_String conv0_HeadVar__2_2;

  conv0_HeadVar__2_2 = libs__shell_util__quote_shell_cmd_arg_1_f_0(((MR_String) (wrapper_arg_1)));
  wrapper_arg_2 = ((MR_Box) (conv0_HeadVar__2_2));
  return wrapper_arg_2;
}

MR_String MR_CALL 
libs__file_util__make_install_file_command_3_f_0(
  MR_Word Globals_5,
  MR_String FileName_6,
  MR_String InstallDir_7)
{
  MR_String Command_8;
  MR_Word FileInstallCmd_9;
  MR_String InstallCmd_10;
  MR_Word Var_13;
  MR_Word Var_15;
  MR_Word Var_16;
  MR_Word Var_17;

  libs__globals__get_file_install_cmd_2_p_0(Globals_5, &FileInstallCmd_9);
  if ((FileInstallCmd_9 == (MR_Word) ((MR_Unsigned) 0U)))
    InstallCmd_10 = (MR_String) "cp";
  else
    InstallCmd_10 = ((MR_String) ((MR_hl_field(1, FileInstallCmd_9, (MR_Integer) 0))));
  {
    Var_17 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (2 * sizeof(MR_Word)), NULL, NULL));
    MR_hl_field(1, Var_17, 0) = ((MR_Box) (InstallDir_7));
    MR_hl_field(1, Var_17, 1) = ((MR_Box) ((MR_Unsigned) 0U));
  }
  {
    Var_16 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (2 * sizeof(MR_Word)), NULL, NULL));
    MR_hl_field(1, Var_16, 0) = ((MR_Box) (FileName_6));
    MR_hl_field(1, Var_16, 1) = ((MR_Box) (Var_17));
  }
  {
    Var_15 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (2 * sizeof(MR_Word)), NULL, NULL));
    MR_hl_field(1, Var_15, 0) = ((MR_Box) (InstallCmd_10));
    MR_hl_field(1, Var_15, 1) = ((MR_Box) (Var_16));
  }
  Var_13 = mercury__list__map_2_f_0((MR_Word) (&mercury__builtin__builtin__type_ctor_info_string_0), (MR_Word) (&mercury__builtin__builtin__type_ctor_info_string_0), (MR_Word) (&libs__file_util_scalar_common_5[0]), Var_15);
  Command_8 = mercury__string__join_list_2_f_0((MR_String) "   ", Var_13);
  return Command_8;
}

void MR_CALL 
libs__file_util__report_error_3_p_0(
  MR_String ErrorMessage_4)
{
  MR_Word Stream_6;

  mercury__io__output_stream_3_p_0(&Stream_6);
  libs__file_util__report_error_4_p_0(Stream_6, ErrorMessage_4);
}

void MR_CALL 
libs__file_util__unable_to_open_file_5_p_0(
  MR_Word ErrorStream_6,
  MR_String FileName_7,
  MR_Word IOErr_8)
{
  MR_String Var_18;

  Var_18 = mercury__io__error_message_1_f_0(IOErr_8);
  mercury__io__write_string_4_p_0(ErrorStream_6, (MR_String) "Unable to open file \'");
  mercury__io__write_string_4_p_0(ErrorStream_6, FileName_7);
  mercury__io__write_string_4_p_0(ErrorStream_6, (MR_String) "\': ");
  mercury__io__write_string_4_p_0(ErrorStream_6, Var_18);
  mercury__io__write_string_4_p_0(ErrorStream_6, (MR_String) "\n");
  mercury__io__set_exit_status_3_p_0((MR_Integer) 1);
}

void MR_CALL 
libs__file_util__maybe_flush_output_to_stream_3_p_0(
  MR_Word HeadVar__1_1)
{
  if (!((HeadVar__1_1 == (MR_Word) ((MR_Unsigned) 0U))))
  {
    MR_Word Stream_4 = ((MR_Word) ((MR_hl_field(1, HeadVar__1_1, (MR_Integer) 0))));

    mercury__io__flush_output_3_p_0(Stream_4);
  }
}

void MR_CALL 
libs__file_util__maybe_flush_output_4_p_0(
  MR_Word HeadVar__1_1,
  MR_Word HeadVar__2_2)
{
  switch (HeadVar__2_2) {
    default: /*NOTREACHED*/ MR_assert(0);
    case (MR_Integer) 0:
      {
      }
      break;
    case (MR_Integer) 1:
      mercury__io__flush_output_3_p_0(HeadVar__1_1);
      break;
  }
}

void MR_CALL 
libs__file_util__maybe_write_string_to_stream_4_p_0(
  MR_Word HeadVar__1_1,
  MR_String String_2)
{
  if (!((HeadVar__1_1 == (MR_Word) ((MR_Unsigned) 0U))))
  {
    MR_Word Stream_5 = ((MR_Word) ((MR_hl_field(1, HeadVar__1_1, (MR_Integer) 0))));

    mercury__io__write_string_4_p_0(Stream_5, String_2);
  }
}

void MR_CALL 
libs__file_util__maybe_report_stats_to_stream_3_p_0(
  MR_Word HeadVar__1_1)
{
  if (!((HeadVar__1_1 == (MR_Word) ((MR_Unsigned) 0U))))
  {
    MR_Word Stream_4 = ((MR_Word) ((MR_hl_field(1, HeadVar__1_1, (MR_Integer) 0))));

    mercury__benchmarking__report_standard_stats_3_p_0(Stream_4);
  }
}

void MR_CALL 
libs__file_util__maybe_report_stats_4_p_0(
  MR_Word HeadVar__1_1,
  MR_Word HeadVar__2_2)
{
  switch (HeadVar__2_2) {
    default: /*NOTREACHED*/ MR_assert(0);
    case (MR_Integer) 0:
      {
      }
      break;
    case (MR_Integer) 1:
      mercury__benchmarking__report_standard_stats_3_p_0(HeadVar__1_1);
      break;
  }
}

void MR_CALL 
libs__file_util__get_install_name_option_3_p_0(
  MR_Word Globals_4,
  MR_String OutputFileName_5,
  MR_String * InstallNameOpt_6)
{
  MR_bool succeeded;
  MR_String InstallNameFlag_7;
  MR_String InstallNamePath0_8;
  MR_String InstallNamePath_11;
  MR_String Var_21;
  MR_String Var_22;

  libs__globals__lookup_string_option_3_p_0(Globals_4, (MR_Integer) 655, &InstallNameFlag_7);
  libs__globals__lookup_string_option_3_p_0(Globals_4, (MR_Integer) 656, &InstallNamePath0_8);
  succeeded = (strcmp(InstallNamePath0_8, (MR_String) "") == 0);
  if (succeeded)
  {
    MR_String InstallPrefix_9;
    MR_String GradeDir_10;
    MR_String Var_15;
    MR_String Var_16;
    MR_String Var_17;

    libs__globals__lookup_string_option_3_p_0(Globals_4, (MR_Integer) 669, &InstallPrefix_9);
    libs__compute_grade__grade_directory_component_2_p_0(Globals_4, &GradeDir_10);
    Var_17 = mercury__dir__f_slash_2_f_0(InstallPrefix_9, (MR_String) "lib");
    Var_16 = mercury__dir__f_slash_2_f_0(Var_17, (MR_String) "mercury");
    Var_15 = mercury__dir__f_slash_2_f_0(Var_16, (MR_String) "lib");
    InstallNamePath_11 = mercury__dir__f_slash_2_f_0(Var_15, GradeDir_10);
  }
  else
    InstallNamePath_11 = InstallNamePath0_8;
  Var_22 = libs__shell_util__quote_shell_cmd_arg_1_f_0(InstallNamePath_11);
  Var_21 = mercury__dir__f_slash_2_f_0(Var_22, OutputFileName_5);
  *InstallNameOpt_6 = mercury__string__f_43_43_2_f_0(InstallNameFlag_7, Var_21);
}

static void MR_CALL 
libs__file_util__write_include_file_contents_5_p_0_1(
  MR_Box closure_arg,
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box * wrapper_arg_3)
{
  MR_Box closure = closure_arg;
  MR_Word conv0_Res_8;

  libs__file_util__copy_stream_5_p_0(((MR_Word) ((MR_hl_field(0, closure, (MR_Integer) 3)))), ((MR_Word) ((MR_hl_field(0, closure, (MR_Integer) 4)))), &conv0_Res_8);
  *wrapper_arg_1 = ((MR_Box) (conv0_Res_8));
}

void MR_CALL 
libs__file_util__write_include_file_contents_5_p_0(
  MR_Word OutputStream_6,
  MR_String FileName_7,
  MR_Word * Res_8)
{
  MR_bool succeeded;
  MR_Word MaybeFileType_11;

  mercury__io__file__file_type_5_p_0((MR_Integer) 1, FileName_7, &MaybeFileType_11);
  if (((MR_tag((MR_Word) MaybeFileType_11)) == (MR_Integer) 1))
  {
    MR_Word FileTypeError_16 = ((MR_Word) ((MR_hl_field(1, MaybeFileType_11, (MR_Integer) 0))));
    MR_String Var_24;
    MR_String Var_25;
    MR_String Message_27;
    MR_String Var_35;
    MR_String Var_36;

    Var_24 = mercury__io__error_message_1_f_0(FileTypeError_16);
    Message_27 = mercury__string__remove_prefix_if_present_2_f_0((MR_String) "can\'t find file type: ", Var_24);
    Var_35 = mercury__string__f_43_43_2_f_0((MR_String) "\' for input: ", Message_27);
    Var_36 = mercury__string__f_43_43_2_f_0(FileName_7, Var_35);
    Var_25 = mercury__string__f_43_43_2_f_0((MR_String) "can\'t open \140", Var_36);
    {
      MR_Word base;
      base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
      *Res_8 = base;
      MR_hl_field(1, base, 0) = ((MR_Box) (Var_25));
    }
  }
  else
  {
    MR_Word FileType_12 = ((MR_Word) ((MR_hl_field(0, MaybeFileType_11, (MR_Integer) 0))));

    switch (FileType_12) {
      default:
        succeeded = MR_FALSE;
        break;
      case (MR_Integer) 0:
        succeeded = MR_TRUE;
        break;
      case (MR_Integer) 10:
        succeeded = MR_TRUE;
        break;
    }
    if (succeeded)
    {
      MR_Word CopyRes_13;
      MR_Word OpenRes_38;

      mercury__io__open_input_4_p_0(FileName_7, &OpenRes_38);
      if (((MR_tag((MR_Word) OpenRes_38)) == (MR_Integer) 1))
      {
        MR_Word Error_44 = ((MR_Word) ((MR_hl_field(1, OpenRes_38, (MR_Integer) 0))));

        {
          CopyRes_13 = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
          MR_hl_field(1, CopyRes_13, 0) = ((MR_Box) (Error_44));
        }
      }
      else
      {
        MR_Word InputStream_39 = ((MR_Word) ((MR_hl_field(0, OpenRes_38, (MR_Integer) 0))));
        MR_Word TryResult_40;
        MR_Word Var_42;

        {
          Var_42 = (MR_Word) MR_new_object(MR_Word, (5 * sizeof(MR_Word)), NULL, NULL);
          MR_hl_field(0, Var_42, 0) = ((MR_Box) (&libs__file_util_scalar_common_2[1]));
          MR_hl_field(0, Var_42, 1) = ((MR_Box) (libs__file_util__write_include_file_contents_5_p_0_1));
          MR_hl_field(0, Var_42, 2) = ((MR_Box) ((MR_Integer) 2));
          MR_hl_field(0, Var_42, 3) = ((MR_Box) (InputStream_39));
          MR_hl_field(0, Var_42, 4) = ((MR_Box) (OutputStream_6));
        }
        mercury__exception__try_io_4_p_0((MR_Word) (&mercury__io__io__type_ctor_info_res_0), Var_42, &TryResult_40);
        mercury__io__close_input_3_p_0(InputStream_39);
        if (((MR_tag((MR_Word) TryResult_40)) == (MR_Integer) 2))
          {
            mercury__exception__rethrow_1_p_0((MR_Word) (&mercury__io__io__type_ctor_info_res_0), TryResult_40);
            return;
          }
        else
        {
          MR_Word Var_47 = ((MR_Word) ((MR_hl_field(1, TryResult_40, (MR_Integer) 0))));

          if ((Var_47 == (MR_Word) ((MR_Unsigned) 0U)))
            CopyRes_13 = (MR_Word) ((MR_Unsigned) 0U);
          else
            CopyRes_13 = Var_47;
        }
      }
      if ((CopyRes_13 == (MR_Word) ((MR_Unsigned) 0U)))
        *Res_8 = (MR_Word) ((MR_Unsigned) 0U);
      else
      {
        MR_Word Error_14 = ((MR_Word) ((MR_hl_field(1, CopyRes_13, (MR_Integer) 0))));
        MR_String Message_15;
        MR_String Var_21;
        MR_String Var_55;
        MR_String Var_56;

        Message_15 = mercury__io__error_message_1_f_0(Error_14);
        Var_55 = mercury__string__f_43_43_2_f_0((MR_String) "\' for input: ", Message_15);
        Var_56 = mercury__string__f_43_43_2_f_0(FileName_7, Var_55);
        Var_21 = mercury__string__f_43_43_2_f_0((MR_String) "can\'t open \140", Var_56);
        {
          MR_Word base;
          base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
          *Res_8 = base;
          MR_hl_field(1, base, 0) = ((MR_Box) (Var_21));
        }
      }
    }
    else
    {
      MR_String Var_22;
      MR_String Var_65;
      MR_String Var_66;

      Var_65 = mercury__string__f_43_43_2_f_0((MR_String) "\' for input: ", (MR_String) "Not a regular file");
      Var_66 = mercury__string__f_43_43_2_f_0(FileName_7, Var_65);
      Var_22 = mercury__string__f_43_43_2_f_0((MR_String) "can\'t open \140", Var_66);
      {
        MR_Word base;
        base = (MR_Word) MR_mkword(1, MR_new_object(MR_Word, (1 * sizeof(MR_Word)), NULL, NULL));
        *Res_8 = base;
        MR_hl_field(1, base, 0) = ((MR_Box) (Var_22));
      }
    }
  }
}

static void MR_CALL 
libs__file_util__output_to_file_stream_7_p_0_2(
  MR_Box closure_arg,
  MR_Box wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box * wrapper_arg_3)
{
  MR_Box closure = closure_arg;

  libs__file_util__report_error_4_p_0(((MR_Word) ((MR_hl_field(0, closure, (MR_Integer) 3)))), ((MR_String) (wrapper_arg_1)));
}

static void MR_CALL 
libs__file_util__output_to_file_stream_7_p_0_1(
  MR_Box closure_arg,
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box * wrapper_arg_3)
{
  MR_Box closure = closure_arg;
  MR_Word conv0_LambdaHeadVar__1_44;

  libs__file_util__IntroducedFrom__pred__output_to_file_stream__164__1_5_p_0(((MR_Word) ((MR_hl_field(0, closure, (MR_Integer) 3)))), ((MR_Word) ((MR_hl_field(0, closure, (MR_Integer) 4)))), &conv0_LambdaHeadVar__1_44);
  *wrapper_arg_1 = ((MR_Box) (conv0_LambdaHeadVar__1_44));
}

void MR_CALL 
libs__file_util__output_to_file_stream_7_p_0(
  MR_Word Globals_8,
  MR_Word ModuleName_9,
  MR_String FileName_10,
  MR_Word Action0_11,
  MR_Word * Succeeded_12)
{
  MR_Word Verbose_14;
  MR_Word Stats_15;
  MR_Word ProgressStream_16;
  MR_String WritingMsg_17;
  MR_Word Res_18;
  MR_String Var_78;

  libs__globals__lookup_bool_option_3_p_0(Globals_8, (MR_Integer) 72, &Verbose_14);
  libs__globals__lookup_bool_option_3_p_0(Globals_8, (MR_Integer) 82, &Stats_15);
  libs__globals__get_progress_output_stream_5_p_0(Globals_8, ModuleName_9, &ProgressStream_16);
  Var_78 = mercury__string__f_43_43_2_f_0(FileName_10, (MR_String) "\'...\n");
  WritingMsg_17 = mercury__string__f_43_43_2_f_0((MR_String) "% Writing to file \140", Var_78);
  switch (Verbose_14) {
    default: /*NOTREACHED*/ MR_assert(0);
    case (MR_Integer) 0:
      {
      }
      break;
    case (MR_Integer) 1:
      {
        mercury__io__write_string_4_p_0(ProgressStream_16, WritingMsg_17);
        mercury__io__flush_output_3_p_0(ProgressStream_16);
      }
      break;
  }
  mercury__io__open_output_4_p_0(FileName_10, &Res_18);
  if (((MR_tag((MR_Word) Res_18)) == (MR_Integer) 1))
  {
    MR_String ErrorMessage_31;
    MR_Word ErrorStream_69;
    MR_String Var_81;

    switch (Verbose_14) {
      default: /*NOTREACHED*/ MR_assert(0);
      case (MR_Integer) 0:
        {
        }
        break;
      case (MR_Integer) 1:
        mercury__io__write_string_4_p_0(ProgressStream_16, (MR_String) "\n");
        break;
    }
    libs__globals__get_error_output_stream_5_p_0(Globals_8, ModuleName_9, &ErrorStream_69);
    Var_81 = mercury__string__f_43_43_2_f_0(FileName_10, (MR_String) "\' for output.");
    ErrorMessage_31 = mercury__string__f_43_43_2_f_0((MR_String) "can\'t open file \140", Var_81);
    libs__file_util__report_error_4_p_0(ErrorStream_69, ErrorMessage_31);
    *Succeeded_12 = (MR_Integer) 0;
  }
  else
  {
    MR_Word FileStream_19 = ((MR_Word) ((MR_hl_field(0, Res_18, (MR_Integer) 0))));
    MR_Word Action_20;
    MR_Word TryResult_24;

    {
      Action_20 = (MR_Word) MR_new_object(MR_Word, (5 * sizeof(MR_Word)), NULL, NULL);
      MR_hl_field(0, Action_20, 0) = ((MR_Box) (&libs__file_util_scalar_common_2[0]));
      MR_hl_field(0, Action_20, 1) = ((MR_Box) (libs__file_util__output_to_file_stream_7_p_0_1));
      MR_hl_field(0, Action_20, 2) = ((MR_Box) ((MR_Integer) 2));
      MR_hl_field(0, Action_20, 3) = ((MR_Box) (Action0_11));
      MR_hl_field(0, Action_20, 4) = ((MR_Box) (FileStream_19));
    }
    mercury__exception__try_io_4_p_0((MR_Word) (&libs__file_util_scalar_common_1[0]), Action_20, &TryResult_24);
    mercury__io__close_output_3_p_0(FileStream_19);
    switch (Verbose_14) {
      default: /*NOTREACHED*/ MR_assert(0);
      case (MR_Integer) 0:
        switch (Stats_15) {
          default: /*NOTREACHED*/ MR_assert(0);
          case (MR_Integer) 0:
            {
            }
            break;
          case (MR_Integer) 1:
            mercury__benchmarking__report_standard_stats_3_p_0(ProgressStream_16);
            break;
        }
        break;
      case (MR_Integer) 1:
        {
          mercury__io__write_string_4_p_0(ProgressStream_16, (MR_String) "% done.\n");
          switch (Stats_15) {
            default: /*NOTREACHED*/ MR_assert(0);
            case (MR_Integer) 0:
              {
              }
              break;
            case (MR_Integer) 1:
              mercury__benchmarking__report_standard_stats_3_p_0(ProgressStream_16);
              break;
          }
          mercury__io__flush_output_3_p_0(ProgressStream_16);
        }
        break;
    }
    if (((MR_tag((MR_Word) TryResult_24)) == (MR_Integer) 2))
      {
        mercury__exception__rethrow_1_p_0((MR_Word) (&libs__file_util_scalar_common_1[0]), TryResult_24);
        return;
      }
    else
    {
      MR_Word Errors_25 = ((MR_Word) ((MR_hl_field(1, TryResult_24, (MR_Integer) 0))));

      if ((Errors_25 == (MR_Word) ((MR_Unsigned) 0U)))
        *Succeeded_12 = (MR_Integer) 1;
      else
      {
        MR_Word ErrorStream_28;
        MR_Word Var_56;
        MR_Box conv1_STATE_VARIABLE_IO_33;

        libs__file_util__maybe_write_string_5_p_0(ProgressStream_16, Verbose_14, (MR_String) "\n");
        libs__globals__get_error_output_stream_5_p_0(Globals_8, ModuleName_9, &ErrorStream_28);
        {
          Var_56 = (MR_Word) MR_new_object(MR_Word, (4 * sizeof(MR_Word)), NULL, NULL);
          MR_hl_field(0, Var_56, 0) = ((MR_Box) (&libs__file_util_scalar_common_3[0]));
          MR_hl_field(0, Var_56, 1) = ((MR_Box) (libs__file_util__output_to_file_stream_7_p_0_2));
          MR_hl_field(0, Var_56, 2) = ((MR_Box) ((MR_Integer) 1));
          MR_hl_field(0, Var_56, 3) = ((MR_Box) (ErrorStream_28));
        }
        mercury__list__foldl_4_p_2((MR_Word) (&mercury__builtin__builtin__type_ctor_info_string_0), (MR_Word) (&mercury__io__io__type_ctor_info_state_0), Var_56, Errors_25, ((MR_Box) ((MR_Integer) 0)), &conv1_STATE_VARIABLE_IO_33);
        *Succeeded_12 = (MR_Integer) 0;
      }
    }
  }
}

void MR_CALL 
libs__file_util__report_error_4_p_0(
  MR_Word Stream_5,
  MR_String ErrorMessage_6)
{
  mercury__io__write_string_4_p_0(Stream_5, (MR_String) "Error: ");
  mercury__io__write_string_4_p_0(Stream_5, ErrorMessage_6);
  mercury__io__write_string_4_p_0(Stream_5, (MR_String) "\n");
  mercury__io__flush_output_3_p_0(Stream_5);
  mercury__io__set_exit_status_3_p_0((MR_Integer) 1);
}

void MR_CALL 
libs__file_util__maybe_write_string_5_p_0(
  MR_Word HeadVar__1_1,
  MR_Word HeadVar__2_2,
  MR_String String_3)
{
  switch (HeadVar__2_2) {
    default: /*NOTREACHED*/ MR_assert(0);
    case (MR_Integer) 0:
      {
      }
      break;
    case (MR_Integer) 1:
      mercury__io__write_string_4_p_0(HeadVar__1_1, String_3);
      break;
  }
}

static MR_bool MR_CALL 
libs__file_util____Unify____dir_name_0_0_10001(
  MR_Box wrapper_arg_1,
  MR_Box wrapper_arg_2)
{
  MR_bool succeeded;

  succeeded = libs__file_util____Unify____dir_name_0_0(((MR_String) (wrapper_arg_1)), ((MR_String) (wrapper_arg_2)));
  return succeeded;
}

static void MR_CALL 
libs__file_util____Compare____dir_name_0_0_10001(
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box wrapper_arg_3)
{
  MR_Word conv0_HeadVar__1_1;

  libs__file_util____Compare____dir_name_0_0(&conv0_HeadVar__1_1, ((MR_String) (wrapper_arg_2)), ((MR_String) (wrapper_arg_3)));
  *wrapper_arg_1 = ((MR_Box) (conv0_HeadVar__1_1));
}

static MR_bool MR_CALL 
libs__file_util____Unify____file_name_0_0_10001(
  MR_Box wrapper_arg_1,
  MR_Box wrapper_arg_2)
{
  MR_bool succeeded;

  succeeded = libs__file_util____Unify____file_name_0_0(((MR_String) (wrapper_arg_1)), ((MR_String) (wrapper_arg_2)));
  return succeeded;
}

static void MR_CALL 
libs__file_util____Compare____file_name_0_0_10001(
  MR_Box * wrapper_arg_1,
  MR_Box wrapper_arg_2,
  MR_Box wrapper_arg_3)
{
  MR_Word conv0_HeadVar__1_1;

  libs__file_util____Compare____file_name_0_0(&conv0_HeadVar__1_1, ((MR_String) (wrapper_arg_2)), ((MR_String) (wrapper_arg_3)));
  *wrapper_arg_1 = ((MR_Box) (conv0_HeadVar__1_1));
}

void mercury__libs__file_util__init(void)
{
}

void mercury__libs__file_util__init_type_tables(void)
{
	static MR_bool initialised = MR_FALSE;
	if (initialised) return;
	initialised = MR_TRUE;

	MR_register_type_ctor_info(&libs__file_util__libs__file_util__type_ctor_info_dir_name_0);
	MR_register_type_ctor_info(&libs__file_util__libs__file_util__type_ctor_info_file_name_0);
}

void mercury__libs__file_util__init_debugger(void)
{
	MR_fatal_error("debugger initialization in MLDS grade");
}

// Ensure everything is compiled with the same grade.
const char *mercury__libs__file_util__grade_check(void)
{
    return &MR_GRADE_VAR;
}

// :- end_module libs.file_util.
