#include <IO/Buffer.d>
#include <__oo2c.h>
#include <setjmp.h>

static void IO_Buffer__Init(IO_Buffer__Channel ch, IO__ByteChannel base) {
  register OOC_INT64 i0,i1;

  i0 = (OOC_INT64)ch;
  IO__InitByteChannel((IO__ByteChannel)i0);
  i1 = (OOC_INT64)base;
  *(OOC_INT64*)((_check_pointer(i0, 1488))+16) = i1;
  *(OOC_UINT8*)((_check_pointer(i0, 1509))+24) = 1u;
  *(OOC_INT32*)((_check_pointer(i0, 1533))+28) = 0;
  *(OOC_INT32*)((_check_pointer(i0, 1550))+32) = 0;
  return;
  ;
}

IO_Buffer__Channel IO_Buffer__Open(IO__ByteChannel base) {
  register OOC_INT64 i0,i1;

  i0 = (OOC_INT64)RT0__NewObject(_td_IO_Buffer__Channel.baseTypes[0]);
  i1 = (OOC_INT64)base;
  IO_Buffer__Init((IO_Buffer__Channel)i0, (IO__ByteChannel)i1);
  return (IO_Buffer__Channel)i0;
  ;
}

void IO_Buffer__ChannelDesc_Flush(IO_Buffer__Channel ch) {
  register OOC_INT64 i0,i1,i2,i3;
  OOC_INT32 d;

  i0 = (OOC_INT64)ch;
  i1 = *(OOC_INT32*)((_check_pointer(i0, 1812))+28);
  i2 = *(OOC_INT32*)((_check_pointer(i0, 1821))+32);
  i1 = i1!=i2;
  if (!i1) goto l7;
  i1 = *(OOC_UINT8*)((_check_pointer(i0, 1851))+24);
  if (i1) goto l5;
  i1 = *(OOC_INT32*)((_check_pointer(i0, 1943))+28);
  _assert((i1==0), 127, 1934);
  i1 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 1969))+16);
  i2 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 1969))+16);
  i3 = *(OOC_INT32*)((_check_pointer(i0, 1997))+32);
  i1 = OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 1974)))), IO__ByteChannelDesc_Write)),IO__ByteChannelDesc_Write)((IO__ByteChannel)i2, (void*)((_check_pointer(i0, 1983))+36), 8192, 0, i3);
  d = i1;
  i2 = *(OOC_INT32*)((_check_pointer(i0, 2033))+32);
  _assert((i1==i2), 127, 2020);
  *(OOC_INT32*)((_check_pointer(i0, 2058))+32) = 0;
  goto l6;
l5:
  *(OOC_INT32*)((_check_pointer(i0, 1875))+28) = 0;
  *(OOC_INT32*)((_check_pointer(i0, 1896))+32) = 0;
l6:
  i1 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 2096))+16);
  i0 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 2096))+16);
  OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 2101)))), IO__ByteChannelDesc_Flush)),IO__ByteChannelDesc_Flush)((IO__ByteChannel)i0);
l7:
  return;
  ;
}

void IO_Buffer__ChannelDesc_SetPos(IO_Buffer__Channel ch, OOC_INT32 pos) {
  register OOC_INT64 i0,i1,i2;

  i0 = (OOC_INT64)ch;
  IO_Buffer__ChannelDesc_Flush((IO_Buffer__Channel)i0);
  i1 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 2227))+16);
  i0 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 2227))+16);
  i2 = pos;
  OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 2232)))), IO__ChannelDesc_SetPos)),IO__ChannelDesc_SetPos)((IO__Channel)i0, i2);
  return;
  ;
}

OOC_INT32 IO_Buffer__ChannelDesc_Read(IO_Buffer__Channel ch, OOC_CHAR8 buffer[], OOC_LEN buffer_0d, OOC_INT32 start, OOC_INT32 length) {
  register OOC_INT64 i0,i1,i2,i3;
  OOC_INT32 i;
  OOC_INT32 d;

  i0 = (OOC_INT64)ch;
  i1 = *(OOC_UINT8*)((_check_pointer(i0, 2447))+24);
  if (i1) goto l3;
  IO_Buffer__ChannelDesc_Flush((IO_Buffer__Channel)i0);
  i = 0;
  
  goto l8;
l3:
  i1 = *(OOC_INT32*)((_check_pointer(i0, 2474))+32);
  i2 = *(OOC_INT32*)((_check_pointer(i0, 2489))+28);
  i1 = i1-i2;
  i = i1;
  _assert((i1>=0), 127, 2501);
  i2 = length;
  i3 = i1>=i2;
  if (i3) goto l6;
  i2 = *(OOC_INT32*)((_check_pointer(i0, 2889))+28);
  i3 = start;
  _move_block((((_check_pointer(i0, 2878))+36)+i2),((OOC_INT64)buffer+i3),i1);
  i0 = (OOC_INT64)ch;
  i1 = *(OOC_INT32*)((_check_pointer(i0, 2979))+28);
  i2 = i;
  i3 = start;
  *(OOC_INT32*)((_check_pointer(i0, 2979))+28) = (i1+i2);
  i1 = length;
  start = (i3+i2);
  length = (i1-i2);
  
  goto l8;
l6:
  i1 = *(OOC_INT32*)((_check_pointer(i0, 2641))+28);
  i3 = start;
  _move_block((((_check_pointer(i0, 2630))+36)+i1),((OOC_INT64)buffer+i3),i2);
  i0 = (OOC_INT64)ch;
  i1 = *(OOC_INT32*)((_check_pointer(i0, 2736))+28);
  i2 = length;
  *(OOC_INT32*)((_check_pointer(i0, 2736))+28) = (i1+i2);
  return i2;
  
l8:
  i1 = length;
  i2 = i1!=0;
  if (i2) goto l11;
  i0 = i;
  return i0;
  goto l32;
l11:
  i2 = *(OOC_INT32*)((_check_pointer(i0, 3273))+28);
  i3 = *(OOC_INT32*)((_check_pointer(i0, 3282))+32);
  _assert((i2==i3), 127, 3264);
  i2 = i1>=4096;
  if (i2) goto l22;
  i2 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 3617))+16);
  i3 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 3617))+16);
  i2 = OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i2, 3622)))), IO__ByteChannelDesc_Read)),IO__ByteChannelDesc_Read)((IO__ByteChannel)i3, (void*)((_check_pointer(i0, 3630))+36), 8192, 0, 8192);
  d = i2;
  *(OOC_UINT8*)((_check_pointer(i0, 3665))+24) = 1u;
  *(OOC_INT32*)((_check_pointer(i0, 3693))+28) = 0;
  i3 = i2<0;
  if (i3) goto l16;
  *(OOC_INT32*)((_check_pointer(i0, 3979))+32) = i2;
  i2 = start;
  i0 = IO_Buffer__ChannelDesc_Read((IO_Buffer__Channel)i0, (void*)(OOC_INT64)buffer, (-1), i2, i1);
  i1 = i;
  return (i1+i0);
  goto l32;
l16:
  *(OOC_INT32*)((_check_pointer(i0, 3775))+32) = 0;
  i0 = i;
  i1 = i0>0;
  if (i1) goto l19;
  return (-1);
  goto l32;
l19:
  return i0;
  goto l32;
l22:
  i2 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 3349))+16);
  i0 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 3349))+16);
  i3 = start;
  i0 = OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i2, 3354)))), IO__ByteChannelDesc_Read)),IO__ByteChannelDesc_Read)((IO__ByteChannel)i0, (void*)(OOC_INT64)buffer, (-1), i3, i1);
  i1 = i0<0;
  if (i1) goto l25;
  i1 = i;
  return (i0+i1);
  goto l32;
l25:
  i1 = i;
  i2 = i1>0;
  if (i2) goto l28;
  return i0;
  goto l32;
l28:
  return i1;
l32:
  _failed_function(2285); return 0;
  ;
}

Object__String8 IO_Buffer__ChannelDesc_ReadLineLF(IO_Buffer__Channel ch) {
  register OOC_INT64 i0,i1,i2,i3,i4;
  OOC_INT32 i;
  Object__String8 str8;
  ADT_StringBuffer__StringBuffer sb;
  OOC_INT32 d;
  Object__String str;

  i0 = (OOC_INT64)ch;
  i1 = *(OOC_UINT8*)((_check_pointer(i0, 4769))+24);
  i1 = !i1;
  if (!i1) goto l3;
  IO_Buffer__ChannelDesc_Flush((IO_Buffer__Channel)i0);
l3:
  i1 = *(OOC_INT32*)((_check_pointer(i0, 4822))+28);
  i = i1;
  i2 = *(OOC_INT32*)((_check_pointer(i0, 4845))+32);
  i2 = i1!=i2;
  if (!i2) goto l14;
l6_loop:
  i2 = *(OOC_UINT8*)(((_check_pointer(i0, 4874))+36)+(_check_index(i1, 8192, OOC_UINT32, 4881)));
  i1 = i1+1;
  i2 = i2==10u;
  if (!i2) goto l9;
  i2 = *(OOC_INT32*)((_check_pointer(i0, 4991))+28);
  i2 = (OOC_INT64)Object__NewLatin1Region((void*)((_check_pointer(i0, 4980))+36), 8192, i2, i1);
  str8 = (Object__String8)i2;
  *(OOC_INT32*)((_check_pointer(i0, 5013))+28) = i1;
  return (Object__String8)i2;
l9:
  i = i1;
  i2 = *(OOC_INT32*)((_check_pointer(i0, 4845))+32);
  i2 = i1!=i2;
  if (i2) goto l6_loop;
l14:
  i1 = *(OOC_INT32*)((_check_pointer(i0, 5266))+28);
  i2 = *(OOC_INT32*)((_check_pointer(i0, 5274))+32);
  i1 = (OOC_INT64)ADT_StringBuffer__NewLatin1Region((void*)((_check_pointer(i0, 5255))+36), 8192, i1, i2);
  sb = (ADT_StringBuffer__StringBuffer)i1;
l15_loop:
  i2 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 5311))+16);
  i3 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 5311))+16);
  i2 = OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i2, 5316)))), IO__ByteChannelDesc_Read)),IO__ByteChannelDesc_Read)((IO__ByteChannel)i3, (void*)((_check_pointer(i0, 5324))+36), 8192, 0, 8192);
  d = i2;
  i3 = i2<0;
  if (i3) goto l39;
  i = 0;
  i3 = 0!=i2;
  if (i3) goto l20;
  i3=0u;
  goto l22;
l20:
  i3 = *(OOC_UINT8*)(((_check_pointer(i0, 5711))+36)+(_check_index(0, 8192, OOC_UINT32, 5718)));
  i3 = i3!=10u;
  
l22:
  if (i3) goto l24;
  i3=0;
  goto l34;
l24:
  i3=0;
l25_loop:
  i3 = i3+1;
  i = i3;
  i4 = i3!=i2;
  if (i4) goto l28;
  i4=0u;
  goto l30;
l28:
  i4 = *(OOC_UINT8*)(((_check_pointer(i0, 5711))+36)+(_check_index(i3, 8192, OOC_UINT32, 5718)));
  i4 = i4!=10u;
  
l30:
  if (i4) goto l25_loop;
l34:
  i4 = i3==i2;
  if (!i4) goto l36;
  goto l37;
l36:
  i3 = i3+1;
  OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 5974)))), ADT_StringBuffer__StringBufferDesc_AppendLatin1Region)),ADT_StringBuffer__StringBufferDesc_AppendLatin1Region)((ADT_StringBuffer__StringBuffer)i1, (void*)((_check_pointer(i0, 5996))+36), 8192, 0, i3);
  *(OOC_INT32*)((_check_pointer(i0, 6026))+28) = i3;
  *(OOC_INT32*)((_check_pointer(i0, 6051))+32) = i2;
  goto l45;
l37:
  OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 5852)))), ADT_StringBuffer__StringBufferDesc_AppendLatin1Region)),ADT_StringBuffer__StringBufferDesc_AppendLatin1Region)((ADT_StringBuffer__StringBuffer)i1, (void*)((_check_pointer(i0, 5874))+36), 8192, 0, i2);
  goto l15_loop;
l39:
  i2 = *(OOC_INT32*)((_check_pointer(i1, 5422))+4);
  i2 = i2==0;
  if (!i2) goto l41;
  goto l42;
l41:
  *(OOC_INT32*)((_check_pointer(i0, 5586))+28) = 0;
  *(OOC_INT32*)((_check_pointer(i0, 5609))+32) = 0;
  goto l45;
l42:
  return (Object__String8)(OOC_INT64)0;
  goto l15_loop;
l45:
  i0 = (OOC_INT64)OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 6132)))), ADT_StringBuffer__StringBufferDesc_ToString)),ADT_StringBuffer__StringBufferDesc_ToString)((ADT_StringBuffer__StringBuffer)i1);
  str = (Object__String)i0;
  return (Object__String8)(_type_guard(i0, ((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i0, 6167)))), &_td_Object__String8Desc, 6167));
  ;
}

OOC_INT32 IO_Buffer__ChannelDesc_Write(IO_Buffer__Channel ch, const OOC_CHAR8 buffer[], OOC_LEN buffer_0d, OOC_INT32 start, OOC_INT32 length) {
  register OOC_INT64 i0,i1,i2,i3;

  i0 = (OOC_INT64)ch;
  i1 = *(OOC_UINT8*)((_check_pointer(i0, 6364))+24);
  if (!i1) goto l3;
  IO_Buffer__ChannelDesc_Flush((IO_Buffer__Channel)i0);
  *(OOC_UINT8*)((_check_pointer(i0, 6404))+24) = 0u;
l3:
  i1 = *(OOC_INT32*)((_check_pointer(i0, 6447))+32);
  i2 = length;
  i1 = (i1+i2)<=8192;
  if (i1) goto l10;
  IO_Buffer__ChannelDesc_Flush((IO_Buffer__Channel)i0);
  i1 = i2>=4096;
  if (i1) goto l8;
  i1 = start;
  i0 = IO_Buffer__ChannelDesc_Write((IO_Buffer__Channel)i0, (void*)(OOC_INT64)buffer, (-1), i1, i2);
  return i0;
  goto l11;
l8:
  i1 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 6834))+16);
  i0 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 6834))+16);
  i3 = start;
  i0 = OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 6839)))), IO__ByteChannelDesc_Write)),IO__ByteChannelDesc_Write)((IO__ByteChannel)i0, (void*)(OOC_INT64)buffer, (-1), i3, i2);
  return i0;
  goto l11;
l10:
  i1 = *(OOC_INT32*)((_check_pointer(i0, 6596))+32);
  i3 = start;
  _move_block(((OOC_INT64)buffer+i3),(((_check_pointer(i0, 6585))+36)+i1),i2);
  i0 = (OOC_INT64)ch;
  i1 = *(OOC_INT32*)((_check_pointer(i0, 6631))+32);
  i2 = length;
  *(OOC_INT32*)((_check_pointer(i0, 6631))+32) = (i1+i2);
  return i2;
l11:
  _failed_function(6220); return 0;
  ;
}

void IO_Buffer__ChannelDesc_SetBlocking(IO_Buffer__Channel ch, OOC_CHAR8 block) {
  register OOC_INT64 i0,i1,i2;

  i0 = (OOC_INT64)ch;
  i1 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 7112))+16);
  i0 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 7112))+16);
  i2 = block;
  OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 7117)))), IO__ChannelDesc_SetBlocking)),IO__ChannelDesc_SetBlocking)((IO__Channel)i0, i2);
  return;
  ;
}

OOC_INT32 IO_Buffer__ChannelDesc_FileDescriptor(IO_Buffer__Channel ch) {

  return (-1);
  ;
}

void IO_Buffer__ChannelDesc_Close(IO_Buffer__Channel ch) {
  register OOC_INT64 i0,i1;

  i0 = (OOC_INT64)ch;
  IO_Buffer__ChannelDesc_Flush((IO_Buffer__Channel)i0);
  i1 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 7346))+16);
  i0 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 7346))+16);
  OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 7351)))), IO__ChannelDesc_Close)),IO__ChannelDesc_Close)((IO__Channel)i0);
  return;
  ;
}

void IO_Buffer__ChannelDesc_CloseAndRegister(IO_Buffer__Channel ch) {
  register OOC_INT64 i0,i1;

  i0 = (OOC_INT64)ch;
  IO_Buffer__ChannelDesc_Flush((IO_Buffer__Channel)i0);
  i1 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 7466))+16);
  i0 = (OOC_INT64)*(OOC_INT64*)((_check_pointer(i0, 7466))+16);
  OOC_TBCALL(((OOC_INT64)OOC_TBPROC_ADR(((OOC_INT64)OOC_TYPE_TAG((_check_pointer(i1, 7471)))), IO__ChannelDesc_CloseAndRegister)),IO__ChannelDesc_CloseAndRegister)((IO__Channel)i0);
  return;
  ;
}

void OOC_IO_Buffer_init(void) {

  return;
  ;
}

/* --- */
