#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ wait(int *);
void forkChild(pid_t f) {
  int st;
  pid_t pid;
  switch (f) {
  case -1:
    perror("fork");
    exit(1);
  case 0:
    puts("child, waiting");
    wait(&st);
    pid = getppid();
    printf("new parent pid: %d\n", pid);
    exit(0);
  default:
    pid = getpid();
    printf("parent pid: %d\n", pid);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<forkChild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0xffffffff,%eax
je     119f <forkChild+0x2f>
jmp    118f <forkChild+0x1f>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11b5 <forkChild+0x45>
jmp    11ea <forkChild+0x7a>
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1090 <wait@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe1a(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<forkChild>:
push   %rax
test   %edi,%edi
je     1190 <forkChild+0x20>
cmp    $0xffffffff,%edi
jne    11b6 <forkChild+0x46>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe60(%rip),%rdi
jmp    11c4 <forkChild+0x54>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe54(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [15 x i8] c"child, waiting\00"
@.str.2 = [20 x i8] c"new parent pid: %d\0A\00"
@.str.3 = [16 x i8] c"parent pid: %d\0A\00"
define void @forkChild(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
switch i32 %5, label %16 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @wait(i32* %3)
%12 = call i32 (...) @getppid()
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %1
%17 = call i32 (...) @getpid()
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [15 x i8] c"child, waiting\00"
@.str.2 = [20 x i8] c"new parent pid: %d\0A\00"
@.str.3 = [16 x i8] c"parent pid: %d\0A\00"
define void @forkChild(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
switch i32 %0, label %13 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @wait(i32* %2)
%10 = call i32 (...) @getppid()
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %1
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i32 @exit(i32 0)
unreachable
}