#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 */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpgid(int /*<<< orphan*/);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int vfork();
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char const *argv[]) {
  printf("In parent process(A) before fork \n");
  printf("A : pid %d pgid %d ppid %d\n", getpid(), getpgid(0), getppid());
  int x;
  int a = 5;
  int b = 10;
  printf("a is %d\n", a);
  printf("b is %d\n", b);
  if ((x = vfork()) == 0) {
    printf("In child process(B)\n");
    printf("B : pid %d pgid %d ppid %d\n", getpid(), getpgid(0), getppid());
    a = 10;
    b = 5;
    printf("a is %d\n", a);
    printf("b is %d\n", b);
    printf("exiting child process\n");
    exit(0);
  } else {
    wait(NULL);
    printf("back in parent process\n");
    printf("a is %d\n", a);
    printf("b is %d\n", b);
  }
  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)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5,-0x18(%rbp)
movl   $0xa,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
call   1050 <vfork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    128a <main+0x11a>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
xor    %edi,%edi
call   1060 <getpgid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xa,-0x18(%rbp)
movl   $0x5,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xd8c(%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   %r15
push   %r14
push   %rbx
push   %rax
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
mov    %r14d,%ebx
mov    %r14d,%eax
call   1040 <getpid@plt>
mov    %eax,%r15d
mov    %r14d,%edi
call   1070 <getpgid@plt>
mov    %eax,%ebp
mov    %r14d,%eax
call   1080 <getppid@plt>
lea    0xe43(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
mov    %r14d,%eax
call   1050 <printf@plt>
lea    0xe49(%rip),%rdi
mov    $0x5,%esi
mov    %r14d,%eax
call   1050 <printf@plt>
lea    0xe3e(%rip),%rdi
mov    $0xa,%esi
mov    %r14d,%eax
call   1050 <printf@plt>
call   1060 <vfork@plt>
test   %eax,%eax
je     1245 <main+0xc5>
xor    %ebp,%ebp
mov    %ebp,%edi
call   10a0 <wait@plt>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe03(%rip),%rdi
mov    $0x5,%esi
mov    %ebp,%eax
call   1050 <printf@plt>
lea    0xdf9(%rip),%rdi
mov    $0xa,%esi
mov    %ebp,%eax
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
call   1040 <getpid@plt>
mov    %eax,%r15d
mov    %r14d,%edi
call   1070 <getpgid@plt>
mov    %eax,%ebp
mov    %r14d,%eax
call   1080 <getppid@plt>
lea    0xdb6(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
mov    %r14d,%eax
call   1050 <printf@plt>
lea    0xd8e(%rip),%rdi
mov    $0xa,%esi
mov    %r14d,%eax
call   1050 <printf@plt>
lea    0xd83(%rip),%rdi
mov    $0x5,%esi
mov    %r14d,%eax
call   1050 <printf@plt>
lea    0xde1(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"In parent process(A) before fork \0A\00"
@.str.1 = [28 x i8] c"A : pid %d pgid %d ppid %d\0A\00"
@.str.2 = [9 x i8] c"a is %d\0A\00"
@.str.3 = [9 x i8] c"b is %d\0A\00"
@.str.4 = [21 x i8] c"In child process(B)\0A\00"
@.str.5 = [28 x i8] c"B : pid %d pgid %d ppid %d\0A\00"
@.str.6 = [23 x i8] c"exiting child process\0A\00"
@.str.7 = [24 x i8] c"back in parent process\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @getpid()
%11 = call i32 @getpgid(i32 0)
%12 = call i32 (...) @getppid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %11, i32 %12)
store i32 5, i32* %7
store i32 10, i32* %8
%14 = load i32, i32* %7
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %8
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 @vfork()
store i32 %18, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 (...) @getpid()
%23 = call i32 @getpgid(i32 0)
%24 = call i32 (...) @getppid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 %23, i32 %24)
store i32 10, i32* %7
store i32 5, i32* %8
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %2
%33 = call i32 @wait(i32* null)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %32
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [28 x i8] c"A : pid %d pgid %d ppid %d\0A\00"
@.str.2 = [9 x i8] c"a is %d\0A\00"
@.str.3 = [9 x i8] c"b is %d\0A\00"
@.str.5 = [28 x i8] c"B : pid %d pgid %d ppid %d\0A\00"
@str = [34 x i8] c"In parent process(A) before fork \00"
@str.8 = [23 x i8] c"back in parent process\00"
@str.9 = [20 x i8] c"In child process(B)\00"
@str.10 = [22 x i8] c"exiting child process\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%4 = call i32 (...) @getpid()
%5 = call i32 @getpgid(i32 0)
%6 = call i32 (...) @getppid()
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %5, i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 5)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 10)
%10 = call i32 @vfork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %2
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%14 = call i32 (...) @getpid()
%15 = call i32 @getpgid(i32 0)
%16 = call i32 (...) @getppid()
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %14, i32 %15, i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 10)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 5)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %2
%23 = call i32 @wait(i32* null)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 5)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 10)
ret i32 0
}