That fact that a program compiles does not guarantee that it will work, only that it doesn't have any obvious syntax or similar issues. It's just an early step in the process.
Your instincts are correct, the code is trying to write to or close a file pointer that's set to null.
The seg fault problem lies in this code:
if (jpeg_image == 0)
It's a problem with processing the EOF condition on the input file. The code is structured so that it reads in 512 bytes, copies them out to the output file, and then checks for EOF. In other words, "Ready, Fire, Aim!"
Note that the EOF flag is not set until an attempt is made to read beyond the end of the file. When the last byte in the input ...