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

/* Variables and functions */
scalar_t__ fork();
int getpgid(int);
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  pid_t mypid;
  if ((mypid = fork()) == 0) {
    printf("\n Child process ! \n");
    printf("\n PID      = %d \n Parent PID = %d \n Group PID = %d \n", getpid(),
           getppid(), getpgid(getpid()));
    if (1)
      sleep(10);
  } else if (mypid > 0) {
    printf("\n Parent process ! \n");
    printf("\n PID      = %d \n Parent PID = %d \n Group PID = %d \n", getpid(),
           getppid(), getpgid(getpid()));
  } else {
    perror("\n Fork() failed ");
  }
  printf("i\n Process %d finished !", getpid());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f0 <main+0x80>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1060 <getpgid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
jmp    1257 <main+0xe7>
cmpq   $0x0,-0x18(%rbp)
jle    1246 <main+0xd6>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1060 <getpgid@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1252 <main+0xe2>
lea    0xe11(%rip),%rdi
call   1050 <perror@plt>
jmp    1257 <main+0xe7>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11d7 <main+0x57>
jle    1241 <main+0xc1>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1070 <getpgid@plt>
lea    0xe38(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1221 <main+0xa1>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1070 <getpgid@plt>
lea    0xdf6(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe17(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xded(%rip),%rdi
call   1060 <perror@plt>
jmp    1221 <main+0xa1>

LLVM IR (-O0):
@.str = [20 x i8] c"\0A Child process ! \0A\00"
@.str.1 = [53 x i8] c"\0A PID = %d \0A Parent PID = %d \0A Group PID = %d \0A\00"
@.str.2 = [21 x i8] c"\0A Parent process ! \0A\00"
@.str.3 = [17 x i8] c"\0A Fork() failed \00"
@.str.4 = [25 x i8] c"i\0A Process %d finished !\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (...) @getpid()
%12 = call i32 (...) @getppid()
%13 = call i32 (...) @getpid()
%14 = call i32 @getpgid(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12, i32 %14)
%16 = call i32 @sleep(i32 10)
br label %30
17: ; preds = %2
%18 = load i64, i64* %6
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 (...) @getpid()
%23 = call i32 (...) @getppid()
%24 = call i32 (...) @getpid()
%25 = call i32 @getpgid(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23, i32 %25)
br label %29
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
br label %30
30: ; preds = %29, %9
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %31)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [53 x i8] c"\0A PID = %d \0A Parent PID = %d \0A Group PID = %d \0A\00"
@.str.3 = [17 x i8] c"\0A Fork() failed \00"
@.str.4 = [25 x i8] c"i\0A Process %d finished !\00"
@str = [20 x i8] c"\0A Parent process ! \00"
@str.5 = [19 x i8] c"\0A Child process ! \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @getppid()
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @getpgid(i32 %9)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8, i32 %10)
%12 = tail call i32 @sleep(i32 10)
br label %24
13: ; preds = %2
%14 = icmp sgt i64 %3, 0
br i1 %14, label %15, label %22
15: ; preds = %13
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (...) @getppid()
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 @getpgid(i32 %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18, i32 %20)
br label %24
22: ; preds = %13
%23 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %15, %22, %5
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %25)
ret i32 0
}