#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);
int WIFEXITED(int);
int WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ use_fork_and_threads(int);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void *do_fork(int level) {
  pid_t pid = fork();
  if (pid == 0) {
    fprintf(stderr, "child process\n");
    if (level > 1) {
      use_fork_and_threads(level - 1);
    }
    exit(0);
  } else {
    int status = -1;
    pid_t waited_pid;
    do {
      waited_pid = waitpid(pid, &status, 0);
    } while (waited_pid != pid);
    if (status != 0) {
      fprintf(stderr, "child %d exited non-zero: %x\n", pid, status);
      fprintf(stderr, "signalled: %d, signal: %d, exited: %d, exit: %d\n",
              WIFSIGNALED(status), WTERMSIG(status), WIFEXITED(status),
              WEXITSTATUS(status));
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<do_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e4 <do_fork+0x54>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x1,-0x4(%rbp)
jle    11dd <do_fork+0x4d>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1030 <use_fork_and_threads@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11eb <do_fork+0x5b>
cmpl   $0x0,-0x14(%rbp)
je     1287 <do_fork+0xf7>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    %eax,%r9d
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    128c <do_fork+0xfc>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_fork>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     124d <do_fork+0xbd>
mov    %rax,%rbx
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x4(%rsp),%rbp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    %rbx,%rax
jne    11c0 <do_fork+0x30>
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
je     1240 <do_fork+0xb0>
mov    0x2dff(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe24(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%r14d
mov    0x4(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
mov    %eax,%r15d
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    %eax,%ebp
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe02(%rip),%rsi
mov    %r14d,%edi
mov    %r15d,%edx
mov    %ebx,%ecx
mov    %ebp,%r8d
mov    %eax,%r9d
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    $0x2,%ebp
jl     1273 <do_fork+0xe3>
add    $0xffffffff,%ebp
mov    %ebp,%edi
call   1030 <use_fork_and_threads@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"child process\0A\00"
@.str.1 = [30 x i8] c"child %d exited non-zero: %x\0A\00"
@.str.2 = [49 x i8] c"signalled: %d, signal: %d, exited: %d, exit: %d\0A\00"
define i8* @do_fork(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
store i32 %0, i32* %2
%6 = call i64 (...) @fork()
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* %2
%16 = sub nsw i32 %15, 1
%17 = call i32 @use_fork_and_threads(i32 %16)
br label %18
18: ; preds = %14, %9
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %1
store i32 -1, i32* %4
br label %21
21: ; preds = %24, %20
%22 = load i64, i64* %3
%23 = call i64 @waitpid(i64 %22, i32* %4, i32 0)
store i64 %23, i64* %5
br label %24
24: ; preds = %21
%25 = load i64, i64* %5
%26 = load i64, i64* %3
%27 = icmp ne i64 %25, %26
br i1 %27, label %21, label %28
28: ; preds = %24
%29 = load i32, i32* %4
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %46
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = load i64, i64* %3
%34 = load i32, i32* %4
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 %33, i32 %34)
%36 = load i32, i32* @stderr
%37 = load i32, i32* %4
%38 = call i32 @WIFSIGNALED(i32 %37)
%39 = load i32, i32* %4
%40 = call i32 @WTERMSIG(i32 %39)
%41 = load i32, i32* %4
%42 = call i32 @WIFEXITED(i32 %41)
%43 = load i32, i32* %4
%44 = call i32 @WEXITSTATUS(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %38, i32 %40, i32 %42, i32 %44)
br label %46
46: ; preds = %31, %28
br label %47
47: ; preds = %46
ret i8* null
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"child process\0A\00"
@.str.1 = [30 x i8] c"child %d exited non-zero: %x\0A\00"
@.str.2 = [49 x i8] c"signalled: %d, signal: %d, exited: %d, exit: %d\0A\00"
define noalias i8* @do_fork(i32 %0) {
%2 = alloca i32
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = add nsw i32 %0, -1
%11 = tail call i32 @use_fork_and_threads(i32 %10)
br label %12
12: ; preds = %9, %5
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %1
%15 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
br label %16
16: ; preds = %16, %14
%17 = call i64 @waitpid(i64 %3, i32* %2, i32 0)
%18 = icmp eq i64 %17, %3
br i1 %18, label %19, label %16
19: ; preds = %16
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, 0
br i1 %21, label %35, label %22
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 %3, i32 %20)
%25 = load i32, i32* @stderr
%26 = load i32, i32* %2
%27 = call i32 @WIFSIGNALED(i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @WTERMSIG(i32 %28)
%30 = load i32, i32* %2
%31 = call i32 @WIFEXITED(i32 %30)
%32 = load i32, i32* %2
%33 = call i32 @WEXITSTATUS(i32 %32)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %29, i32 %31, i32 %33)
br label %35
35: ; preds = %22, %19
ret i8* null
}