/* Generated by F* Custard extraction. Do not edit. */
#include "ListAppend.h"

static inline Prims_list__uint32 cherry_new_Prims_list__uint32_Cons(uint32_t a0, Prims_list__uint32 a1);
static inline Prims_list__uint32 cherry_reuse_Prims_list__uint32_Cons(cherry_obj *w, size_t cap, uint32_t a0, Prims_list__uint32 a1);
static inline void cherry_free_Prims_list__uint32(cherry_obj *x, int m);
static inline void cherry_markf_Prims_list__uint32(cherry_obj *x, int m);
static inline void cherry_dec_Prims_list__uint32(Prims_list__uint32 x);
static inline void cherry_mark_Prims_list__uint32(Prims_list__uint32 x, int m);

static inline Prims_list__uint32 cherry_new_Prims_list__uint32_Cons(uint32_t a0, Prims_list__uint32 a1) {
  struct Prims_list__uint32_Cons_s *c = (struct Prims_list__uint32_Cons_s *)cherry_alloc(sizeof(struct Prims_list__uint32_Cons_s), PRIMS_CONS__UINT32);
  c->hd = a0;
  c->tl = a1;
  return (Prims_list__uint32)c;
}

static inline Prims_list__uint32 cherry_reuse_Prims_list__uint32_Cons(cherry_obj *w, size_t cap, uint32_t a0, Prims_list__uint32 a1) {
  struct Prims_list__uint32_Cons_s *c;
  if (w != NULL && sizeof(struct Prims_list__uint32_Cons_s) <= cap) {
    c = (struct Prims_list__uint32_Cons_s *)w;
    ((cherry_obj *)c)->tag = PRIMS_CONS__UINT32;
  } else {
    free(w);
    c = (struct Prims_list__uint32_Cons_s *)cherry_alloc(sizeof(struct Prims_list__uint32_Cons_s), PRIMS_CONS__UINT32);
  }
  c->hd = a0;
  c->tl = a1;
  return (Prims_list__uint32)c;
}

static inline void cherry_free_Prims_list__uint32(cherry_obj *x, int m) {
  (void)m;
  for (;;) {
    Prims_list__uint32 next = CHERRY_SCALAR(0);
    switch (x->tag) {
      case PRIMS_CONS__UINT32: {
        struct Prims_list__uint32_Cons_s *c = (struct Prims_list__uint32_Cons_s *)x;
        next = c->tl;
        break;
      }
      default: break;
    }
    free(x);
    if (!cherry_dec_ref(next)) return;
    x = next;
  }
}

static inline void cherry_markf_Prims_list__uint32(cherry_obj *x, int m) {
  for (;;) {
    Prims_list__uint32 next = CHERRY_SCALAR(0);
    switch (x->tag) {
      case PRIMS_CONS__UINT32: {
        struct Prims_list__uint32_Cons_s *c = (struct Prims_list__uint32_Cons_s *)x;
        next = c->tl;
        break;
      }
      default: break;
    }
    if (!cherry_mark_ref(next, m)) return;
    x = next;
  }
}

static inline void cherry_dec_Prims_list__uint32(Prims_list__uint32 x) {
  if (cherry_dec_ref(x)) cherry_defer(x, cherry_free_Prims_list__uint32, 0);
}

static inline void cherry_mark_Prims_list__uint32(Prims_list__uint32 x, int m) {
  if (cherry_mark_ref(x, m)) cherry_defer(x, cherry_markf_Prims_list__uint32, m);
}

static Prims_list__uint32 ListAppend_list_append__t(Prims_list__uint32 x, Prims_list__uint32 y);

static Prims_list__uint32 ListAppend_list_append__t(Prims_list__uint32 x, Prims_list__uint32 y) {
  (void)y;
  Prims_list__uint32 custard_result = NULL;
  Prims_list__uint32 *custard_dst = &custard_result;
custard_tail:;
  if (cherry_tag(x) == PRIMS_NIL__UINT32) {
    cherry_inc(y);
    *custard_dst = y;
    return custard_result;
  }
  else {
    CHERRY_ASSUME(cherry_tag(x) == PRIMS_CONS__UINT32, "incomplete match in ListAppend.list_append@t");
    uint32_t hd = ((struct Prims_list__uint32_Cons_s *)x)->hd;
    Prims_list__uint32 tl = ((struct Prims_list__uint32_Cons_s *)x)->tl;
    Prims_list__uint32 cherry_tmp2;
    if (cherry_is_unique(x)) {
      cherry_tmp2 = x;
    } else {
      cherry_inc(((struct Prims_list__uint32_Cons_s *)x)->tl);
      cherry_dec_Prims_list__uint32(x);
      cherry_tmp2 = NULL;
    }
    Prims_list__uint32 custard_trmc_v = cherry_reuse_Prims_list__uint32_Cons(cherry_tmp2, sizeof(struct Prims_list__uint32_Cons_s), hd, NULL);
    *custard_dst = custard_trmc_v;
    custard_dst = &((struct Prims_list__uint32_Cons_s *)custard_trmc_v)->tl;
    {
      Prims_list__uint32 _ctail1 = tl;
      x = _ctail1;
      goto custard_tail;
    }
  }
}

Prims_list__uint32 ListAppend_append_u32(Prims_list__uint32 x, Prims_list__uint32 y) {
  CHERRY_MUSTTAIL return ListAppend_list_append__t(x, y);
}


