// File: /workspace/github_c_makefile_star2_O0/marcespie[P]unix-samples/chapter1/fork1.c
// Function: main
int
main()
{
	int i = 15; // XXX implicit communication
	// since fork() gives you a CC of the parent,
	// -> you get implicit parameter passing to the child
	int pid = fork();
	switch(pid) {
	case -1: 
		err(1, "fork");
	case 0:
		perform_computation(i);
		exit(0);
	default:
		break;
	}
	// this is purely the parent's code
	int status;
	// always check *all* syscalls for errors
	// always use waitpid *even if you have one single child*
	// (you never know when you're going to reuse your code,
	// in a context with more children)
	int r = waitpid(pid, &status, 0);
	if (r == -1)
		err(1, "waitpid");
	// decode status
	// some similar code is *required* each time you interact with
	// children
	// -> we have some simple communication back from child to parent
	// which is whether we succeeded or not
	if (WIFEXITED(status)) {
		int rc = WEXITSTATUS(status);
		if (rc == 0) {
			// that case is 100% silent because 
			exit(0);	// everything is okay
		} else {
			fprintf(stderr, "Child exited with exit(%d)\n", rc);
			exit(1);
		}
	} else if (WIFSIGNALED(status)) {
		int sig = WTERMSIG(status);
		fprintf(stderr, "Child exited with signal %d (%s)\n", 
		    sig, strsignal(sig));
		exit(1);
	} else {
		// under normal circumstances, you either get a signal
		// or an exit status
		fprintf(stderr, "This should never happen: %d\n", status);
		exit(1);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
movl $0xf, -0x8(%rbp)
callq 0x1090
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x20(%rbp)
subl $-0x1, %eax
je 0x1204
jmp 0x11fb
movl -0x20(%rbp), %eax
testl %eax, %eax
je 0x1217
jmp 0x1226
movl $0x1, %edi
leaq 0xe08(%rip), %rsi       # 0x2018
movb $0x0, %al
callq 0x1050
movl -0x8(%rbp), %edi
callq 0x11a0
xorl %edi, %edi
callq 0x1080
jmp 0x1228
movl -0xc(%rbp), %edi
leaq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x1070
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x1252
movl $0x1, %edi
leaq 0xdd2(%rip), %rsi       # 0x201d
movb $0x0, %al
callq 0x1050
movl -0x10(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x129d
movl -0x10(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x1278
xorl %edi, %edi
callq 0x1080
movq 0x2d79(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
leaq 0xd99(%rip), %rsi       # 0x2025
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1080
movl -0x10(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x12f7
movl -0x10(%rbp), %eax
andl $0x7f, %eax
movl %eax, -0x1c(%rbp)
movq 0x2d38(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x1c(%rbp), %edi
callq 0x1060
movq -0x30(%rbp), %rdi
movl -0x24(%rbp), %edx
movq %rax, %rcx
leaq 0xd5b(%rip), %rsi       # 0x2041
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1080
movq 0x2cfa(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x10(%rbp), %edx
leaq 0xd58(%rip), %rsi       # 0x2063
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1080