title: Program hangs with no output
body: <p>I've been poring over this program for ages, and have no idea why it doesn't work. I'm reasonably sure it's doing everything right but instead of actually <em>working</em> it just hangs indefinitely after printing the first prompt, and I just can't figure out why. I'm pretty much at my wit's end now, so if any can suggest what I'm doing wrong, I'd be much obliged...</p>
  
  <p>It's C99, and you'll need the mhash library to compile it (uses for the CRC32 calculation). It's pretty portable but I developed it on Linux. <strong>Do not run in a VM!</strong></p>
  
  <pre><code>#define _BSD_SOURCE
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;mhash.h&gt;
  
  /* WARNING: Do not debug this program. Halting on breakpoints at the wrong
   * time can be extremely hazardous. YOU HAVE BEEN WARNED. */
  
  /* Structures used to define our layout. Note the careful use of volatile;
   * we don't want the compiler optimising away part of the invocation. */
  
  typedef struct
  {
      const char name[7];       /* sigil at focus */
      volatile int target;      /* summoning point */
      volatile char invocation; /* current char of invocation */
  } focus_t;
  
  typedef struct node
  {
      const char name[4];   /* name of node */
      focus_t* center;      /* points to the evocation focus */
      struct node* cw;      /* clockwise binding ring */
      struct node* ccw;     /* counterclockwise binding ring */
      struct node* star;    /* next node of star */
      const char* linkname; /* name of star linkage */
      volatile uint32_t angel; /* protective angel for this node */
  } node_t;
  
  /* The pentacle nodes are circularly linked in both directions to form
   * a binding perimeter. In addition, they are singly linked to form a
   * classic 'daemon trap' five-pointed star. Each node points towards the
   * evocation focus (but not the other way around!) to enforce the geometry
   * we want. The design is based heavily on the Pentagram of Solomon. */
  
  struct
  {
      focus_t focus;
      node_t node[5];
  }
  S =
  {
      /* None of the symbols for the pentacle are in Unicode. So we have to make
       * do with Latin transcriptions. */
      .focus = { "SOLUZEN", 0 },
      .node = {
          [0] = { "TE",   &amp;S.focus, &amp;S.node[1], &amp;S.node[4], &amp;S.node[2], "BELLONY" },
          [1] = { "TRA",  &amp;S.focus, &amp;S.node[2], &amp;S.node[0], &amp;S.node[3], "HALLIY" },
          [2] = { "GRAM", &amp;S.focus, &amp;S.node[3], &amp;S.node[1], &amp;S.node[4], "HALLIZA" },
          [3] = { "MA",   &amp;S.focus, &amp;S.node[4], &amp;S.node[2], &amp;S.node[0], "ABDIA" },
          [4] = { "TON",  &amp;S.focus, &amp;S.node[0], &amp;S.node[3], &amp;S.node[1], "BALLATON" }
      }
  };
  
  /* Name of spirit to summon --- rot13'd for safety.
   * (#65 from Crowley's translation of SHEMHAMPHORASH.)
   * This is Andrealphus, he that has dominion over menusuration, astronomy and
   * geometry. He seems fairly non-threatening. */
  
  const char spiritname[] = "NAQERNYCUHF";
  int rot13(int c) { return 'A' + (((c - 'A') + 13) % 26); }
  
  /* We invoke the following names around the circle as a protective measure.
   * Strictly these should be in Hebrew script, but as the computer is a dumb
   * instrument we're relying on the symbolism rather than the actual literal
   * meaning themselves. Plus, working in RTL is a pain. */
  
  const char* angels[] = {
          "Kether", "Eheieh", "Metatron", "Chaioth ha-Qadesh",
          "Rashith ha-Gilgalim", "Chokmah", "Jah", "Ratziel", "Auphanim",
          "Masloth", "Binah", "Jehovah Elohim", "Tzaphkiel", "Aralim",
          "Shabbathai", "Chesed", "El", "Tzadkiel", "Chasmalim", "Tzadekh",
          "Geburah", "Elohim Gibor", "Khamael", "Seraphim", "Madim",
          "Tiphareth", "Eloah Va-Daath", "Raphael", "Malachim", "Shemesh",
          "Netzach", "Jehovah Sabaoth", "Haniel", "Elohim", "Nogah", "Hod",
          "Elohim Sabaoth", "Michael", "Beni Elohim", "Kokab", "Yesod",
          "Shaddai El Chai", "Gabriel", "Cherubim", "Levanah"
  };
  const int angelcount = sizeof(angels)/sizeof(*angels);
  
  /* Place the next angel on the pentacle. */
  
  static void updatepentacle()
  {
      static int angelnode = 0;
      static int angelindex = 0;
  
      const char* angel = angels[angelindex++];
      angelindex %= angelcount;
  
      /* Hash the angel's name to reduce its essence to 32 bits (which lets us
       * copy the angel bodily into the pentacle node. */
  
      uint32_t angelhash;
      MHASH td = mhash_init(MHASH_CRC32);
      mhash(td, angel, strlen(angel));
      mhash_deinit(td, &amp;angelhash);
  
      S.node[angelnode].angel = angelhash;
      angelnode = (angelnode + 1) % 5;
  }
  
  int main(int argc, const char* argv[])
  {
      /* Lock the evocation into memory, to prevent it from being paged out
       * while the spirit has manifested --- which would be bad. */
  
      int e = mlock(&amp;S, sizeof(S));
      if (e != 0)
      {
          fprintf(stderr, "Unable to lock evocation, refusing to run\n");
          exit(1);
      }
  
      /* Actually perform the invocation: continually cycle the spirit's
       * name into the evocation focus (while maintaining our pentacle
       * integrity!) until something shows up in the target of the
       * evocation focus. */
  
      printf("Summoning...\n");
      do
      {
          for (int i = 0; i &lt; sizeof(spiritname)-1; i++)
          {
              S.focus.invocation = rot13(spiritname[i]);
              updatepentacle();
              usleep(100); /* don't CPU-starve our spirit */
          }
      }
      while (S.focus.target == 0);
      printf("Summoning successful! %d\n", S.focus.target);
  
      /* Our spirit's arrived! Dismiss it immediately by using a null
       * invocation. Keep going until the evocation focus remains empty.
       * FIXME: a particularly mean spirit might find a way to hide. Until
       * we can sort this out, only summon relatively benign ones. This is
       * probably safe anyway, as when the process terminates the spirit's
       * address space will be nuked, taking the spirit with it. */
  
      printf("Dismissing...\n");
      do
      {
          S.focus.target = 0;
          for (int i = 0; i &lt; 1000; i++)
          {
              S.focus.invocation = 0;
              updatepentacle();
          }
      }
      while (S.focus.target != 0);
  
      printf("Done.\n");
      return 0;
  }
  </code></pre>
  
  <p>Incidentally, shouldn't there be a <code>goetic</code> tag?</p>
  
  <p><strong>Edit:</strong> Sorry I haven't gotten back earlier --- after I posted my query last night I ran some more tests and then my computer started making funny burning smells which didn't go away when I switched it off, so I spent the rest of the night tearing it down trying to find which part was faulty. (Didn't find anything.) I'm going to grab some sleep and get back to you. Thanks for the replies!</p>
  
  <p><strong>Edit:</strong> I'm posting this from a web café. My house has burnt down. Don't have time to post more but have to warn you: <strong>do not run this program</strong> for any reason! Really! Not joking! Have to go now, must find sanctuary somewhere---</p>
  
  <p><strong>Edit:</strong>    .     .</p>