#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__ fork();
int getpgrp();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  if (fork() == 0) {
    printf("child in group:%d\n", getpgrp());
    setsid();
    puts("after setsid()");
    printf("child in group:%d\n", getpgrp());
    for (;;) {
      write(1, ".", 1);
      sleep(1);
    }
  } else {
    printf("parent in group:%d\n", getpgrp());
    for (;;) {
      write(1, "$", 1);
      sleep(1);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    11ef <main+0x7f>
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,%esi
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,%esi
lea    0xe38(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edx
lea    0xe49(%rip),%rsi
mov    %edx,%edi
call   1040 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11cd <main+0x5d>
mov    $0x0,%al
call   1070 <getpgrp@plt>
mov    %eax,%esi
lea    0xe25(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edx
lea    0xe28(%rip),%rsi
mov    %edx,%edi
call   1040 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1204 <main+0x94>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getpgrp@plt>
test   %rbx,%rbx
je     11be <main+0x4e>
lea    0xe96(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe9c(%rip),%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11a0 <main+0x30>
lea    0xe3b(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <getpgrp@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe28(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1200 <main+0x90>

LLVM IR (-O0):
@.str = [19 x i8] c"child in group:%d\0A\00"
@.str.1 = [15 x i8] c"after setsid()\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [20 x i8] c"parent in group:%d\0A\00"
@.str.4 = [2 x i8] c"$\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %14
4: ; preds = %0
%5 = call i32 (...) @getpgrp()
%6 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 (...) @setsid()
%8 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (...) @getpgrp()
%10 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %11, %4
%12 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%13 = call i32 @sleep(i32 1)
br label %11
14: ; preds = %0
%15 = call i32 (...) @getpgrp()
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %17, %14
%18 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%19 = call i32 @sleep(i32 1)
br label %17
}

LLVM IR (-O2):
@.str = [19 x i8] c"child in group:%d\0A\00"
@.str.1 = [15 x i8] c"after setsid()\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [20 x i8] c"parent in group:%d\0A\00"
@.str.4 = [2 x i8] c"$\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
%3 = tail call i32 (...) @getpgrp()
br i1 %2, label %4, label %13
4: ; preds = %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3)
%6 = tail call i32 (...) @setsid()
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 (...) @getpgrp()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %10, %4
%11 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%12 = tail call i32 @sleep(i32 1)
br label %10
13: ; preds = %0
%14 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %3)
br label %15
15: ; preds = %15, %13
%16 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%17 = tail call i32 @sleep(i32 1)
br label %15
}