I was able to reproduce your crash,
Get rid of this line:
SDL_FreeSurface(temp);
temp and optimized point to the same resource ,so freeing one means they both point to garbage now.
In his code he called a function which evidently allocated some copy of that memory, in yours you simply assign the pointer which wont work.
optimized = SDL_DisplayFormatAlpha(temp); //this will create a copy
SDL_FreeSurface(temp);
Aside from that I am not sure this code will produce anything useful as this code looks like it was written for SDL 1.2 (SDL1.2 and SDL2,0 are sufficiently different beasts that you will not be able to mix and match code unless you really know what you are doing).
There are tutorials for 2.0 , i will see if i can dig up where they are for you. ( http://lazyfoo.net/tutorials/SDL/index.php)