#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 /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid == 0) {
    if ((pid = fork()) < 0) {
      fprintf(stderr, "fork error\n");
      exit(1);
    } else if (pid > 0) {
      exit(0);
    }
    sleep(2);
    printf("second child, parent pid = %ld\n", (long)getppid());
    exit(0);
  }
  if (waitpid(pid, NULL, 0) != pid) {
    fprintf(stderr, "waitpid error\n");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11ba <main+0x4a>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1237 <main+0xc7>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11f9 <main+0x89>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    120b <main+0x9b>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1210 <main+0xa0>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %rax,%rsi
lea    0xde1(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    123c <main+0xcc>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     1274 <main+0x104>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11a3 <main+0x33>
je     1197 <main+0x27>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %rbx,%rax
jne    11b5 <main+0x45>
xor    %eax,%eax
pop    %rbx
ret
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    11d4 <main+0x64>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
jmp    11c5 <main+0x55>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jne    11f6 <main+0x86>
mov    $0x2,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe1e(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"fork error\0A\00"
@.str.1 = [32 x i8] c"second child, parent pid = %ld\0A\00"
@.str.2 = [15 x i8] c"waitpid error\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = icmp eq i64 %3, 0
br i1 %10, label %11, label %27
11: ; preds = %9
%12 = tail call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = icmp eq i64 %12, 0
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %18
%23 = tail call i32 @sleep(i32 2)
%24 = tail call i64 (...) @getppid()
%25 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i64 %24)
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %9
%28 = tail call i64 @waitpid(i64 %3, i32* null, i32 0)
%29 = icmp eq i64 %28, %3
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27
ret i32 0
}