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

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int, ...);
int main() {
  pid_t pid;
  int x = 1;
  pid = fork();
  printf("当前进程的ID为：%d   pid的值为：%d\n", getpid(), pid);
  if (pid == 0) {
    printf("child : x = %d, pid = %d, 父进程的id:%d\n", ++x, getpid(),
           getppid());
    exit(0);
  }
  printf("parent: x = %d, pid = %d\n", --x, getpid());
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11ce <main+0x7e>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    %eax,%ecx
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe96(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
test   %ebp,%ebp
jne    11a7 <main+0x57>
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe9f(%rip),%rdi
mov    $0x2,%esi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xeae(%rip),%rdi
xor    %esi,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"\E5\BD\93\E5\89\8D\E8\BF\9B\E7\A8\8B\E7\9A\84ID\E4\B8\BA\EF\BC\9A%d pid\E7\9A\84\E5\80\BC\E4\B8\BA\EF\BC\9A%d\0A\00"
@.str.1 = [45 x i8] c"child : x = %d, pid = %d, \E7\88\B6\E8\BF\9B\E7\A8\8B\E7\9A\84id:%d\0A\00"
@.str.2 = [26 x i8] c"parent: x = %d, pid = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %3
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = call i32 (...) @getpid()
%6 = load i32, i32* %2
%7 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %0
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 1
store i32 %12, i32* %3
%13 = call i32 (...) @getpid()
%14 = call i32 (...) @getppid()
%15 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %13, i32 %14)
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %0
%18 = load i32, i32* %3
%19 = add nsw i32 %18, -1
store i32 %19, i32* %3
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [47 x i8] c"\E5\BD\93\E5\89\8D\E8\BF\9B\E7\A8\8B\E7\9A\84ID\E4\B8\BA\EF\BC\9A%d pid\E7\9A\84\E5\80\BC\E4\B8\BA\EF\BC\9A%d\0A\00"
@.str.1 = [45 x i8] c"child : x = %d, pid = %d, \E7\88\B6\E8\BF\9B\E7\A8\8B\E7\9A\84id:%d\0A\00"
@.str.2 = [26 x i8] c"parent: x = %d, pid = %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %1)
%4 = icmp eq i32 %1, 0
%5 = tail call i32 (...) @getpid()
br i1 %4, label %6, label %10
6: ; preds = %0
%7 = tail call i32 (...) @getppid()
%8 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %5, i32 %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %5)
%12 = tail call i32 @exit(i32 0)
unreachable
}