/*
**
** The C code is generated by ATS/Anairiats
** The compilation time is: 2020-11-4: 18h:10m
**
*/

/* include some .h files */
#ifndef _ATS_HEADER_NONE
#include "ats_config.h"
#include "ats_basics.h"
#include "ats_types.h"
#include "ats_exception.h"
#include "ats_memory.h"
#endif /* _ATS_HEADER_NONE */

/* include some .cats files */
#ifndef _ATS_PRELUDE_NONE
#include "prelude/CATS/basics.cats"
#include "prelude/CATS/bool.cats"
#include "prelude/CATS/char.cats"
#include "prelude/CATS/byte.cats"
#include "prelude/CATS/float.cats"
#include "prelude/CATS/integer.cats"
#include "prelude/CATS/integer_ptr.cats"
#include "prelude/CATS/integer_fixed.cats"
#include "prelude/CATS/sizetype.cats"
#include "prelude/CATS/pointer.cats"
#include "prelude/CATS/reference.cats"
#include "prelude/CATS/string.cats"
#include "prelude/CATS/lazy.cats"
#include "prelude/CATS/lazy_vt.cats"
#include "prelude/CATS/printf.cats"
#include "prelude/CATS/list.cats"
#include "prelude/CATS/option.cats"
#include "prelude/CATS/array.cats"
#include "prelude/CATS/matrix.cats"
#endif /* _ATS_PRELUDE_NONE */
/* prologues from statically loaded files */

#include "libc/CATS/errno.cats"

#include "libc/CATS/stdio.cats"

#include "libc/sys/CATS/types.cats"

#include "libc/CATS/stdlib.cats"

#include "libc/CATS/fcntl.cats"

#include "libc/sys/CATS/types.cats"

#include "libc/CATS/fcntl.cats"

#include "libc/sys/CATS/types.cats"
/* external codes at top */
/* type definitions */
/* external typedefs */
/* external dynamic constructor declarations */
/* external dynamic constant declarations */
ATSextern_fun(ats_int_type, atspre_add_int_int) (ats_int_type, ats_int_type) ;
ATSextern_fun(ats_bool_type, atspre_gt_int_int) (ats_int_type, ats_int_type) ;
ATSextern_fun(ats_bool_type, atspre_eq_int_int) (ats_int_type, ats_int_type) ;
ATSextern_fun(ats_int_type, atspre_iadd) (ats_int_type, ats_int_type) ;
ATSextern_fun(ats_bool_type, atspre_igte) (ats_int_type, ats_int_type) ;
ATSextern_fun(ats_size_type, atspre_size1_of_int1) (ats_int_type) ;
ATSextern_fun(ats_size_type, atspre_sub_size1_size1) (ats_size_type, ats_size_type) ;
ATSextern_fun(ats_bool_type, atspre_lt_size1_size1) (ats_size_type, ats_size_type) ;
ATSextern_fun(ats_bool_type, atspre_gt_size1_int1) (ats_size_type, ats_int_type) ;
ATSextern_fun(ats_ssize_type, atspre_ssize1_of_int1) (ats_int_type) ;
ATSextern_fun(ats_bool_type, atspre_lt_ssize1_int1) (ats_ssize_type, ats_int_type) ;
ATSextern_fun(ats_bool_type, atspre_gt_ssize1_int1) (ats_ssize_type, ats_int_type) ;
ATSextern_fun(ats_bool_type, atspre_gte_ssize1_int1) (ats_ssize_type, ats_int_type) ;
ATSextern_fun(ats_void_type, atspre_prerrf_exn) (ats_ptr_type, ...) ;
ATSextern_fun(ats_int_type, atslib_errno_get) () ;
ATSextern_fun(ats_bool_type, atslib_eq_errno_errno) (ats_int_type, ats_int_type) ;
ATSextern_fun(ats_void_type, atslib_perror) (ats_ptr_type) ;
ATSextern_fun(ats_int_type, atslib_close_err) (ats_int_type) ;
ATSextern_fun(ats_int_type, ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_err) (ats_int_type) ;
ATSextern_fun(ats_ssize_type, atslib_fildes_read_err) (ats_int_type, ats_ref_type, ats_size_type) ;
ATSextern_fun(ats_ssize_type, atslib_fildes_read_all_err) (ats_int_type, ats_ref_type, ats_size_type) ;
ATSextern_fun(ats_ssize_type, atslib_fildes_write_err) (ats_int_type, ats_ref_type, ats_size_type) ;
ATSextern_fun(ats_ssize_type, atslib_fildes_write_all_err) (ats_int_type, ats_ref_type, ats_size_type) ;

/* external dynamic terminating constant declarations */
#ifdef _ATS_PROOFCHECK
#endif /* _ATS_PROOFCHECK */

/* assuming abstract types */
/* sum constructor declarations */
/* exn constructor declarations */
/* global dynamic (non-functional) constant declarations */
/* internal function declarations */
static
ats_int_type loop_1 (ats_int_type arg0) ;
static
ats_size_type loop_4 (ats_int_type arg0, ats_ptr_type arg1, ats_size_type arg2, ats_ref_type arg3) ;
static
ats_size_type loop_7 (ats_int_type arg0, ats_ptr_type arg1, ats_size_type arg2, ats_ref_type arg3) ;

/* partial value template declarations */
/* static temporary variable declarations */
/* external value variable declarations */

/* function implementations */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 2040(line=65, offs=7) -- 2395(line=75, offs=6)
*/
ATSstaticdec()
ats_int_type
loop_1 (ats_int_type arg0) {
/* local vardec */
ATSlocal (ats_int_type, tmp1) ;
ATSlocal (ats_int_type, tmp2) ;
ATSlocal (ats_int_type, tmp3) ;
ATSlocal (ats_bool_type, tmp4) ;
ATSlocal (ats_bool_type, tmp5) ;
ATSlocal (ats_int_type, tmp6) ;

__ats_lab_loop_1:
tmp2 = atslib_close_err (arg0) ;
tmp3 = ats_selsin_mac(tmp2, atslab_1) ;
tmp4 = atspre_igte (tmp3, 0) ;
if (tmp4) {
tmp1 = tmp3 ;
} else {
tmp6 = atslib_errno_get () ;
tmp5 = atslib_eq_errno_errno (tmp6, EINTR) ;
if (tmp5) {
arg0 = arg0 ;
goto __ats_lab_loop_1 ; // tail call
} else {
tmp1 = tmp3 ;
} /* end of [if] */
} /* end of [if] */
return (tmp1) ;
} /* end of [loop_1] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 1976(line=63, offs=8) -- 2417(line=76, offs=2)
*/
ATSglobaldec()
ats_int_type
ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_err (ats_int_type arg0) {
/* local vardec */
ATSlocal (ats_int_type, tmp0) ;

__ats_lab_ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_err:
tmp0 = loop_1 (arg0) ;
return (tmp0) ;
} /* end of [ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_err] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 2471(line=79, offs=16) -- 2895(line=93, offs=4)
*/
ATSglobaldec()
ats_void_type
ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_exn (ats_int_type arg0) {
/* local vardec */
// ATSlocal_void (tmp7) ;
ATSlocal (ats_int_type, tmp8) ;
ATSlocal (ats_int_type, tmp9) ;
ATSlocal (ats_bool_type, tmp10) ;
// ATSlocal_void (tmp11) ;
// ATSlocal_void (tmp12) ;

__ats_lab_ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_exn:
tmp8 = ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_err (arg0) ;
tmp9 = ats_selsin_mac(tmp8, atslab_1) ;
tmp10 = atspre_igte (tmp9, 0) ;
if (tmp10) {
/* empty */
} else {
/* tmp11 = */ atslib_perror (ATSstrcst("close")) ;
/* tmp12 = */ atspre_prerrf_exn (ATSstrcst("exit(ATS): [close_loop] failed\n")) ;
/* tmp7 = */ ats_exit (EXIT_FAILURE) ;
} /* end of [if] */
return /* (tmp7) */ ;
} /* end of [ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__close_loop_exn] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 3271(line=108, offs=7) -- 4265(line=134, offs=8)
*/
ATSstaticdec()
ats_size_type
loop_4 (ats_int_type arg0, ats_ptr_type arg1, ats_size_type arg2, ats_ref_type arg3) {
/* local vardec */
ATSlocal (ats_size_type, tmp14) ;
ATSlocal (ats_bool_type, tmp15) ;
ATSlocal (ats_ssize_type, tmp16) ;
ATSlocal (ats_bool_type, tmp17) ;
ATSlocal (ats_ptr_type, tmp18) ;
ATSlocal (ats_size_type, tmp19) ;
ATSlocal (ats_bool_type, tmp20) ;
ATSlocal (ats_bool_type, tmp21) ;
ATSlocal (ats_int_type, tmp22) ;
ATSlocal (ats_bool_type, tmp24) ;
ATSlocal (ats_int_type, tmp25) ;

__ats_lab_loop_4:
tmp15 = atspre_gt_size1_int1 (arg2, 0) ;
if (tmp15) {
tmp16 = atslib_fildes_read_err (arg0, arg1, arg2) ;
tmp17 = atspre_gt_ssize1_int1 (tmp16, 0) ;
if (tmp17) {
tmp18 = atspre_padd_size (arg1, ats_castfn_mac(ats_size_type, tmp16)) ;
tmp19 = atspre_sub_size1_size1 (arg2, ats_castfn_mac(ats_size_type, tmp16)) ;
arg0 = arg0 ;
arg1 = tmp18 ;
arg2 = tmp19 ;
arg3 = arg3 ;
goto __ats_lab_loop_4 ; // tail call
} else {
tmp21 = atspre_lt_ssize1_int1 (tmp16, 0) ;
if (tmp21) {
tmp22 = atslib_errno_get () ;
tmp20 = atslib_eq_errno_errno (tmp22, EINTR) ;
} else {
tmp20 = ats_false_bool ;
} /* end of [if] */
if (tmp20) {
arg0 = arg0 ;
arg1 = arg1 ;
arg2 = arg2 ;
arg3 = arg3 ;
goto __ats_lab_loop_4 ; // tail call
} else {
tmp24 = atspre_lt_ssize1_int1 (tmp16, 0) ;
if (tmp24) {
tmp25 = atspre_add_int_int (ats_ptrget_mac(ats_int_type, arg3), 1) ;
ats_ptrget_mac(ats_int_type, arg3) = tmp25 ;
} else {
/* empty */
} /* end of [if] */
tmp14 = arg2 ;
} /* end of [if] */
} /* end of [if] */
} else {
tmp14 = atspre_size1_of_int1 (0) ;
} /* end of [if] */
return (tmp14) ;
} /* end of [loop_4] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 3233(line=107, offs=3) -- 4445(line=140, offs=4)
*/
ATSglobaldec()
ats_ssize_type
atslib_fildes_read_all_err (ats_int_type arg0, ats_ref_type arg1, ats_size_type arg2) {
/* local vardec */
ATSlocal (ats_ssize_type, tmp13) ;
ATSlocal (ats_int_type, tmp26) ;
ATSlocal (ats_size_type, tmp27) ;
ATSlocal (ats_bool_type, tmp28) ;
ATSlocal (ats_size_type, tmp29) ;

__ats_lab_atslib_fildes_read_all_err:
/* ats_int_type tmp26 ; */
tmp26 = 0 ;
tmp27 = loop_4 (arg0, arg1, arg2, (&tmp26)) ;
tmp28 = atspre_eq_int_int (tmp26, 0) ;
if (tmp28) {
tmp29 = atspre_sub_size1_size1 (arg2, tmp27) ;
tmp13 = ats_castfn_mac(ats_ssize_type, tmp29) ;
} else {
tmp13 = atspre_ssize1_of_int1 (-1) ;
} /* end of [if] */
return (tmp13) ;
} /* end of [atslib_fildes_read_all_err] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 4497(line=144, offs=3) -- 4756(line=153, offs=4)
*/
ATSglobaldec()
ats_size_type
atslib_fildes_read_all_exn (ats_int_type arg0, ats_ref_type arg1, ats_size_type arg2) {
/* local vardec */
ATSlocal (ats_size_type, tmp30) ;
ATSlocal (ats_ssize_type, tmp31) ;
ATSlocal (ats_bool_type, tmp32) ;
// ATSlocal_void (tmp33) ;

__ats_lab_atslib_fildes_read_all_exn:
tmp31 = atslib_fildes_read_all_err (arg0, arg1, arg2) ;
tmp32 = atspre_gte_ssize1_int1 (tmp31, 0) ;
if (tmp32) {
tmp30 = ats_castfn_mac(ats_size_type, tmp31) ;
} else {
/* tmp33 = */ atslib_perror (ATSstrcst("read")) ;
/* tmp30 = */ ats_exit_errmsg (EXIT_FAILURE, ATSstrcst("exit(ATS): [read_all] failed\n")) ;
} /* end of [if] */
return (tmp30) ;
} /* end of [atslib_fildes_read_all_exn] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 4882(line=161, offs=7) -- 5790(line=185, offs=8)
*/
ATSstaticdec()
ats_size_type
loop_7 (ats_int_type arg0, ats_ptr_type arg1, ats_size_type arg2, ats_ref_type arg3) {
/* local vardec */
ATSlocal (ats_size_type, tmp35) ;
ATSlocal (ats_bool_type, tmp36) ;
ATSlocal (ats_ssize_type, tmp37) ;
ATSlocal (ats_bool_type, tmp38) ;
ATSlocal (ats_ptr_type, tmp39) ;
ATSlocal (ats_size_type, tmp40) ;
ATSlocal (ats_bool_type, tmp41) ;
ATSlocal (ats_bool_type, tmp42) ;
ATSlocal (ats_int_type, tmp43) ;
ATSlocal (ats_int_type, tmp44) ;

__ats_lab_loop_7:
tmp36 = atspre_gt_size1_int1 (arg2, 0) ;
if (tmp36) {
tmp37 = atslib_fildes_write_err (arg0, arg1, arg2) ;
tmp38 = atspre_gt_ssize1_int1 (tmp37, 0) ;
if (tmp38) {
tmp39 = atspre_padd_size (arg1, ats_castfn_mac(ats_size_type, tmp37)) ;
tmp40 = atspre_sub_size1_size1 (arg2, ats_castfn_mac(ats_size_type, tmp37)) ;
arg0 = arg0 ;
arg1 = tmp39 ;
arg2 = tmp40 ;
arg3 = arg3 ;
goto __ats_lab_loop_7 ; // tail call
} else {
tmp42 = atspre_lt_ssize1_int1 (tmp37, 0) ;
if (tmp42) {
tmp43 = atslib_errno_get () ;
tmp41 = atslib_eq_errno_errno (tmp43, EINTR) ;
} else {
tmp41 = ats_true_bool ;
} /* end of [if] */
if (tmp41) {
arg0 = arg0 ;
arg1 = arg1 ;
arg2 = arg2 ;
arg3 = arg3 ;
goto __ats_lab_loop_7 ; // tail call
} else {
tmp44 = atspre_add_int_int (ats_ptrget_mac(ats_int_type, arg3), 1) ;
ats_ptrget_mac(ats_int_type, arg3) = tmp44 ;
tmp35 = arg2 ;
} /* end of [if] */
} /* end of [if] */
} else {
tmp35 = atspre_size1_of_int1 (0) ;
} /* end of [if] */
return (tmp35) ;
} /* end of [loop_7] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 4844(line=160, offs=3) -- 5970(line=191, offs=4)
*/
ATSglobaldec()
ats_ssize_type
atslib_fildes_write_all_err (ats_int_type arg0, ats_ref_type arg1, ats_size_type arg2) {
/* local vardec */
ATSlocal (ats_ssize_type, tmp34) ;
ATSlocal (ats_int_type, tmp45) ;
ATSlocal (ats_size_type, tmp46) ;
ATSlocal (ats_bool_type, tmp47) ;
ATSlocal (ats_size_type, tmp48) ;

__ats_lab_atslib_fildes_write_all_err:
/* ats_int_type tmp45 ; */
tmp45 = 0 ;
tmp46 = loop_7 (arg0, arg1, arg2, (&tmp45)) ;
tmp47 = atspre_eq_int_int (tmp45, 0) ;
if (tmp47) {
tmp48 = atspre_sub_size1_size1 (arg2, tmp46) ;
tmp34 = ats_castfn_mac(ats_ssize_type, tmp48) ;
} else {
tmp34 = atspre_ssize1_of_int1 (-1) ;
} /* end of [if] */
return (tmp34) ;
} /* end of [atslib_fildes_write_all_err] */

/*
// /nix/store/rfja8ixrs5wb4kj3kiwlch1apnh7qgvy-ats-0.2.12/lib/ats-anairiats-0.2.12/libc/DATS/fcntl.dats: 6045(line=197, offs=3) -- 6455(line=209, offs=4)
*/
ATSglobaldec()
ats_void_type
atslib_fildes_write_all_exn (ats_int_type arg0, ats_ref_type arg1, ats_size_type arg2) {
/* local vardec */
// ATSlocal_void (tmp49) ;
ATSlocal (ats_int_type, tmp50) ;
ATSlocal (ats_ssize_type, tmp51) ;
ATSlocal (ats_bool_type, tmp53) ;
ATSlocal (ats_bool_type, tmp54) ;
ATSlocal (ats_int_type, tmp55) ;
ATSlocal (ats_int_type, tmp56) ;
ATSlocal (ats_bool_type, tmp57) ;
// ATSlocal_void (tmp58) ;

__ats_lab_atslib_fildes_write_all_exn:
/* ats_int_type tmp50 ; */
tmp50 = 0 ;
tmp51 = atslib_fildes_write_all_err (arg0, arg1, arg2) ;
tmp53 = atspre_gte_ssize1_int1 (tmp51, 0) ;
if (tmp53) {
tmp54 = atspre_lt_size1_size1 (ats_castfn_mac(ats_size_type, tmp51), arg2) ;
if (tmp54) {
tmp55 = atspre_iadd (tmp50, 1) ;
tmp50 = tmp55 ;
} else {
/* empty */
} /* end of [if] */
} else {
tmp56 = atspre_iadd (tmp50, 1) ;
tmp50 = tmp56 ;
} /* end of [if] */
tmp57 = atspre_gt_int_int (tmp50, 0) ;
if (tmp57) {
/* tmp58 = */ atslib_perror (ATSstrcst("write")) ;
/* tmp49 = */ ats_exit_errmsg (EXIT_FAILURE, ATSstrcst("exit(ATS): [write_all]: failed\n")) ;
} else {
/* empty */
} /* end of [if] */
return /* (tmp49) */ ;
} /* end of [atslib_fildes_write_all_exn] */

/* static load function */

extern ats_void_type ATS_2d0_2e2_2e12_2libc_2SATS_2errno_2esats__staload (void) ;
extern ats_void_type ATS_2d0_2e2_2e12_2libc_2SATS_2stdio_2esats__staload (void) ;
extern ats_void_type ATS_2d0_2e2_2e12_2libc_2SATS_2stdlib_2esats__staload (void) ;
extern ats_void_type ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__staload (void) ;

ats_void_type
ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__staload () {
static int ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__staload_flag = 0 ;
if (ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__staload_flag) return ;
ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__staload_flag = 1 ;

ATS_2d0_2e2_2e12_2libc_2SATS_2errno_2esats__staload () ;
ATS_2d0_2e2_2e12_2libc_2SATS_2stdio_2esats__staload () ;
ATS_2d0_2e2_2e12_2libc_2SATS_2stdlib_2esats__staload () ;
ATS_2d0_2e2_2e12_2libc_2SATS_2fcntl_2esats__staload () ;

return ;
} /* staload function */

/* dynamic load function */

// dynload flag declaration
// extern ats_int_type ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__dynload_flag ;

ats_void_type
ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__dynload () {
// ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__dynload_flag = 1 ;
ATS_2d0_2e2_2e12_2libc_2DATS_2fcntl_2edats__staload () ;

#ifdef _ATS_PROOFCHECK
#endif /* _ATS_PROOFCHECK */

/* marking static variables for GC */

/* marking external values for GC */

/* code for dynamic loading */
return ;
} /* end of [dynload function] */

/* external codes at mid */
/* external codes at bot */


ats_void_type
atslib_close_exn
  (ats_int_type fd) {
  int err = close(fd) ;
  if (err < 0) {
    perror ("close") ;
    fprintf (stderr, "exit(ATS): [close(%i)] failed\n", (fd)) ;
    exit (EXIT_FAILURE) ;
  } // end of [atslib_close_exn]
  return ;
} // end of [atslib_close_exn]



/* ****** ****** */

/* end of [DATS_fcntl_dats.c] */
