#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 atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usage();
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  int sleep_val;
  if (argc != 2) {
    usage();
    exit(0);
  }
  sleep_val = atoi(argv[1]);
  if ((pid = fork()) < 0) {
    printf("fork error.\n");
    exit(0);
  } else if (pid == 0) {
    if ((pid = fork()) < 0) {
      printf("fork error.\n");
      exit(0);
    } else if (pid > 0) {
      exit(0);
    }
    sleep(sleep_val);
    printf("second child, parent pid = %ld\n", (long)getppid());
    exit(0);
  }
  if (waitpid(pid, NULL, 0) != pid) {
    printf("waitpid error.\n");
  }
  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)
cmpl   $0x2,-0x8(%rbp)
je     11ae <main+0x2e>
mov    $0x0,%al
call   1040 <usage@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11e8 <main+0x68>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    125b <main+0xdb>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    121d <main+0x9d>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    122f <main+0xaf>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1234 <main+0xb4>
mov    -0x1c(%rbp),%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %rax,%rsi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1260 <main+0xe0>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     1287 <main+0x107>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11da <main+0x4a>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11f4 <main+0x64>
je     11e8 <main+0x58>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %rbx,%rax
je     11d1 <main+0x41>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1050 <usage@plt>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    1207 <main+0x77>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jne    1200 <main+0x70>
mov    %ebp,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xde2(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

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