#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 scalar_t__ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  pid_t a;
  a = fork();
  if (a < 0) {
    perror("fork");
    exit(1);
  } else if (a == 0) {
    printf("child: PID=%d, PPID=%d\n", getpid(), getppid());
    return 77;
  } else if (a > 0) {
    int status;
    if (waitpid(a, &status, 0) > 0) {
      if (WIFEXITED(status)) {
        printf("%d\n", WEXITSTATUS(status));
      }
    }
    printf("parent: PID=%d, PPID=%d\n", getpid(), getppid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11cb <main+0x3b>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1206 <main+0x76>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x4d,-0x4(%rbp)
jmp    128e <main+0xfe>
cmpq   $0x0,-0x10(%rbp)
jle    127d <main+0xed>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jle    1259 <main+0xc9>
mov    -0x14(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1254 <main+0xc4>
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1259 <main+0xc9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1282 <main+0xf2>
jmp    1287 <main+0xf7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1236 <main+0xa6>
je     1206 <main+0x76>
lea    0x4(%rsp),%rsi
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jle    11e2 <main+0x52>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11e2 <main+0x52>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe40(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xe24(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    122d <main+0x9d>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xde8(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x4d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [24 x i8] c"child: PID=%d, PPID=%d\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [25 x i8] c"parent: PID=%d, PPID=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 (...) @getppid()
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15)
store i32 77, i32* %1
br label %40
17: ; preds = %10
%18 = load i64, i64* %2
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = load i64, i64* %2
%22 = call i64 @waitpid(i64 %21, i32* %3, i32 0)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i32, i32* %3
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* %3
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28, %24
br label %33
33: ; preds = %32, %20
%34 = call i32 (...) @getpid()
%35 = call i32 (...) @getppid()
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %35)
br label %37
37: ; preds = %33, %17
br label %38
38: ; preds = %37
br label %39
39: ; preds = %38
store i32 0, i32* %1
br label %40
40: ; preds = %39, %13
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [24 x i8] c"child: PID=%d, PPID=%d\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [25 x i8] c"parent: PID=%d, PPID=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%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 = %0
%8 = icmp eq i64 %2, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (...) @getppid()
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %11)
br label %29
13: ; preds = %7
%14 = bitcast i32* %1 to i8*
%15 = call i64 @waitpid(i64 %2, i32* %1, i32 0)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %13
%18 = load i32, i32* %1
%19 = call i64 @WIFEXITED(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i32, i32* %1
%23 = call i32 @WEXITSTATUS(i32 %22)
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %17, %21, %13
%26 = call i32 (...) @getpid()
%27 = call i32 (...) @getppid()
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %25, %9
%30 = phi i32 [ 77, %9 ], [ 0, %25 ]
ret i32 %30
}