in reply to system() and malformed headers

You must write a header before you write to stderr, your printf statement gets buffered by libc and isn't actually written to stdout until after the system call and because stderr is not buffered it gets written first. When you execute the program from the shell it works fine because stdout on a tty is line buffered. If you redirect the output from the command line to a file (/path/to/program > filename 2>&1) you will probably see the stderr before the stdout.