#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);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int vfork();
int main(int argc, char *argv[]) {
  int count = 1;
  int child;
  printf("before create son, parent count is %d!\n", count);
  child = vfork();
  if (child < 0) {
    printf("error in vfork!\n");
    exit(1);
  } else if (0 == child) {
    printf("I am child process, the count is %d, my process ID %d ,my parent "
           "process ID is %d\n",
           ++count, getpid(), getppid());
  } else {
    printf("after son, I am parent process, the count is %d, my process ID %d "
           ",the child is %d\n",
           count, getpid(), child);
  }
  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)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
call   1050 <vfork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a8 <main+0x58>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jne    11eb <main+0x9b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120e <main+0xbe>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
mov    -0x18(%rbp),%ecx
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1213 <main+0xc3>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe96(%rip),%rdi
xor    %eax,%eax
mov    $0x1,%esi
call   1050 <printf@plt>
call   1060 <vfork@plt>
test   %eax,%eax
js     11cb <main+0x6b>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
test   %ebp,%ebp
je     11a2 <main+0x42>
lea    0xee6(%rip),%rdi
xor    %eax,%eax
mov    $0x1,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
jmp    11bd <main+0x5d>
xor    %eax,%eax
mov    %eax,%ebp
call   1070 <getppid@plt>
lea    0xe76(%rip),%rdi
mov    $0x2,%esi
mov    %ebx,%edx
mov    %eax,%ecx
mov    %ebp,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xefd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"before create son, parent count is %d!\0A\00"
@.str.1 = [17 x i8] c"error in vfork!\0A\00"
@.str.2 = [83 x i8] c"I am child process, the count is %d, my process ID %d ,my parent process ID is %d\0A\00"
@.str.3 = [84 x i8] c"after son, I am parent process, the count is %d, my process ID %d ,the child is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
%8 = load i32, i32* %6
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @vfork()
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* %7
%18 = icmp eq i32 0, %17
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
store i32 %21, i32* %6
%22 = call i32 (...) @getpid()
%23 = call i32 (...) @getppid()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22, i32 %23)
br label %30
25: ; preds = %16
%26 = load i32, i32* %6
%27 = call i32 (...) @getpid()
%28 = load i32, i32* %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27, i32 %28)
br label %30
30: ; preds = %25, %19
br label %31
31: ; preds = %30
ret i32 0
}

LLVM IR (-O2):
@.str = [40 x i8] c"before create son, parent count is %d!\0A\00"
@.str.2 = [83 x i8] c"I am child process, the count is %d, my process ID %d ,my parent process ID is %d\0A\00"
@.str.3 = [84 x i8] c"after son, I am parent process, the count is %d, my process ID %d ,the child is %d\0A\00"
@str = [16 x i8] c"error in vfork!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 1)
%4 = call i32 @vfork()
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = icmp eq i32 %4, 0
%11 = call i32 (...) @getpid()
br i1 %10, label %12, label %15
12: ; preds = %9
%13 = call i32 (...) @getppid()
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([83 x i8], [83 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %11, i32 %13)
br label %17
15: ; preds = %9
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([84 x i8], [84 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %11, i32 %4)
br label %17
17: ; preds = %12, %15
ret i32 0
}