#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_FAILURE;
int /*<<< orphan*/ SIGCONT;
int /*<<< orphan*/ SIGSTOP;
int /*<<< orphan*/ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getpid();
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void continue_as_child(void) {
  pid_t child = fork();
  int status;
  pid_t ret;
  if (child < 0) {
    fprintf(stderr, "Fork failed.\n");
    exit(EXIT_FAILURE);
  }
  if (child == 0)
    return;
  for (;;) {
    ret = waitpid(child, &status, WUNTRACED);
    if ((ret == child) && (WIFSTOPPED(status))) {
      kill(getpid(), SIGSTOP);
      kill(child, SIGCONT);
    } else {
      break;
    }
  }
  if (WIFEXITED(status)) {
    exit(WEXITSTATUS(status));
  } else if (WIFSIGNALED(status)) {
    kill(getpid(), WTERMSIG(status));
  }
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<continue_as_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11f1 <continue_as_child+0x41>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jne    1202 <continue_as_child+0x52>
add    $0x20,%rsp
pop    %rbp
ret
jmp    1207 <continue_as_child+0x57>
mov    -0x8(%rbp),%rdi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   10a0 <waitpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1270 <continue_as_child+0xc0>
mov    -0xc(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1270 <continue_as_child+0xc0>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rdi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
jmp    1275 <continue_as_child+0xc5>
jmp    127a <continue_as_child+0xca>
jmp    1207 <continue_as_child+0x57>
mov    -0xc(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     129b <continue_as_child+0xeb>
mov    -0xc(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10c0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12cb <continue_as_child+0x11b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   1060 <kill@plt>
jmp    12d0 <continue_as_child+0x120>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<continue_as_child>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     11d9 <continue_as_child+0x29>
jne    11f3 <continue_as_child+0x43>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12a9 <continue_as_child+0xf9>
mov    %rax,%rbx
mov    0x2ddb(%rip),%r15
mov    (%r15),%edx
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
call   10a0 <waitpid@plt>
cmp    %rbx,%rax
jne    1270 <continue_as_child+0xc0>
mov    0x2da7(%rip),%r12
mov    0x2dc8(%rip),%r13
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%edi
call   1070 <WIFSTOPPED@plt>
test   %rax,%rax
je     1270 <continue_as_child+0xc0>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    (%r12),%esi
mov    %rax,%rdi
call   1060 <kill@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1060 <kill@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <waitpid@plt>
cmp    %rbx,%rax
je     1230 <continue_as_child+0x80>
mov    0xc(%rsp),%edi
call   10b0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
jne    12b7 <continue_as_child+0x107>
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     12a9 <continue_as_child+0xf9>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1080 <WTERMSIG@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <kill@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
call   1090 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"Fork failed.\0A\00"
@EXIT_FAILURE = global i32 0
@WUNTRACED = global i32 0
@SIGSTOP = global i32 0
@SIGCONT = global i32 0
define void @continue_as_child() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i64
%4 = call i64 (...) @fork()
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i64, i64* %1
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
ret void
16: ; preds = %12
br label %17
17: ; preds = %36, %16
%18 = load i64, i64* %1
%19 = load i32, i32* @WUNTRACED
%20 = call i64 @waitpid(i64 %18, i32* %2, i32 %19)
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = load i64, i64* %1
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %35
24: ; preds = %17
%25 = load i32, i32* %2
%26 = call i64 @WIFSTOPPED(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = call i64 (...) @getpid()
%30 = load i32, i32* @SIGSTOP
%31 = call i32 @kill(i64 %29, i32 %30)
%32 = load i64, i64* %1
%33 = load i32, i32* @SIGCONT
%34 = call i32 @kill(i64 %32, i32 %33)
br label %36
35: ; preds = %24, %17
br label %37
36: ; preds = %28
br label %17
37: ; preds = %35
%38 = load i32, i32* %2
%39 = call i64 @WIFEXITED(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* %2
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %37
%46 = load i32, i32* %2
%47 = call i64 @WIFSIGNALED(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = call i64 (...) @getpid()
%51 = load i32, i32* %2
%52 = call i32 @WTERMSIG(i32 %51)
%53 = call i32 @kill(i64 %50, i32 %52)
br label %54
54: ; preds = %49, %45
br label %55
55: ; preds = %54
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"Fork failed.\0A\00"
@EXIT_FAILURE = global i32 0
@WUNTRACED = global i32 0
@SIGSTOP = global i32 0
@SIGCONT = global i32 0
define void @continue_as_child() {
%1 = alloca i32
%2 = tail call i64 (...) @fork()
%3 = bitcast i32* %1 to i8*
%4 = icmp slt i64 %2, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = icmp eq i64 %2, 0
br i1 %11, label %16, label %12
12: ; preds = %10
%13 = load i32, i32* @WUNTRACED
%14 = call i64 @waitpid(i64 %2, i32* %1, i32 %13)
%15 = icmp eq i64 %14, %2
br i1 %15, label %17, label %30
16: ; preds = %10
ret void
17: ; preds = %12, %21
%18 = load i32, i32* %1
%19 = call i64 @WIFSTOPPED(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %17
%22 = call i64 (...) @getpid()
%23 = load i32, i32* @SIGSTOP
%24 = call i32 @kill(i64 %22, i32 %23)
%25 = load i32, i32* @SIGCONT
%26 = call i32 @kill(i64 %2, i32 %25)
%27 = load i32, i32* @WUNTRACED
%28 = call i64 @waitpid(i64 %2, i32* %1, i32 %27)
%29 = icmp eq i64 %28, %2
br i1 %29, label %17, label %30
30: ; preds = %17, %21, %12
%31 = load i32, i32* %1
%32 = call i64 @WIFEXITED(i32 %31)
%33 = icmp eq i64 %32, 0
%34 = load i32, i32* %1
br i1 %33, label %38, label %35
35: ; preds = %30
%36 = call i32 @WEXITSTATUS(i32 %34)
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %30
%39 = call i64 @WIFSIGNALED(i32 %34)
%40 = icmp eq i64 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %38
%42 = call i64 (...) @getpid()
%43 = load i32, i32* %1
%44 = call i32 @WTERMSIG(i32 %43)
%45 = call i32 @kill(i64 %42, i32 %44)
br label %46
46: ; preds = %38, %41
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
}