sema_own (void * handle)
{
  gboolean D.9515;
  struct _WapiHandle_sem * sem_handle.0;
  unsigned int D.9517;
  unsigned int D.9518;
  struct _WapiHandle_sem * sem_handle;
  gboolean ok;
  static const char __func__[9] = "sema_own";

  try
    {
      ok = _wapi_lookup_handle (handle, 4, &sem_handle);
      if (ok == 0) goto <D.9513>; else goto <D.9514>;
      <D.9513>:
      monoeg_g_log (0B, 16, "%s: error looking up sem handle %p", &__func__, handle);
      D.9515 = 0;
      return D.9515;
      <D.9514>:
      sem_handle.0 = sem_handle;
      D.9517 = sem_handle.0->val;
      D.9518 = D.9517 + 4294967295;
      sem_handle.0->val = D.9518;
      sem_handle.0 = sem_handle;
      D.9517 = sem_handle.0->val;
      if (D.9517 == 0) goto <D.9519>; else goto <D.9520>;
      <D.9519>:
      _wapi_handle_set_signal_state (handle, 0, 0);
      <D.9520>:
      D.9515 = 1;
      return D.9515;
    }
  finally
    {
      sem_handle = {CLOBBER};
    }
}


_wapi_handle_set_signal_state (void * handle, gboolean state, gboolean broadcast)
{
  long int handle.1;
  int iftmp.2;
  <unnamed type> D.9530;
  <unnamed type> D.9532;
  <unnamed type> D.9534;
  <unnamed type> D.9536;
  _Bool D.9537;
  long int D.9538;
  long int D.9539;
  unsigned int D.9542;
  struct _WapiHandleUnshared * D.9543;
  long unsigned int D.9544;
  long unsigned int D.9545;
  long unsigned int D.9546;
  long int D.9549;
  long int D.9550;
  union mono_mutex_t * _wapi_global_signal_mutex.3;
  _Bool D.9556;
  long int D.9557;
  long int D.9558;
  union pthread_cond_t * D.9563;
  union pthread_cond_t * _wapi_global_signal_cond.4;
  guint32 idx;
  struct _WapiHandleUnshared * handle_data;
  int thr_ret;

  handle.1 = (long int) handle;
  idx = (guint32) handle.1;
  if (idx > 4194303) goto <D.9524>; else goto <D.9525>;
  <D.9524>:
  return;
  <D.9525>:
  D.9530 = _wapi_handle_type (handle);
  if (D.9530 == 9) goto <D.9527>; else goto <D.9531>;
  <D.9531>:
  D.9532 = _wapi_handle_type (handle);
  if (D.9532 == 11) goto <D.9527>; else goto <D.9533>;
  <D.9533>:
  D.9534 = _wapi_handle_type (handle);
  if (D.9534 == 12) goto <D.9527>; else goto <D.9535>;
  <D.9535>:
  D.9536 = _wapi_handle_type (handle);
  if (D.9536 == 13) goto <D.9527>; else goto <D.9528>;
  <D.9527>:
  iftmp.2 = 1;
  goto <D.9529>;
  <D.9528>:
  iftmp.2 = 0;
  <D.9529>:
  D.9537 = iftmp.2 != 0;
  D.9538 = (long int) D.9537;
  D.9539 = __builtin_expect (D.9538, 0);
  if (D.9539 != 0) goto <D.9540>; else goto <D.9541>;
  <D.9540>:
  monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "../../mono/io-layer/handles-private.h", 132, "!_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))");
  <D.9541>:
  D.9542 = idx / 256;
  D.9543 = _wapi_private_handles[D.9542];
  D.9544 = (long unsigned int) idx;
  D.9545 = D.9544 & 255;
  D.9546 = D.9545 * 200;
  handle_data = D.9543 + D.9546;
  if (state == 1) goto <D.9547>; else goto <D.9548>;
  <D.9547>:
  {
    struct __pthread_unwind_buf_t __cancel_buf;
    void (*<Tc2>) (void *) __cancel_routine;
    void * __cancel_arg;
    int __not_first_call;

    try
      {
        __cancel_routine = mono_mutex_unlock_in_cleanup;
        __cancel_arg = _wapi_global_signal_mutex;
        __not_first_call = __sigsetjmp (&__cancel_buf.__cancel_jmp_buf, 0);
        D.9549 = (long int) __not_first_call;
        D.9550 = __builtin_expect (D.9549, 0);
        if (D.9550 != 0) goto <D.9551>; else goto <D.9552>;
        <D.9551>:
        __cancel_routine (__cancel_arg);
        __pthread_unwind_next (&__cancel_buf);
        <D.9552>:
        __pthread_register_cancel (&__cancel_buf);
        _wapi_global_signal_mutex.3 = _wapi_global_signal_mutex;
        thr_ret = pthread_mutex_lock (_wapi_global_signal_mutex.3);
        if (thr_ret != 0) goto <D.9554>; else goto <D.9555>;
        <D.9554>:
        monoeg_g_log (0B, 16, "Bad call to mono_mutex_lock result %d for global signal mutex", thr_ret);
        <D.9555>:
        D.9556 = thr_ret != 0;
        D.9557 = (long int) D.9556;
        D.9558 = __builtin_expect (D.9557, 0);
        if (D.9558 != 0) goto <D.9559>; else goto <D.9560>;
        <D.9559>:
        monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "../../mono/io-layer/handles-private.h", 151, "thr_ret == 0");
        <D.9560>:
        handle_data->signalled = state;
        if (broadcast == 1) goto <D.9561>; else goto <D.9562>;
        <D.9561>:
        D.9563 = &handle_data->signal_cond;
        thr_ret = pthread_cond_broadcast (D.9563);
        if (thr_ret != 0) goto <D.9564>; else goto <D.9565>;
        <D.9564>:
        monoeg_g_log (0B, 16, "Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
        <D.9565>:
        D.9556 = thr_ret != 0;
        D.9557 = (long int) D.9556;
        D.9558 = __builtin_expect (D.9557, 0);
        if (D.9558 != 0) goto <D.9566>; else goto <D.9567>;
        <D.9566>:
        monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "../../mono/io-layer/handles-private.h", 162, "thr_ret == 0");
        <D.9567>:
        goto <D.9568>;
        <D.9562>:
        D.9563 = &handle_data->signal_cond;
        thr_ret = pthread_cond_signal (D.9563);
        if (thr_ret != 0) goto <D.9569>; else goto <D.9570>;
        <D.9569>:
        monoeg_g_log (0B, 16, "Bad call to pthread_cond_signal result %d for handle %p", thr_ret, handle);
        <D.9570>:
        D.9556 = thr_ret != 0;
        D.9557 = (long int) D.9556;
        D.9558 = __builtin_expect (D.9557, 0);
        if (D.9558 != 0) goto <D.9571>; else goto <D.9572>;
        <D.9571>:
        monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "../../mono/io-layer/handles-private.h", 167, "thr_ret == 0");
        <D.9572>:
        <D.9568>:
        _wapi_global_signal_cond.4 = _wapi_global_signal_cond;
        thr_ret = pthread_cond_broadcast (_wapi_global_signal_cond.4);
        if (thr_ret != 0) goto <D.9574>; else goto <D.9575>;
        <D.9574>:
        monoeg_g_log (0B, 16, "Bad call to pthread_cond_broadcast result %d for handle %p", thr_ret, handle);
        <D.9575>:
        D.9556 = thr_ret != 0;
        D.9557 = (long int) D.9556;
        D.9558 = __builtin_expect (D.9557, 0);
        if (D.9558 != 0) goto <D.9576>; else goto <D.9577>;
        <D.9576>:
        monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "../../mono/io-layer/handles-private.h", 176, "thr_ret == 0");
        <D.9577>:
        _wapi_global_signal_mutex.3 = _wapi_global_signal_mutex;
        thr_ret = pthread_mutex_unlock (_wapi_global_signal_mutex.3);
        if (thr_ret != 0) goto <D.9578>; else goto <D.9579>;
        <D.9578>:
        monoeg_g_log (0B, 16, "Bad call to mono_mutex_unlock result %d for global signal mutex", thr_ret);
        <D.9579>:
        D.9556 = thr_ret != 0;
        D.9557 = (long int) D.9556;
        D.9558 = __builtin_expect (D.9557, 0);
        if (D.9558 != 0) goto <D.9580>; else goto <D.9581>;
        <D.9580>:
        monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "../../mono/io-layer/handles-private.h", 181, "thr_ret == 0");
        <D.9581>:
        __pthread_unregister_cancel (&__cancel_buf);
        if (0 != 0) goto <D.9582>; else goto <D.9583>;
        <D.9582>:
        __cancel_routine (__cancel_arg);
        <D.9583>:
      }
    finally
      {
        __cancel_buf = {CLOBBER};
      }
  }
  goto <D.9584>;
  <D.9548>:
  handle_data->signalled = state;
  <D.9584>:
}


mono_mutex_unlock_in_cleanup (union mono_mutex_t * mutex)
{
  int D.9595;

  D.9595 = pthread_mutex_unlock (mutex);
  return D.9595;
}


_wapi_handle_type (void * handle)
{
  long int handle.5;
  unsigned int D.9601;
  struct _WapiHandleUnshared * D.9602;
  WapiHandleType D.9603;
  unsigned int D.9604;
  struct _WapiHandleUnshared * D.9605;
  long unsigned int D.9606;
  long unsigned int D.9607;
  long unsigned int D.9608;
  struct _WapiHandleUnshared * D.9609;
  guint32 idx;

  handle.5 = (long int) handle;
  idx = (guint32) handle.5;
  if (idx > 4194303) goto <D.9598>; else goto <D.9600>;
  <D.9600>:
  D.9601 = idx / 256;
  D.9602 = _wapi_private_handles[D.9601];
  if (D.9602 == 0B) goto <D.9598>; else goto <D.9599>;
  <D.9598>:
  D.9603 = 0;
  return D.9603;
  <D.9599>:
  D.9604 = idx / 256;
  D.9605 = _wapi_private_handles[D.9604];
  D.9606 = (long unsigned int) idx;
  D.9607 = D.9606 & 255;
  D.9608 = D.9607 * 200;
  D.9609 = D.9605 + D.9608;
  D.9603 = D.9609->type;
  return D.9603;
}


sema_signal (void * handle)
{
  ReleaseSemaphore (handle, 1, 0B);
}


_wapi_sem_details (void * handle_info)
{
  int D.9611;
  unsigned int D.9612;
  struct _WapiHandle_sem * sem;

  sem = handle_info;
  D.9611 = sem->max;
  D.9612 = sem->val;
  monoeg_g_print ("val: %5u, max: %5d", D.9612, D.9611);
}


namedsema_own (void * handle)
{
  gboolean D.9615;
  struct _WapiHandle_namedsem * namedsem_handle.6;
  unsigned int D.9617;
  unsigned int D.9618;
  struct _WapiHandle_namedsem * namedsem_handle;
  gboolean ok;
  static const char __func__[14] = "namedsema_own";

  try
    {
      ok = _wapi_lookup_handle (handle, 12, &namedsem_handle);
      if (ok == 0) goto <D.9613>; else goto <D.9614>;
      <D.9613>:
      monoeg_g_log (0B, 16, "%s: error looking up named sem handle %p", &__func__, handle);
      D.9615 = 0;
      return D.9615;
      <D.9614>:
      namedsem_handle.6 = namedsem_handle;
      D.9617 = namedsem_handle.6->val;
      D.9618 = D.9617 + 4294967295;
      namedsem_handle.6->val = D.9618;
      namedsem_handle.6 = namedsem_handle;
      D.9617 = namedsem_handle.6->val;
      if (D.9617 == 0) goto <D.9619>; else goto <D.9620>;
      <D.9619>:
      _wapi_shared_handle_set_signal_state (handle, 0);
      <D.9620>:
      D.9615 = 1;
      return D.9615;
    }
  finally
    {
      namedsem_handle = {CLOBBER};
    }
}


_wapi_shared_handle_set_signal_state (void * handle, gboolean state)
{
  long int handle.7;
  int iftmp.8;
  <unnamed type> D.9629;
  <unnamed type> D.9631;
  <unnamed type> D.9633;
  <unnamed type> D.9635;
  _Bool D.9637;
  long int D.9638;
  long int D.9639;
  unsigned int D.9642;
  struct _WapiHandleUnshared * D.9643;
  long unsigned int D.9644;
  long unsigned int D.9645;
  long unsigned int D.9646;
  struct _WapiHandleSharedLayout * _wapi_shared_layout.9;
  unsigned int D.9648;
  guint32 idx;
  struct _WapiHandleUnshared * handle_data;
  struct _WapiHandle_shared_ref * ref;
  struct _WapiHandleShared * shared_data;

  handle.7 = (long int) handle;
  idx = (guint32) handle.7;
  if (idx > 4194303) goto <D.9624>; else goto <D.9625>;
  <D.9624>:
  return;
  <D.9625>:
  D.9629 = _wapi_handle_type (handle);
  if (D.9629 != 9) goto <D.9630>; else goto <D.9627>;
  <D.9630>:
  D.9631 = _wapi_handle_type (handle);
  if (D.9631 != 11) goto <D.9632>; else goto <D.9627>;
  <D.9632>:
  D.9633 = _wapi_handle_type (handle);
  if (D.9633 != 12) goto <D.9634>; else goto <D.9627>;
  <D.9634>:
  D.9635 = _wapi_handle_type (handle);
  if (D.9635 != 13) goto <D.9636>; else goto <D.9627>;
  <D.9636>:
  iftmp.8 = 1;
  goto <D.9628>;
  <D.9627>:
  iftmp.8 = 0;
  <D.9628>:
  D.9637 = iftmp.8 != 0;
  D.9638 = (long int) D.9637;
  D.9639 = __builtin_expect (D.9638, 0);
  if (D.9639 != 0) goto <D.9640>; else goto <D.9641>;
  <D.9640>:
  monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "../../mono/io-layer/handles-private.h", 201, "_WAPI_SHARED_HANDLE(_wapi_handle_type (handle))");
  <D.9641>:
  D.9642 = idx / 256;
  D.9643 = _wapi_private_handles[D.9642];
  D.9644 = (long unsigned int) idx;
  D.9645 = D.9644 & 255;
  D.9646 = D.9645 * 200;
  handle_data = D.9643 + D.9646;
  ref = &handle_data->u.shared;
  _wapi_shared_layout.9 = _wapi_shared_layout;
  D.9648 = ref->offset;
  shared_data = &_wapi_shared_layout.9->handles[D.9648];
  shared_data->signalled = state;
}


namedsema_signal (void * handle)
{
  ReleaseSemaphore (handle, 1, 0B);
}


CreateSemaphore (struct WapiSecurityAttributes * security, gint32 initial, gint32 max, const gunichar2 * name)
{
  void * D.9652;
  _Bool D.9653;
  _Bool D.9654;
  _Bool D.9655;

  mono_once (&sem_ops_once, sem_ops_init);
  if (max <= 0) goto <D.9650>; else goto <D.9651>;
  <D.9650>:
  SetLastError (87);
  D.9652 = 0B;
  return D.9652;
  <D.9651>:
  D.9653 = initial > max;
  D.9654 = initial < 0;
  D.9655 = D.9653 | D.9654;
  if (D.9655 != 0) goto <D.9656>; else goto <D.9657>;
  <D.9656>:
  SetLastError (87);
  D.9652 = 0B;
  return D.9652;
  <D.9657>:
  if (name == 0B) goto <D.9658>; else goto <D.9659>;
  <D.9658>:
  D.9652 = sem_create (security, initial, max);
  return D.9652;
  <D.9659>:
  D.9652 = namedsem_create (security, initial, max, name);
  return D.9652;
}


sem_ops_init ()
{
  _wapi_handle_register_capabilities (4, 3);
  _wapi_handle_register_capabilities (12, 3);
}


sem_create (struct WapiSecurityAttributes * security, gint32 initial, gint32 max)
{
  unsigned int initial.10;
  void * D.9664;
  long int D.9665;
  long int D.9666;
  _Bool D.9669;
  long int D.9670;
  long int D.9671;
  struct _WapiHandle_sem sem_handle;
  void * handle;
  int thr_ret;
  static const char __func__[11] = "sem_create";

  try
    {
      sem_handle = {};
      SetLastError (0);
      initial.10 = (unsigned int) initial;
      sem_handle.val = initial.10;
      sem_handle.max = max;
      handle = _wapi_handle_new (4, &sem_handle);
      if (handle == -1B) goto <D.9662>; else goto <D.9663>;
      <D.9662>:
      monoeg_g_log (0B, 16, "%s: error creating semaphore handle", &__func__);
      SetLastError (31);
      D.9664 = 0B;
      return D.9664;
      <D.9663>:
      {
        struct __pthread_unwind_buf_t __cancel_buf;
        void (*<Tc2>) (void *) __cancel_routine;
        void * __cancel_arg;
        int __not_first_call;

        try
          {
            __cancel_routine = _wapi_handle_unlock_handle;
            __cancel_arg = handle;
            __not_first_call = __sigsetjmp (&__cancel_buf.__cancel_jmp_buf, 0);
            D.9665 = (long int) __not_first_call;
            D.9666 = __builtin_expect (D.9665, 0);
            if (D.9666 != 0) goto <D.9667>; else goto <D.9668>;
            <D.9667>:
            __cancel_routine (__cancel_arg);
            __pthread_unwind_next (&__cancel_buf);
            <D.9668>:
            __pthread_register_cancel (&__cancel_buf);
            thr_ret = _wapi_handle_lock_handle (handle);
            D.9669 = thr_ret != 0;
            D.9670 = (long int) D.9669;
            D.9671 = __builtin_expect (D.9670, 0);
            if (D.9671 != 0) goto <D.9672>; else goto <D.9673>;
            <D.9672>:
            monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 188, "thr_ret == 0");
            <D.9673>:
            if (initial != 0) goto <D.9674>; else goto <D.9675>;
            <D.9674>:
            _wapi_handle_set_signal_state (handle, 1, 0);
            <D.9675>:
            thr_ret = _wapi_handle_unlock_handle (handle);
            D.9669 = thr_ret != 0;
            D.9670 = (long int) D.9669;
            D.9671 = __builtin_expect (D.9670, 0);
            if (D.9671 != 0) goto <D.9676>; else goto <D.9677>;
            <D.9676>:
            monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 198, "thr_ret == 0");
            <D.9677>:
            __pthread_unregister_cancel (&__cancel_buf);
            if (0 != 0) goto <D.9678>; else goto <D.9679>;
            <D.9678>:
            __cancel_routine (__cancel_arg);
            <D.9679>:
          }
        finally
          {
            __cancel_buf = {CLOBBER};
          }
      }
      D.9664 = handle;
      return D.9664;
    }
  finally
    {
      sem_handle = {CLOBBER};
    }
}


_wapi_handle_lock_handle (void * handle)
{
  long int handle.11;
  int D.9688;
  <unnamed type> D.9691;
  <unnamed type> D.9693;
  <unnamed type> D.9695;
  <unnamed type> D.9697;
  unsigned int D.9698;
  struct _WapiHandleUnshared * D.9699;
  long unsigned int D.9700;
  long unsigned int D.9701;
  long unsigned int D.9702;
  struct _WapiHandleUnshared * D.9703;
  union mono_mutex_t * D.9704;
  guint32 idx;

  handle.11 = (long int) handle;
  idx = (guint32) handle.11;
  if (idx > 4194303) goto <D.9686>; else goto <D.9687>;
  <D.9686>:
  D.9688 = 0;
  return D.9688;
  <D.9687>:
  _wapi_handle_ref (handle);
  D.9691 = _wapi_handle_type (handle);
  if (D.9691 == 9) goto <D.9689>; else goto <D.9692>;
  <D.9692>:
  D.9693 = _wapi_handle_type (handle);
  if (D.9693 == 11) goto <D.9689>; else goto <D.9694>;
  <D.9694>:
  D.9695 = _wapi_handle_type (handle);
  if (D.9695 == 12) goto <D.9689>; else goto <D.9696>;
  <D.9696>:
  D.9697 = _wapi_handle_type (handle);
  if (D.9697 == 13) goto <D.9689>; else goto <D.9690>;
  <D.9689>:
  D.9688 = 0;
  return D.9688;
  <D.9690>:
  D.9698 = idx / 256;
  D.9699 = _wapi_private_handles[D.9698];
  D.9700 = (long unsigned int) idx;
  D.9701 = D.9700 & 255;
  D.9702 = D.9701 * 200;
  D.9703 = D.9699 + D.9702;
  D.9704 = &D.9703->signal_mutex;
  D.9688 = pthread_mutex_lock (D.9704);
  return D.9688;
}


_wapi_handle_unlock_handle (void * handle)
{
  long int handle.12;
  int D.9709;
  <unnamed type> D.9712;
  <unnamed type> D.9714;
  <unnamed type> D.9716;
  <unnamed type> D.9718;
  unsigned int D.9719;
  struct _WapiHandleUnshared * D.9720;
  long unsigned int D.9721;
  long unsigned int D.9722;
  long unsigned int D.9723;
  struct _WapiHandleUnshared * D.9724;
  union mono_mutex_t * D.9725;
  guint32 idx;
  int ret;

  handle.12 = (long int) handle;
  idx = (guint32) handle.12;
  if (idx > 4194303) goto <D.9707>; else goto <D.9708>;
  <D.9707>:
  D.9709 = 0;
  return D.9709;
  <D.9708>:
  D.9712 = _wapi_handle_type (handle);
  if (D.9712 == 9) goto <D.9710>; else goto <D.9713>;
  <D.9713>:
  D.9714 = _wapi_handle_type (handle);
  if (D.9714 == 11) goto <D.9710>; else goto <D.9715>;
  <D.9715>:
  D.9716 = _wapi_handle_type (handle);
  if (D.9716 == 12) goto <D.9710>; else goto <D.9717>;
  <D.9717>:
  D.9718 = _wapi_handle_type (handle);
  if (D.9718 == 13) goto <D.9710>; else goto <D.9711>;
  <D.9710>:
  _wapi_handle_unref (handle);
  D.9709 = 0;
  return D.9709;
  <D.9711>:
  D.9719 = idx / 256;
  D.9720 = _wapi_private_handles[D.9719];
  D.9721 = (long unsigned int) idx;
  D.9722 = D.9721 & 255;
  D.9723 = D.9722 * 200;
  D.9724 = D.9720 + D.9723;
  D.9725 = &D.9724->signal_mutex;
  ret = pthread_mutex_unlock (D.9725);
  _wapi_handle_unref (handle);
  D.9709 = ret;
  return D.9709;
}


namedsem_create (struct WapiSecurityAttributes * security, gint32 initial, gint32 max, const gunichar2 * name)
{
  _Bool D.9727;
  long int D.9728;
  long int D.9729;
  long unsigned int D.9738;
  long unsigned int D.9741;
  long unsigned int D.9743;
  unsigned int initial.13;
  unsigned int offset.14;
  void * D.9755;
  struct _WapiHandle_namedsem namedsem_handle;
  void * handle;
  gchar * utf8_name;
  int thr_ret;
  void * ret;
  guint32 namelen;
  gint32 offset;
  void cleanup = <<< error >>>;
  static const char __func__[16] = "namedsem_create";

  try
    {
      namedsem_handle = {};
      ret = 0B;
      thr_ret = _wapi_namespace_lock ();
      D.9727 = thr_ret != 0;
      D.9728 = (long int) D.9727;
      D.9729 = __builtin_expect (D.9728, 0);
      if (D.9729 != 0) goto <D.9730>; else goto <D.9731>;
      <D.9730>:
      monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 218, "thr_ret == 0");
      <D.9731>:
      SetLastError (0);
      utf8_name = monoeg_g_utf16_to_utf8 (name, -1, 0B, 0B, 0B);
      offset = _wapi_search_handle_namespace (12, utf8_name);
      if (offset == -1) goto <D.9732>; else goto <D.9733>;
      <D.9732>:
      SetLastError (6);
      goto cleanup;
      <D.9733>:
      if (offset != 0) goto <D.9734>; else goto <D.9735>;
      <D.9734>:
      SetLastError (183);
      <D.9735>:
      if (offset == 0) goto <D.9736>; else goto <D.9737>;
      <D.9736>:
      D.9738 = strlen (utf8_name);
      if (D.9738 <= 259) goto <D.9739>; else goto <D.9740>;
      <D.9739>:
      D.9741 = strlen (utf8_name);
      namelen = (guint32) D.9741;
      goto <D.9742>;
      <D.9740>:
      namelen = 260;
      <D.9742>:
      D.9743 = (long unsigned int) namelen;
      memcpy (&namedsem_handle.sharedns.name, utf8_name, D.9743);
      initial.13 = (unsigned int) initial;
      namedsem_handle.val = initial.13;
      namedsem_handle.max = max;
      handle = _wapi_handle_new (12, &namedsem_handle);
      goto <D.9745>;
      <D.9737>:
      offset.14 = (unsigned int) offset;
      handle = _wapi_handle_new_from_offset (12, offset.14, 1);
      <D.9745>:
      if (handle == -1B) goto <D.9747>; else goto <D.9748>;
      <D.9747>:
      monoeg_g_log (0B, 16, "%s: error creating named sem handle", &__func__);
      SetLastError (31);
      goto cleanup;
      <D.9748>:
      ret = handle;
      if (offset == 0) goto <D.9749>; else goto <D.9750>;
      <D.9749>:
      thr_ret = _wapi_handle_lock_shared_handles ();
      D.9727 = thr_ret != 0;
      D.9728 = (long int) D.9727;
      D.9729 = __builtin_expect (D.9728, 0);
      if (D.9729 != 0) goto <D.9751>; else goto <D.9752>;
      <D.9751>:
      monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 283, "thr_ret == 0");
      <D.9752>:
      if (initial != 0) goto <D.9753>; else goto <D.9754>;
      <D.9753>:
      _wapi_shared_handle_set_signal_state (handle, 1);
      <D.9754>:
      _wapi_handle_unlock_shared_handles ();
      <D.9750>:
      cleanup:
      monoeg_g_free (utf8_name);
      _wapi_namespace_unlock (0B);
      D.9755 = ret;
      return D.9755;
    }
  finally
    {
      namedsem_handle = {CLOBBER};
    }
}


_wapi_namespace_lock ()
{
  int D.9758;

  D.9758 = _wapi_shm_sem_lock (0);
  return D.9758;
}


memcpy (void * restrict __dest, const void * restrict __src, size_t __len)
{
  void * D.9760;
  long unsigned int D.9761;

  D.9761 = __builtin_object_size (__dest, 0);
  D.9760 = __builtin___memcpy_chk (__dest, __src, __len, D.9761);
  return D.9760;
}


_wapi_handle_lock_shared_handles ()
{
  int D.9763;

  D.9763 = _wapi_shm_sem_lock (3);
  return D.9763;
}


_wapi_handle_unlock_shared_handles ()
{
  int D.9765;

  D.9765 = _wapi_shm_sem_unlock (3);
  return D.9765;
}


_wapi_namespace_unlock (void * data)
{
  int D.9767;

  D.9767 = _wapi_shm_sem_unlock (0);
  return D.9767;
}


ReleaseSemaphore (void * handle, gint32 count, gint32 * prevcount)
{
  gboolean D.9771;
  gboolean (*<T1648>) (void *, gint32, gint32 *) D.9772;
  WapiHandleType type;

  if (handle == 0B) goto <D.9769>; else goto <D.9770>;
  <D.9769>:
  SetLastError (6);
  D.9771 = 0;
  return D.9771;
  <D.9770>:
  type = _wapi_handle_type (handle);
  D.9772 = sem_ops[type].release;
  if (D.9772 == 0B) goto <D.9773>; else goto <D.9774>;
  <D.9773>:
  SetLastError (6);
  D.9771 = 0;
  return D.9771;
  <D.9774>:
  D.9772 = sem_ops[type].release;
  D.9771 = D.9772 (handle, count, prevcount);
  return D.9771;
}


namedsem_release (void * handle, gint32 count, gint32 * prevcount)
{
  gboolean D.9778;
  _Bool D.9779;
  long int D.9780;
  long int D.9781;
  struct _WapiHandle_namedsem * sem_handle.15;
  unsigned int D.9787;
  int D.9788;
  unsigned int count.16;
  unsigned int D.9790;
  int D.9791;
  unsigned int D.9792;
  struct _WapiHandle_namedsem * sem_handle;
  gboolean ok;
  gboolean ret;
  int thr_ret;
  static const char __func__[17] = "namedsem_release";
  void end = <<< error >>>;

  try
    {
      ret = 0;
      ok = _wapi_lookup_handle (handle, 12, &sem_handle);
      if (ok == 0) goto <D.9776>; else goto <D.9777>;
      <D.9776>:
      monoeg_g_log (0B, 16, "%s: error looking up sem handle %p", &__func__, handle);
      D.9778 = 0;
      return D.9778;
      <D.9777>:
      thr_ret = _wapi_handle_lock_shared_handles ();
      D.9779 = thr_ret != 0;
      D.9780 = (long int) D.9779;
      D.9781 = __builtin_expect (D.9780, 0);
      if (D.9781 != 0) goto <D.9782>; else goto <D.9783>;
      <D.9782>:
      monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 415, "thr_ret == 0");
      <D.9783>:
      if (prevcount != 0B) goto <D.9784>; else goto <D.9785>;
      <D.9784>:
      sem_handle.15 = sem_handle;
      D.9787 = sem_handle.15->val;
      D.9788 = (int) D.9787;
      *prevcount = D.9788;
      <D.9785>:
      sem_handle.15 = sem_handle;
      D.9787 = sem_handle.15->val;
      count.16 = (unsigned int) count;
      D.9790 = D.9787 + count.16;
      sem_handle.15 = sem_handle;
      D.9791 = sem_handle.15->max;
      D.9792 = (unsigned int) D.9791;
      if (D.9790 > D.9792) goto end; else goto <D.9793>;
      <D.9793>:
      sem_handle.15 = sem_handle;
      sem_handle.15 = sem_handle;
      D.9787 = sem_handle.15->val;
      count.16 = (unsigned int) count;
      D.9790 = D.9787 + count.16;
      sem_handle.15->val = D.9790;
      _wapi_shared_handle_set_signal_state (handle, 1);
      ret = 1;
      end:
      _wapi_handle_unlock_shared_handles ();
      D.9778 = ret;
      return D.9778;
    }
  finally
    {
      sem_handle = {CLOBBER};
    }
}


sem_release (void * handle, gint32 count, gint32 * prevcount)
{
  gboolean D.9798;
  long int D.9799;
  long int D.9800;
  _Bool D.9803;
  long int D.9804;
  long int D.9805;
  struct _WapiHandle_sem * sem_handle.17;
  unsigned int D.9811;
  int D.9812;
  unsigned int count.18;
  unsigned int D.9814;
  int D.9815;
  unsigned int D.9816;
  struct _WapiHandle_sem * sem_handle;
  gboolean ok;
  gboolean ret;
  int thr_ret;
  static const char __func__[12] = "sem_release";
  void end = <<< error >>>;

  try
    {
      ret = 0;
      ok = _wapi_lookup_handle (handle, 4, &sem_handle);
      if (ok == 0) goto <D.9796>; else goto <D.9797>;
      <D.9796>:
      monoeg_g_log (0B, 16, "%s: error looking up sem handle %p", &__func__, handle);
      D.9798 = 0;
      return D.9798;
      <D.9797>:
      {
        struct __pthread_unwind_buf_t __cancel_buf;
        void (*<Tc2>) (void *) __cancel_routine;
        void * __cancel_arg;
        int __not_first_call;

        try
          {
            __cancel_routine = _wapi_handle_unlock_handle;
            __cancel_arg = handle;
            __not_first_call = __sigsetjmp (&__cancel_buf.__cancel_jmp_buf, 0);
            D.9799 = (long int) __not_first_call;
            D.9800 = __builtin_expect (D.9799, 0);
            if (D.9800 != 0) goto <D.9801>; else goto <D.9802>;
            <D.9801>:
            __cancel_routine (__cancel_arg);
            __pthread_unwind_next (&__cancel_buf);
            <D.9802>:
            __pthread_register_cancel (&__cancel_buf);
            thr_ret = _wapi_handle_lock_handle (handle);
            D.9803 = thr_ret != 0;
            D.9804 = (long int) D.9803;
            D.9805 = __builtin_expect (D.9804, 0);
            if (D.9805 != 0) goto <D.9806>; else goto <D.9807>;
            <D.9806>:
            monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 364, "thr_ret == 0");
            <D.9807>:
            if (prevcount != 0B) goto <D.9808>; else goto <D.9809>;
            <D.9808>:
            sem_handle.17 = sem_handle;
            D.9811 = sem_handle.17->val;
            D.9812 = (int) D.9811;
            *prevcount = D.9812;
            <D.9809>:
            sem_handle.17 = sem_handle;
            D.9811 = sem_handle.17->val;
            count.18 = (unsigned int) count;
            D.9814 = D.9811 + count.18;
            sem_handle.17 = sem_handle;
            D.9815 = sem_handle.17->max;
            D.9816 = (unsigned int) D.9815;
            if (D.9814 > D.9816) goto end; else goto <D.9817>;
            <D.9817>:
            sem_handle.17 = sem_handle;
            sem_handle.17 = sem_handle;
            D.9811 = sem_handle.17->val;
            count.18 = (unsigned int) count;
            D.9814 = D.9811 + count.18;
            sem_handle.17->val = D.9814;
            _wapi_handle_set_signal_state (handle, 1, 1);
            ret = 1;
            end:
            thr_ret = _wapi_handle_unlock_handle (handle);
            D.9803 = thr_ret != 0;
            D.9804 = (long int) D.9803;
            D.9805 = __builtin_expect (D.9804, 0);
            if (D.9805 != 0) goto <D.9818>; else goto <D.9819>;
            <D.9818>:
            monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 392, "thr_ret == 0");
            <D.9819>:
            __pthread_unregister_cancel (&__cancel_buf);
            if (0 != 0) goto <D.9820>; else goto <D.9821>;
            <D.9820>:
            __cancel_routine (__cancel_arg);
            <D.9821>:
          }
        finally
          {
            __cancel_buf = {CLOBBER};
          }
      }
      D.9798 = ret;
      return D.9798;
    }
  finally
    {
      sem_handle = {CLOBBER};
    }
}


OpenSemaphore (guint32 access, gboolean inherit, const gunichar2 * name)
{
  _Bool D.9827;
  long int D.9828;
  long int D.9829;
  unsigned int offset.19;
  void * D.9839;
  void * handle;
  gchar * utf8_name;
  int thr_ret;
  void * ret;
  gint32 offset;
  void cleanup = <<< error >>>;
  static const char __func__[14] = "OpenSemaphore";

  ret = 0B;
  mono_once (&sem_ops_once, sem_ops_init);
  thr_ret = _wapi_namespace_lock ();
  D.9827 = thr_ret != 0;
  D.9828 = (long int) D.9827;
  D.9829 = __builtin_expect (D.9828, 0);
  if (D.9829 != 0) goto <D.9830>; else goto <D.9831>;
  <D.9830>:
  monoeg_assertion_message ("* Assertion at %s:%d, condition `%s\' not met\n", "semaphores.c", 494, "thr_ret == 0");
  <D.9831>:
  utf8_name = monoeg_g_utf16_to_utf8 (name, -1, 0B, 0B, 0B);
  offset = _wapi_search_handle_namespace (12, utf8_name);
  if (offset == -1) goto <D.9832>; else goto <D.9833>;
  <D.9832>:
  SetLastError (6);
  goto cleanup;
  <D.9833>:
  if (offset == 0) goto <D.9834>; else goto <D.9835>;
  <D.9834>:
  SetLastError (2);
  goto cleanup;
  <D.9835>:
  offset.19 = (unsigned int) offset;
  handle = _wapi_handle_new_from_offset (12, offset.19, 1);
  if (handle == -1B) goto <D.9837>; else goto <D.9838>;
  <D.9837>:
  monoeg_g_log (0B, 16, "%s: error opening named sem handle", &__func__);
  SetLastError (31);
  goto cleanup;
  <D.9838>:
  ret = handle;
  cleanup:
  monoeg_g_free (utf8_name);
  _wapi_namespace_unlock (0B);
  D.9839 = ret;
  return D.9839;
}


