  
  extern "C" int enter_enclave(int index, void *ms, void *tcs, int cssa) __attribute__((section(".nipx")));
  
-@@ -87,13 +66,33 @@
+@@ -87,13 +66,37 @@
          return error;
      }
  

 
 #ifdef __cplusplus
 extern "C" {
@@ -82,6 +83,53 @@
 */
 sgx_status_t SGXAPI sgx_read_rand(unsigned char *rand, size_t length_in_bytes);
 

         }
         else if(index == ECMD_INIT_ENCLAVE)
         {
@@ -124,5 +127,10 @@
     {
         set_enclave_state(ENCLAVE_CRASHED);
     }
+    // The ecall has finished and is leaving the enclave.
+    if (index != ECMD_ORET && index != ECMD_EXCEPT)
+    {
+        decrease_ecall_count();
+    }
