This code is one of the winners of the 22nd International Obfuscated C Code Contest. It takes 3 arguments: the last name of any of the last 31 US Presidents, the string “republican” and the string “democrat”. The program then prints out either “republican” or “democrat” depending on which political party the president was affiliated with. The code is surprisingly small, which means it shouldn’t be too difficult to figure out how it works.
The first thing to do is rename the variables to sensible names and add some standard main() code.
In C var and 1[var] are equivalent, so let’s swap it around to look more normal.
The next step is to pull out the magic math into a separate statement. And since argc is always 4, replace it with that constant.
This helps with understanding what the magic math is doing. The integer i is either 0 or 1, which is then used to print out either the 2nd or 3rd argument to the program. What I did was hard code the strings “democrat” and “republican” so that it isn’t necessary to pass them as arguments.
The integer i is made up of the first 4 characters of the President’s name, the next step is to replace the pointer casting with more explicit steps.
This is about as clear as the code can be. The only mystery left is why the constants 4796, 275 and 4 were chosen. I imagine that the original author ran a bruteforce calculation to determine what numbers would cause the correct division between the names.