#include <perms.h>

//          Edinburgh IMP77 Compiler - Version 8.4
//
//     1  %begin

int main(int argc, char **argv) { /*%begin*/
  _imp_enter(main);
  _imp_initialise(argc, argv);

  //     2
  //     3    ! i2c correctly requires the parameter of length() to be an lvalue.  However we may at some point
  //     4    ! want to allow rvalues too - but only when length() is itself being used as an rvalue...
  //     5
  //     6    ! To do:
  //     7
  //     8    ! #define UNCONST(x) ((__typeof__(x))(x))
  //     9
  //    10    %const %string (255) const = "string"

  const _imp_string255 CONST = _imp_str_init("string");

  //    11
  //    12    %ownstring (255) string = "string"

  static _imp_string255 STRING = _imp_str_init("string");

  //    13
  //    14    %string(255) %fn fn

  _imp_string255 FN(void) {
    _imp_enter(FN);

    //    15      %result = const

    _imp_result(*(_imp_string255 *)_imp_strp(CONST));

    //    16    %end

    _imp_begin_backtrace(FN);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of FN */

  //    17
  //    18    %string(255) %map map

  _imp_string255 *MAP(void) {
    _imp_enter(MAP);

    //    19      %result == const

    _imp_result(_imp_strp(CONST));

    //    20    %end

    _imp_begin_backtrace(MAP);
    _imp_diagf("\nLocal Variables:\n\n");
    _imp_end_backtrace();
  } /* end of MAP */

  //    21
  //    22    %string(255) s

  _imp_string255 S;

  //    23
  //    24    s = "string"

  _imp_strcpy(&S, _imp_strp(_imp_str_literal("string")), /*max:*/ 255);

  //    25    s = const

  _imp_strcpy(&S, _imp_strp(CONST), /*max:*/ 255);

  //    26    s = fn

  _imp_strcpy(&S, _imp_strp(FN()), /*max:*/ 255);

  //    27    s = map

  _imp_strcpy(&S, _imp_strp(*MAP()), /*max:*/ 255);

  //    28
  //    29    printstring("Fail # map") %and newline %if s # map   { <-- this one fails at the moment }


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(S), (_imp_string *)MAP()) == 0) goto L_0001;

  /* clang-format on */
  _imp_printf("%s", "Fail # map");
  _imp_NEWLINE();
L_0001:;

//    30    printstring("Fail # fn") %and newline %if s # fn


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(S), (_imp_string *)_imp_strp(FN())) == 0) goto L_0002;

  /* clang-format on */
  _imp_printf("%s", "Fail # fn");
  _imp_NEWLINE();
L_0002:;

//    31    printstring("Fail # const") %and newline %if s # const


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(S), (_imp_string *)_imp_strp(CONST)) == 0) goto L_0003;

  /* clang-format on */
  _imp_printf("%s", "Fail # const");
  _imp_NEWLINE();
L_0003:;

//    32    printstring("Fail # string") %and newline %if s # string


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(S), (_imp_string *)_imp_strp(STRING)) == 0) goto L_0004;

  /* clang-format on */
  _imp_printf("%s", "Fail # string");
  _imp_NEWLINE();
L_0004:;

//    33    printstring("Fail # literal") %and newline %if s # "string"


  /* clang-format off */
if (_imp_strcmp((_imp_string *)_imp_strp(S), (_imp_string *)_imp_strp(_imp_str_literal("string"))) == 0) goto L_0005;

  /* clang-format on */
  _imp_printf("%s", "Fail # literal");
  _imp_NEWLINE();
L_0005:;

//    34
//    35    printstring("Fail map") %and newline %if length(map) # 6

  if ((*_imp_LENGTH((_imp_string255 *)&MAP())) == (6)) goto L_0006;
  _imp_printf("%s", "Fail map");
  _imp_NEWLINE();
L_0006:;

//    36  ! printstring("Fail") %and newline %if length(fn) # 6
//    37  ! printstring("Fail") %and newline %if length(const) # 6
//    38    printstring("Fail string") %and newline %if length(string) # 6

  if ((*_imp_LENGTH((_imp_string255 *)&STRING)) == (6)) goto L_0007;
  _imp_printf("%s", "Fail string");
  _imp_NEWLINE();
L_0007:;

//    39  ! printstring("Fail") %and newline %if length("string") # 6
//    40
//    41    length(string) = 5   ; printstring(string); newline

  *_imp_LENGTH((_imp_string255 *)&STRING) = 5;
  _imp_printf("%S", _imp_strp(STRING));
  _imp_NEWLINE();

  //    42    length(map) = 5      ; printstring(map); newline

  *_imp_LENGTH((_imp_string255 *)&MAP()) = 5;
  _imp_printf("%S", _imp_strp(*MAP()));
  _imp_NEWLINE();

  //    43  ! length(fn) = 5       ; printstring(fn); newline
  //    44  ! length(const) = 5    ; printstring(const); newline
  //    45  ! length("string") = 5 ; printstring("""string"""); newline
  //    46
  //    47  %endofprogram

  _imp_stop(0);
end_0:;
  _imp_begin_backtrace(main);
  _imp_diagf("\n\nArguments:\n\n");
  _imp_diagf("ARGC = %d\n", argc);
  _imp_diagf("ARGV @[%p]\n", argv);
  for (int i = 0; i < argc; i++) {
    _imp_diagf("  ARGV(%d) = \"%s\"\n", i, argv[i]);
  }
  if (argv[argc] == 0) {
    _imp_diagf("  ARGV(%d) = NULL\n", argc);
  } else {
    _imp_diagf("  ARGV(%d) = \"%s\"\n", argc, argv[argc]);
  }
  _imp_diagf("\nLocal Variables:\n\n");
  _imp_diagf("CONST = \"%.*s\"\n", CONST.cstr.len, CONST.cstr.s);
  _imp_diagf("STRING = \"%.*s\"\n", STRING.cstr.len, STRING.cstr.s);
  _imp_diagf("S = \"%.*s\"\n", S.cstr.len, S.cstr.s);
  _imp_end_backtrace();
  _imp_stop(0);
}