#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 void *pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIGSTOP;
int WIFSTOPPED(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ assert(int);
void *fork();
void *getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ raise(int /*<<< orphan*/);
scalar_t__ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
void *setsid();
int /*<<< orphan*/ signal_setup(int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_child_exit(void *, int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_signal(int /*<<< orphan*/);
void *waitpid(void *, int *, int /*<<< orphan*/);
int test_pgrp_orphan() {
  signal_setup(SIGHUP);
  int ppid = getpid();
  pid_t cpid = fork();
  int status;
  if (cpid == 0) {
    cpid = getpid();
    assert(setsid() == cpid);
    pid_t gcpid = fork();
    if (gcpid == 0) {
      gcpid = getpid();
      assert(setpgid(0, 0) == 0);
      raise(SIGSTOP);
      wait_for_signal(SIGHUP);
      kill(ppid, SIGHUP);
      return 0;
    }
    printf("Child: waiting for the grandchild to stop...\n");
    assert(waitpid(gcpid, &status, WUNTRACED) == gcpid);
    assert(WIFSTOPPED(status));
    return 0;
  }
  printf("Parent: waiting for the child to exit...\n");
  wait_for_child_exit(cpid, 0);
  printf("Parent: waiting for a signal from the grandchild...\n");
  wait_for_signal(SIGHUP);
  return 0;
}


x86 assembly (clang, -O0):
<test_pgrp_orphan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   10b0 <signal_setup@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12eb <test_pgrp_orphan+0x11b>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    -0x10(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    129d <test_pgrp_orphan+0xcd>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x20(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   10e0 <setpgid@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10c0 <wait_for_signal@plt>
mov    -0x8(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
movl   $0x0,-0x4(%rbp)
jmp    1323 <test_pgrp_orphan+0x153>
lea    0xd5c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rsi
call   10d0 <waitpid@plt>
cmp    -0x20(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x14(%rbp),%edi
call   10a0 <WIFSTOPPED@plt>
mov    %eax,%edi
call   1080 <assert@plt>
movl   $0x0,-0x4(%rbp)
jmp    1323 <test_pgrp_orphan+0x153>
lea    0xd3c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1040 <wait_for_child_exit@plt>
lea    0xd4f(%rip),%rdi
call   1060 <printf@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10c0 <wait_for_signal@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_pgrp_orphan>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e10(%rip),%r15
mov    (%r15),%edi
call   10b0 <signal_setup@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     1230 <test_pgrp_orphan+0x60>
mov    %rax,%rbx
lea    0xdf8(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <wait_for_child_exit@plt>
lea    0xe0b(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%edi
call   10c0 <wait_for_signal@plt>
jmp    12df <test_pgrp_orphan+0x10f>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <setsid@plt>
xor    %edi,%edi
cmp    %rbx,%rax
sete   %dil
call   1080 <assert@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     12a0 <test_pgrp_orphan+0xd0>
mov    %rax,%rbx
lea    0xdf8(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   10d0 <waitpid@plt>
xor    %edi,%edi
cmp    %rbx,%rax
sete   %dil
call   1080 <assert@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFSTOPPED@plt>
mov    %eax,%edi
call   1080 <assert@plt>
jmp    12df <test_pgrp_orphan+0x10f>
xor    %eax,%eax
call   1060 <getpid@plt>
xor    %edi,%edi
xor    %esi,%esi
call   10e0 <setpgid@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1080 <assert@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
mov    (%r15),%edi
call   10c0 <wait_for_signal@plt>
mov    (%r15),%esi
mov    %r14d,%edi
call   1090 <kill@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIGHUP = global i32 0
@SIGSTOP = global i32 0
@.str = [46 x i8] c"Child: waiting for the grandchild to stop...\0A\00"
@WUNTRACED = global i32 0
@.str.1 = [42 x i8] c"Parent: waiting for the child to exit...\0A\00"
@.str.2 = [53 x i8] c"Parent: waiting for a signal from the grandchild...\0A\00"
define i32 @test_pgrp_orphan() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = load i32, i32* @SIGHUP
%7 = call i32 @signal_setup(i32 %6)
%8 = call i8* (...) @getpid()
%9 = ptrtoint i8* %8 to i32
store i32 %9, i32* %2
%10 = call i8* (...) @fork()
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %48
13: ; preds = %0
%14 = call i8* (...) @getpid()
store i8* %14, i8** %3
%15 = call i8* (...) @setsid()
%16 = load i8*, i8** %3
%17 = icmp eq i8* %15, %16
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = call i8* (...) @fork()
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %36
23: ; preds = %13
%24 = call i8* (...) @getpid()
store i8* %24, i8** %5
%25 = call i64 @setpgid(i32 0, i32 0)
%26 = icmp eq i64 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i32, i32* @SIGSTOP
%30 = call i32 @raise(i32 %29)
%31 = load i32, i32* @SIGHUP
%32 = call i32 @wait_for_signal(i32 %31)
%33 = load i32, i32* %2
%34 = load i32, i32* @SIGHUP
%35 = call i32 @kill(i32 %33, i32 %34)
store i32 0, i32* %1
br label %55
36: ; preds = %13
%37 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%38 = load i8*, i8** %5
%39 = load i32, i32* @WUNTRACED
%40 = call i8* @waitpid(i8* %38, i32* %4, i32 %39)
%41 = load i8*, i8** %5
%42 = icmp eq i8* %40, %41
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i32, i32* %4
%46 = call i32 @WIFSTOPPED(i32 %45)
%47 = call i32 @assert(i32 %46)
store i32 0, i32* %1
br label %55
48: ; preds = %0
%49 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%50 = load i8*, i8** %3
%51 = call i32 @wait_for_child_exit(i8* %50, i32 0)
%52 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* @SIGHUP
%54 = call i32 @wait_for_signal(i32 %53)
store i32 0, i32* %1
br label %55
55: ; preds = %48, %36, %23
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@SIGHUP = global i32 0
@SIGSTOP = global i32 0
@WUNTRACED = global i32 0
@str = [41 x i8] c"Parent: waiting for the child to exit...\00"
@str.3 = [52 x i8] c"Parent: waiting for a signal from the grandchild...\00"
@str.4 = [45 x i8] c"Child: waiting for the grandchild to stop...\00"
define i32 @test_pgrp_orphan() {
%1 = alloca i32
%2 = load i32, i32* @SIGHUP
%3 = tail call i32 @signal_setup(i32 %2)
%4 = tail call i8* (...) @getpid()
%5 = ptrtoint i8* %4 to i64
%6 = trunc i64 %5 to i32
%7 = tail call i8* (...) @fork()
%8 = bitcast i32* %1 to i8*
%9 = icmp eq i8* %7, null
br i1 %9, label %10, label %40
10: ; preds = %0
%11 = tail call i8* (...) @getpid()
%12 = tail call i8* (...) @setsid()
%13 = icmp eq i8* %12, %11
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i8* (...) @fork()
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %30
18: ; preds = %10
%19 = tail call i8* (...) @getpid()
%20 = tail call i64 @setpgid(i32 0, i32 0)
%21 = icmp eq i64 %20, 0
%22 = zext i1 %21 to i32
%23 = tail call i32 @assert(i32 %22)
%24 = load i32, i32* @SIGSTOP
%25 = tail call i32 @raise(i32 %24)
%26 = load i32, i32* @SIGHUP
%27 = tail call i32 @wait_for_signal(i32 %26)
%28 = load i32, i32* @SIGHUP
%29 = tail call i32 @kill(i32 %6, i32 %28)
br label %46
30: ; preds = %10
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.4, i64 0, i64 0))
%32 = load i32, i32* @WUNTRACED
%33 = call i8* @waitpid(i8* %16, i32* %1, i32 %32)
%34 = icmp eq i8* %33, %16
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i32, i32* %1
%38 = call i32 @WIFSTOPPED(i32 %37)
%39 = call i32 @assert(i32 %38)
br label %46
40: ; preds = %0
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%42 = tail call i32 @wait_for_child_exit(i8* %7, i32 0)
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.3, i64 0, i64 0))
%44 = load i32, i32* @SIGHUP
%45 = tail call i32 @wait_for_signal(i32 %44)
br label %46
46: ; preds = %18, %30, %40
ret i32 0
}