#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 /*<<< orphan*/ print();
int /*<<< orphan*/ printf(char *, int, int);
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);
  usleep(10000);
  while (1) {
    ret = setpgid(0, 0);
    if (ret == -1)
      err_sys("setpgid");
    print();
    usleep(100000);
    ret = setpgid(0, grp);
    if (ret == -1)
      err_sys("setpgid");
    print();
  }
  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    0xe59(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2710,%edi
call   1090 <usleep@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1080 <setpgid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d8 <main+0x68>
lea    0xe3b(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1060 <print@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
mov    -0x10(%rbp),%esi
xor    %edi,%edi
call   1080 <setpgid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    120c <main+0x9c>
lea    0xe07(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1060 <print@plt>
jmp    11b6 <main+0x46>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getpgrp@plt>
mov    %eax,%ebx
lea    0xe73(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <printf@plt>
mov    $0x2710,%edi
call   1090 <usleep@plt>
lea    0xe67(%rip),%r14
jmp    11b7 <main+0x47>
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <print@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1080 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    11cd <main+0x5d>
mov    %r14,%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
call   1060 <print@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
xor    %edi,%edi
mov    %ebx,%esi
call   1080 <setpgid@plt>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x40>
mov    %r14,%rdi
call   1030 <err_sys@plt>
jmp    11b0 <main+0x40>

LLVM IR (-O0):
@.str = [14 x i8] c"child %u: %u\0A\00"
@.str.1 = [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 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i32 @usleep(i32 10000)
br label %11
11: ; preds = %0, %26
%12 = call i32 @setpgid(i32 0, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 (...) @print()
%19 = call i32 @usleep(i32 100000)
%20 = load i32, i32* %4
%21 = call i32 @setpgid(i32 0, i32 %20)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = call i32 (...) @print()
br label %11
}

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