#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpgrp();
int getpid();
int /*<<< orphan*/ printf(char *, int, int);
int setpgrp();
int /*<<< orphan*/ stdout;
char *ttyname(int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int i, pgrp, child;
  if ((child = fork()) != 0) {
    printf("parent process; pid: %d grp: %d\n", getpid(), getpgrp());
    fflush(stdout);
    waitpid(child, 0, 0);
    return 0;
  }
  FILE *f = fopen("log", "w+t");
  fprintf(f, "pid: %d grp: %d\n", getpid(), getpgrp());
  pgrp = setpgrp();
  fprintf(f, "successfull: %d grp: %d\n", pgrp, getpgrp());
  for (i = 0; i < 3; ++i)
    fprintf(f, "%s\n", ttyname(i));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     120a <main+0x6a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x10(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <waitpid@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d2 <main+0x132>
lea    0xe10(%rip),%rdi
lea    0xe0d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   10b0 <setpgrp@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1090 <getpgrp@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    12cb <main+0x12b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <ttyname@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rdx
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    128e <main+0xee>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     11ee <main+0x4e>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getpgrp@plt>
lea    0xe39(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
jmp    12a0 <main+0x100>
lea    0xe2c(%rip),%rdi
lea    0xe29(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getpgrp@plt>
lea    0xdf5(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   10b0 <setpgrp@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getpgrp@plt>
lea    0xde9(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <ttyname@plt>
lea    0xde6(%rip),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <ttyname@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <ttyname@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"parent process; pid: %d grp: %d\0A\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"log\00"
@.str.2 = [4 x i8] c"w+t\00"
@.str.3 = [17 x i8] c"pid: %d grp: %d\0A\00"
@.str.4 = [25 x i8] c"successfull: %d grp: %d\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32 (...) @fork()
store i32 %6, i32* %4
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %0
%9 = call i32 (...) @getpid()
%10 = call i32 (...) @getpgrp()
%11 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = load i32, i32* @stdout
%13 = call i32 @fflush(i32 %12)
%14 = load i32, i32* %4
%15 = call i32 @waitpid(i32 %14, i32 0, i32 0)
store i32 0, i32* %1
br label %39
16: ; preds = %0
%17 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getpgrp()
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 (...) @setpgrp()
store i32 %22, i32* %3
%23 = load i32*, i32** %5
%24 = load i32, i32* %3
%25 = call i32 (...) @getpgrp()
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %24, i32 %25)
store i32 0, i32* %2
br label %27
27: ; preds = %35, %16
%28 = load i32, i32* %2
%29 = icmp slt i32 %28, 3
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i32*, i32** %5
%32 = load i32, i32* %2
%33 = call i8* @ttyname(i32 %32)
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %30
%36 = load i32, i32* %2
%37 = add nsw i32 %36, 1
store i32 %37, i32* %2
br label %27
38: ; preds = %27
store i32 0, i32* %1
br label %39
39: ; preds = %38, %8
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@.str = [33 x i8] c"parent process; pid: %d grp: %d\0A\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"log\00"
@.str.2 = [4 x i8] c"w+t\00"
@.str.3 = [17 x i8] c"pid: %d grp: %d\0A\00"
@.str.4 = [25 x i8] c"successfull: %d grp: %d\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
br i1 %2, label %10, label %3
3: ; preds = %0
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (...) @getpgrp()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %5)
%7 = load i32, i32* @stdout
%8 = tail call i32 @fflush(i32 %7)
%9 = tail call i32 @waitpid(i32 %1, i32 0, i32 0)
br label %24
10: ; preds = %0
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (...) @getpgrp()
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %12, i32 %13)
%15 = tail call i32 (...) @setpgrp()
%16 = tail call i32 (...) @getpgrp()
%17 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %15, i32 %16)
%18 = tail call i8* @ttyname(i32 0)
%19 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %18)
%20 = tail call i8* @ttyname(i32 1)
%21 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%22 = tail call i8* @ttyname(i32 2)
%23 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %10, %3
ret i32 0
}