#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 EXIT_SUCCESS;
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int vfork();
int main(void) {
  int count = 1;
  int child;
  printf("Before create son, the father's count is : %d\n", count);
  if ((child = vfork()) < 0) {
    printf("%d", child);
    perror("fork error : ");
  } else if (child == 0) {
    printf("This is son, his count is: %d (%p). and his pid is: %d\n", ++count,
           &count, getpid());
  } else {
    printf("After son, This is father, his count is: %d (%p), his pid is: %d\n",
           count, &count, getpid());
    sleep(2);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
call   1060 <vfork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11aa <main+0x5a>
mov    -0xc(%rbp),%esi
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
jmp    1216 <main+0xc6>
cmpl   $0x0,-0xc(%rbp)
jne    11e3 <main+0x93>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%ecx
lea    0xe6d(%rip),%rdi
lea    -0x8(%rbp),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1211 <main+0xc1>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%ecx
lea    0xe7c(%rip),%rdi
lea    -0x8(%rbp),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    1216 <main+0xc6>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x1,0xc(%rsp)
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
mov    $0x1,%esi
call   1040 <printf@plt>
call   1060 <vfork@plt>
test   %eax,%eax
js     11e4 <main+0x94>
je     11aa <main+0x5a>
xor    %eax,%eax
mov    %eax,%ebx
call   1030 <getpid@plt>
mov    %eax,%ecx
lea    0xeeb(%rip),%rdi
mov    $0x1,%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%eax
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    11d5 <main+0x85>
movl   $0x2,0xc(%rsp)
xor    %eax,%eax
mov    %eax,%ebx
call   1030 <getpid@plt>
mov    %eax,%ecx
lea    0xe7c(%rip),%rdi
mov    $0x2,%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%eax
call   1040 <printf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    %eax,%esi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
jmp    11d5 <main+0x85>

LLVM IR (-O0):
@.str = [47 x i8] c"Before create son, the father's count is : %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"fork error : \00"
@.str.3 = [56 x i8] c"This is son, his count is: %d (%p). and his pid is: %d\0A\00"
@.str.4 = [66 x i8] c"After son, This is father, his count is: %d (%p), his pid is: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %2
%4 = load i32, i32* %2
%5 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @vfork()
store i32 %6, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* %3
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %26
12: ; preds = %0
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32* %2, i32 %18)
br label %25
20: ; preds = %12
%21 = load i32, i32* %2
%22 = call i32 (...) @getpid()
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i32 %21, i32* %2, i32 %22)
%24 = call i32 @sleep(i32 2)
br label %25
25: ; preds = %20, %15
br label %26
26: ; preds = %25, %8
%27 = load i32, i32* @EXIT_SUCCESS
ret i32 %27
}

LLVM IR (-O2):
@.str = [47 x i8] c"Before create son, the father's count is : %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"fork error : \00"
@.str.3 = [56 x i8] c"This is son, his count is: %d (%p). and his pid is: %d\0A\00"
@.str.4 = [66 x i8] c"After son, This is father, his count is: %d (%p), his pid is: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%3 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 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 = %0
%7 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%8 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %18
9: ; preds = %0
%10 = icmp eq i32 %4, 0
br i1 %10, label %11, label %14
11: ; preds = %9
store i32 2, i32* %1
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 2, i32* %1, i32 %12)
br label %18
14: ; preds = %9
%15 = call i32 (...) @getpid()
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i32 1, i32* %1, i32 %15)
%17 = call i32 @sleep(i32 2)
br label %18
18: ; preds = %11, %14, %6
%19 = load i32, i32* @EXIT_SUCCESS
ret i32 %19
}