#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 */
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ vfork();
int main(void) {
  pid_t pid;
  int count = 0;
  pid = vfork();
  count++;
  if (pid < 0) {
    perror("Error in fork");
    exit(1);
  } else if (pid == 0) {
    printf("this is child process, id = %i\n", getpid());
    printf("count = %i\n", count);
    exit(0);
  } else {
    printf("this is parent process, id = %i\n", getpid());
    printf("count = %i\n", count);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1060 <vfork@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    119b <main+0x4b>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <main+0x81>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
jmp    11fa <main+0xaa>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1060 <vfork@plt>
test   %rax,%rax
js     118f <main+0x3f>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
test   %rbx,%rbx
je     11a5 <main+0x55>
lea    0xec7(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xead(%rip),%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe62(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe74(%rip),%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Error in fork\00"
@.str.1 = [32 x i8] c"this is child process, id = %i\0A\00"
@.str.2 = [12 x i8] c"count = %i\0A\00"
@.str.3 = [33 x i8] c"this is parent process, id = %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%4 = call i64 (...) @vfork()
store i64 %4, i64* %2
%5 = load i32, i32* %3
%6 = add nsw i32 %5, 1
store i32 %6, i32* %3
%7 = load i64, i64* %2
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i64, i64* %2
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %3
%19 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %12
%22 = call i32 (...) @getpid()
%23 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %3
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %21
br label %27
27: ; preds = %26
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"Error in fork\00"
@.str.1 = [32 x i8] c"this is child process, id = %i\0A\00"
@.str.2 = [12 x i8] c"count = %i\0A\00"
@.str.3 = [33 x i8] c"this is parent process, id = %i\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @vfork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
%8 = tail call i32 (...) @getpid()
br i1 %7, label %9, label %13
9: ; preds = %6
%10 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 1)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %6
%14 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 1)
ret i32 0
}