
/* This file was automatically generated by pclu.*/

#include "pclu_err.h"
#include "pclu_sys.h"

extern errcode streamOPprimary_input();
extern errcode streamOPprimary_output();
extern errcode streamOPputs();
extern errcode streamOPgetl();
extern errcode stringOPempty();
extern errcode intOPparse();
extern errcode streamOPputl();
extern errcode stringOPconcat();
extern errcode intOPunparse();
extern errcode factorial();
CLUREF STR_Enter_040an_040integer_040_050or_040RETURN_040to_040exit_051_072_040;
CLUREF STR_Illegal_040integer;
CLUREF STR__041_040_075_040;
CLUREF STR_Integer_040must_040be_040positive;
CLUREF STR_Overflow;
static int start_up_own_init = 0;

/**** BEGIN PROCEDURE start_up ****/

errcode start_up()
    {
    errcode err;
    errcode ecode2;
    CLUREF pi;
    CLUREF po;
    CLUREF s;
    CLUREF n;
        if (start_up_own_init == 0) {
        stringOPcons("Enter an integer (or RETURN to exit): ", CLU_1, CLU_38, &STR_Enter_040an_040integer_040_050or_040RETURN_040to_040exit_051_072_040);
        stringOPcons("Illegal integer", CLU_1, CLU_15, &STR_Illegal_040integer);
        stringOPcons("! = ", CLU_1, CLU_4, &STR__041_040_075_040);
        stringOPcons("Integer must be positive", CLU_1, CLU_24, &STR_Integer_040must_040be_040positive);
        stringOPcons("Overflow", CLU_1, CLU_8, &STR_Overflow);
        start_up_own_init = 1;
    }
    enter_proc(3);

  LINE(4);
    {
        {CLUREF T_1_1;
        err = streamOPprimary_input(&T_1_1);
        if (err != ERR_ok) goto ex_0;
        pi.num = T_1_1.num;
        }
        }

  LINE(5);
    {
        {CLUREF T_1_1;
        err = streamOPprimary_output(&T_1_1);
        if (err != ERR_ok) goto ex_0;
        po.num = T_1_1.num;
        }
        }

  LINE(6);
    for (;;) {
        if (true != true) { break; }

  LINE(7);
        {
        err = streamOPputs(po, STR_Enter_040an_040integer_040_050or_040RETURN_040to_040exit_051_072_040);
        if (err != ERR_ok) goto ex_0;
        }

  LINE(8);
        {
            {CLUREF T_2_1;
            err = streamOPgetl(pi, &T_2_1);
            if (err != ERR_ok) goto ex_0;
            s.num = T_2_1.num;
            }
            }

  LINE(9);
        {
        CLUREF T_2_1;
        err = stringOPempty(s, &T_2_1);
        if (err != ERR_ok) goto ex_0;
        if (T_2_1.num == true) {
            goto end_while_1;
            }
            }/* end if */

  LINE(10);
        {
            {CLUREF T_3_1;
            err = intOPparse(s, &T_3_1);
            if (err != ERR_ok) goto ex_1;
            n.num = T_3_1.num;
            }
            }
            goto end_1;
            ex_1:
                if ((err == ERR_bad_format)) {

  LINE(12);
                    {
                    err = streamOPputl(po, STR_Illegal_040integer);
                    if (err != ERR_ok) goto ex_0;
                    }
                }
                else {
                    goto ex_0;
                }
            end_1:;

  LINE(14);
        {
        CLUREF T_3_1;
        CLUREF T_3_2;
        CLUREF T_3_3;
        CLUREF T_3_4;
        CLUREF T_3_5;
        err = intOPunparse(n, &T_3_1);
        if (err != ERR_ok) goto ex_2;
        err = stringOPconcat(T_3_1, STR__041_040_075_040, &T_3_2);
        if (err != ERR_ok) goto ex_2;
        err = factorial(n, &T_3_3);
        if (err != ERR_ok) goto ex_2;
        err = intOPunparse(T_3_3, &T_3_4);
        if (err != ERR_ok) goto ex_2;
        err = stringOPconcat(T_3_2, T_3_4, &T_3_5);
        if (err != ERR_ok) goto ex_2;
        err = streamOPputl(po, T_3_5);
        if (err != ERR_ok) goto ex_2;
        }
            goto end_2;
            ex_2:
                if (errcmp(err, "ERR_negative")) {

  LINE(16);
                    {
                    err = streamOPputl(po, STR_Integer_040must_040be_040positive);
                    if (err != ERR_ok) goto ex_0;
                    }
                }
                else if ((err == ERR_overflow)) {

  LINE(18);
                    {
                    err = streamOPputl(po, STR_Overflow);
                    if (err != ERR_ok) goto ex_0;
                    }
                }
                else {
                    goto ex_0;
                }
            end_2:;
        }
        end_while_1:;
    goto end_0;
    ex_0:
        {
            if (err == ERR_failure) {signal(ERR_failure);}
            elist[0] = _pclu_erstr(err);
            {signal(ERR_failure);}
        }
    end_0: {signal(ERR_ok);}
    }

/**** END PROCEDURE start_up ****/


extern errcode intOPlt();
extern errcode intOPequal();
extern errcode intOPmul();
extern errcode intOPsub();

/**** BEGIN PROCEDURE factorial ****/

errcode factorial(n, ret_1)
CLUREF n;
CLUREF *ret_1;
    {
    errcode err;
    errcode ecode2;
    enter_proc(23);

  LINE(24);
    {
    CLUREF T_1_1;
    T_1_1.num = (n.num < 0)? true : false;
    if (T_1_1.num == true) {
        {
        {signal ("ERR_negative");}}
        }
        }/* end if */

  LINE(25);
    {
    CLUREF T_1_1;
    T_1_1.num = (n.num == 0)? true : false;
    if (T_1_1.num == true) {
        {
        {
        ret_1->num = 1;
        }
        {signal (ERR_ok);}}
        }
        }/* end if */

  LINE(26);
    {
    {
    CLUREF T_2_1;
    CLUREF T_2_2;
    CLUREF T_2_3;
    T_2_1.num = n.num - 1;
     if ((T_2_1.num >= 0 && n.num < 0 && (-1) < 0) || 
         (T_2_1.num <= 0 && n.num > 0 && (-1) > 0)) {
        err = ERR_overflow;
        goto ex_1;}
    err = factorial(T_2_1, &T_2_2);
    if (err != ERR_ok) goto ex_1;
    err = intOPmul(n, T_2_2, &T_2_3);
    if (err != ERR_ok) goto ex_1;
    ret_1->num = T_2_3.num;
    }
    {signal (ERR_ok);}}
    goto end_1;
    ex_1:
        if (err == ERR_overflow) {signal(ERR_overflow);}
        else {
            goto ex_0;}
    end_1:;
    goto end_0;
    ex_0:
        {
            if (err == ERR_failure) {signal(ERR_failure);}
            elist[0] = _pclu_erstr(err);
            {signal(ERR_failure);}
        }
    end_0: elist[0].str = no_return_values_STRING;
        {signal(ERR_failure);}
    }

/**** END PROCEDURE factorial ****/

