#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 */

/* Variables and functions */
scalar_t__ SIGCONT;
scalar_t__ SIGSTOP;
scalar_t__ SIGTERM;
int /*<<< orphan*/ WIFSTOPPED(int);
int WNOHANG;
scalar_t__ WSTOPSIG(int);
int WUNTRACED;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ kill(int, scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int *, int);
int main(int argc, char **argv) {
  int r;
  r = fork();
  if (r == 0) {
    while (1) {
    }
    _exit(0);
  } else {
    int child = r;
    int status;
    sleep(1);
    kill(child, SIGSTOP);
    sleep(1);
    status = 0;
    r = waitpid(-1, &status, WUNTRACED | WNOHANG);
    if (r != child)
      return 1;
    if (!WIFSTOPPED(status))
      return 1;
    if (WSTOPSIG(status) != SIGSTOP)
      return 1;
    sleep(1);
    kill(child, SIGCONT);
    sleep(1);
    kill(child, SIGTERM);
  }
  printf("ok\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)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a4 <main+0x34>
jmp    119f <main+0x2f>
jmp    119f <main+0x2f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x18(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%rsi
call   1040 <kill@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
movl   $0x0,-0x1c(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1212 <main+0xa2>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x12e>
mov    -0x1c(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%eax
jne    122f <main+0xbf>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x12e>
mov    -0x1c(%rbp),%edi
call   1080 <WSTOPSIG@plt>
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%rax
je     1253 <main+0xe3>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x12e>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x18(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%rsi
call   1040 <kill@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x18(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%rsi
call   1040 <kill@plt>
lea    0xd6e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1250 <main+0xe0>
mov    %eax,%ebx
mov    $0x1,%ebp
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2e21(%rip),%r14
mov    (%r14),%rsi
mov    %ebx,%edi
call   1040 <kill@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
movl   $0x0,0xc(%rsp)
mov    0x2e06(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1060 <waitpid@plt>
cmp    %ebx,%eax
jne    123f <main+0xcf>
mov    0xc(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %eax,%eax
je     123f <main+0xcf>
mov    0xc(%rsp),%edi
call   1080 <WSTOPSIG@plt>
cmp    (%r14),%rax
jne    123f <main+0xcf>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rsi
mov    %ebx,%edi
call   1040 <kill@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%rsi
mov    %ebx,%edi
call   1040 <kill@plt>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
nopw   0x0(%rax,%rax,1)
jmp    1250 <main+0xe0>

LLVM IR (-O0):
@SIGSTOP = global i64 0
@WUNTRACED = global i32 0
@WNOHANG = global i32 0
@SIGCONT = global i64 0
@SIGTERM = global i64 0
@.str = [4 x i8] c"ok\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
br label %13
13: ; preds = %12, %13
br label %13
14: ; preds = %2
%15 = load i32, i32* %6
store i32 %15, i32* %7
%16 = call i32 @sleep(i32 1)
%17 = load i32, i32* %7
%18 = load i64, i64* @SIGSTOP
%19 = call i32 @kill(i32 %17, i64 %18)
%20 = call i32 @sleep(i32 1)
store i32 0, i32* %8
%21 = load i32, i32* @WUNTRACED
%22 = load i32, i32* @WNOHANG
%23 = or i32 %21, %22
%24 = call i32 @waitpid(i32 -1, i32* %8, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %14
store i32 1, i32* %3
br label %51
29: ; preds = %14
%30 = load i32, i32* %8
%31 = call i32 @WIFSTOPPED(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %29
store i32 1, i32* %3
br label %51
34: ; preds = %29
%35 = load i32, i32* %8
%36 = call i64 @WSTOPSIG(i32 %35)
%37 = load i64, i64* @SIGSTOP
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 1, i32* %3
br label %51
40: ; preds = %34
%41 = call i32 @sleep(i32 1)
%42 = load i32, i32* %7
%43 = load i64, i64* @SIGCONT
%44 = call i32 @kill(i32 %42, i64 %43)
%45 = call i32 @sleep(i32 1)
%46 = load i32, i32* %7
%47 = load i64, i64* @SIGTERM
%48 = call i32 @kill(i32 %46, i64 %47)
br label %49
49: ; preds = %40
%50 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %51
51: ; preds = %49, %39, %33, %28
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@SIGSTOP = global i64 0
@WUNTRACED = global i32 0
@WNOHANG = global i32 0
@SIGCONT = global i64 0
@SIGTERM = global i64 0
@str = [3 x i8] c"ok\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %2, %6
br label %6
7: ; preds = %2
%8 = bitcast i32* %3 to i8*
%9 = tail call i32 @sleep(i32 1)
%10 = load i64, i64* @SIGSTOP
%11 = tail call i32 @kill(i32 %4, i64 %10)
%12 = tail call i32 @sleep(i32 1)
store i32 0, i32* %3
%13 = load i32, i32* @WUNTRACED
%14 = load i32, i32* @WNOHANG
%15 = or i32 %14, %13
%16 = call i32 @waitpid(i32 -1, i32* %3, i32 %15)
%17 = icmp eq i32 %16, %4
br i1 %17, label %18, label %27
18: ; preds = %7
%19 = load i32, i32* %3
%20 = call i32 @WIFSTOPPED(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %18
%23 = load i32, i32* %3
%24 = call i64 @WSTOPSIG(i32 %23)
%25 = load i64, i64* @SIGSTOP
%26 = icmp eq i64 %24, %25
br i1 %26, label %28, label %27
27: ; preds = %7, %18, %22
br label %36
28: ; preds = %22
%29 = call i32 @sleep(i32 1)
%30 = load i64, i64* @SIGCONT
%31 = call i32 @kill(i32 %4, i64 %30)
%32 = call i32 @sleep(i32 1)
%33 = load i64, i64* @SIGTERM
%34 = call i32 @kill(i32 %4, i64 %33)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %27, %28
%37 = phi i32 [ 0, %28 ], [ 1, %27 ]
ret i32 %37
}