#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ err_sys(char *);
int getpgrp();
int getpid();
int getppid();
int /*<<< orphan*/ print();
int /*<<< orphan*/ printf(char *, ...);
int setpgid(int /*<<< orphan*/, int);
int /*<<< orphan*/ usleep(int);
int main() {
  int ret = 0;
  pid_t pid = getpid();
  pid_t grp = getpgrp();
  printf("child %u: %u\n", pid, grp);
  while (1) {
    if (getppid() == 1) {
      printf("adopt to init\n");
      break;
    }
    usleep(10000);
  }
  while (1) {
    ret = setpgid(0, 0);
    if (ret == -1)
      err_sys("setpgid");
    print();
    ret = setpgid(0, grp);
    if (ret == -1)
      err_sys("setpgid");
    print();
    usleep(100000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1090 <getppid@plt>
cmp    $0x1,%eax
jne    11e1 <main+0x61>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11f0 <main+0x70>
mov    $0x2710,%edi
call   10a0 <usleep@plt>
jmp    11be <main+0x3e>
jmp    11f5 <main+0x75>
xor    %esi,%esi
mov    %esi,%edi
call   1080 <setpgid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1217 <main+0x97>
lea    0xe0b(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1060 <print@plt>
mov    -0x10(%rbp),%esi
xor    %edi,%edi
call   1080 <setpgid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1241 <main+0xc1>
lea    0xde1(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1060 <print@plt>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
jmp    11f5 <main+0x75>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getpgrp@plt>
mov    %eax,%ebx
lea    0xe53(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <getppid@plt>
cmp    $0x1,%eax
je     11d8 <main+0x48>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
jmp    11c0 <main+0x30>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
lea    0xe23(%rip),%r14
jmp    1201 <main+0x71>
nopl   (%rax)
xor    %eax,%eax
call   1070 <print@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1090 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    1217 <main+0x87>
mov    %r14,%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
call   1070 <print@plt>
xor    %edi,%edi
mov    %ebx,%esi
call   1090 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x60>
mov    %r14,%rdi
call   1040 <err_sys@plt>
jmp    11f0 <main+0x60>

LLVM IR (-O0):
@.str = [14 x i8] c"child %u: %u\0A\00"
@.str.1 = [15 x i8] c"adopt to init\0A\00"
@.str.2 = [8 x i8] c"setpgid\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%5 = call i32 (...) @getpid()
store i32 %5, i32* %3
%6 = call i32 (...) @getpgrp()
store i32 %6, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
br label %10
10: ; preds = %0, %15
%11 = call i32 (...) @getppid()
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
15: ; preds = %10
%16 = call i32 @usleep(i32 10000)
br label %10
17: ; preds = %13
br label %18
18: ; preds = %17, %32
%19 = call i32 @setpgid(i32 0, i32 0)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = call i32 (...) @print()
%26 = load i32, i32* %4
%27 = call i32 @setpgid(i32 0, i32 %26)
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = call i32 (...) @print()
%34 = call i32 @usleep(i32 100000)
br label %18
}

LLVM IR (-O2):
@.str = [14 x i8] c"child %u: %u\0A\00"
@.str.2 = [8 x i8] c"setpgid\00"
@str = [14 x i8] c"adopt to init\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @getpgrp()
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%4 = tail call i32 (...) @getppid()
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %8, %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %12
8: ; preds = %0, %8
%9 = tail call i32 @usleep(i32 10000)
%10 = tail call i32 (...) @getppid()
%11 = icmp eq i32 %10, 1
br i1 %11, label %6, label %8
12: ; preds = %23, %6
%13 = tail call i32 @setpgid(i32 0, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i32 (...) @print()
%19 = tail call i32 @setpgid(i32 0, i32 %2)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = tail call i32 (...) @print()
%25 = tail call i32 @usleep(i32 100000)
br label %12
}