#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);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid, pid2;
  char buf[20];
  printf("Enter input (n = next, other input terminates):");
  fgets(buf, 20, stdin);
  while (buf[0] == 'n') {
    pid = fork();
    if (pid < 0) {
      perror("Fork:");
      exit(1);
    }
    if (pid == 0) {
      pid2 = fork();
      if (pid2 > 0)
        exit(0);
      else if (pid2 < 0)
        perror("child1 fork()");
      else
        sleep(2);
      exit(0);
    }
    wait(NULL);
    printf("Enter input (n = next, other input terminates):");
    fgets(buf, 20, stdin);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    $0x14,%esi
call   1040 <fgets@plt>
movsbl -0x30(%rbp),%eax
cmp    $0x6e,%eax
jne    1266 <main+0xf6>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11db <main+0x6b>
lea    0xe64(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1235 <main+0xc5>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1203 <main+0x93>
xor    %edi,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jge    121f <main+0xaf>
lea    0xe21(%rip),%rdi
call   1050 <perror@plt>
jmp    1229 <main+0xb9>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    122e <main+0xbe>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
lea    0xdbb(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    $0x14,%esi
call   1040 <fgets@plt>
jmp    11a2 <main+0x32>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e5c(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rdi
mov    $0x14,%esi
call   1040 <fgets@plt>
cmpb   $0x6e,(%rsp)
jne    11e3 <main+0x73>
lea    0xe57(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11ef <main+0x7f>
je     1205 <main+0x95>
xor    %edi,%edi
call   1080 <wait@plt>
mov    %r14,%rdi
call   1030 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1040 <fgets@plt>
cmpb   $0x6e,(%rsp)
je     11b0 <main+0x40>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jle    1218 <main+0xa8>
xor    %edi,%edi
call   1060 <exit@plt>
jns    122d <main+0xbd>
lea    0xe15(%rip),%rdi
call   1050 <perror@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [48 x i8] c"Enter input (n = next, other input terminates):\00"
@stdin = global i32 0
@.str.1 = [6 x i8] c"Fork:\00"
@.str.2 = [14 x i8] c"child1 fork()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca [20 x i8]
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @stdin
%8 = call i32 @fgets(i8* %6, i32 20, i32 %7)
br label %9
9: ; preds = %40, %0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 110
br i1 %13, label %14, label %46
14: ; preds = %9
%15 = call i64 (...) @fork()
store i64 %15, i64* %2
%16 = load i64, i64* %2
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = load i64, i64* %2
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = call i64 (...) @fork()
store i64 %25, i64* %3
%26 = load i64, i64* %3
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %24
%31 = load i64, i64* %3
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %37
35: ; preds = %30
%36 = call i32 @sleep(i32 2)
br label %37
37: ; preds = %35, %33
br label %38
38: ; preds = %37
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %21
%41 = call i32 @wait(i32* null)
%42 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%43 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%44 = load i32, i32* @stdin
%45 = call i32 @fgets(i8* %43, i32 20, i32 %44)
br label %9
46: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@.str = [48 x i8] c"Enter input (n = next, other input terminates):\00"
@stdin = global i32 0
@.str.1 = [6 x i8] c"Fork:\00"
@.str.2 = [14 x i8] c"child1 fork()\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @stdin
%5 = call i32 @fgets(i8* %2, i32 20, i32 %4)
%6 = load i8, i8* %2
%7 = icmp eq i8 %6, 110
br i1 %7, label %8, label %36
8: ; preds = %0, %29
%9 = call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = icmp eq i64 %9, 0
br i1 %15, label %16, label %29
16: ; preds = %14
%17 = call i64 (...) @fork()
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %16
%22 = icmp slt i64 %17, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %27
25: ; preds = %21
%26 = call i32 @sleep(i32 2)
br label %27
27: ; preds = %23, %25
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %14
%30 = call i32 @wait(i32* null)
%31 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* @stdin
%33 = call i32 @fgets(i8* %2, i32 20, i32 %32)
%34 = load i8, i8* %2
%35 = icmp eq i8 %34, 110
br i1 %35, label %8, label %36
36: ; preds = %29, %0
ret i32 0
}