#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 fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  pid_t child_pid;
  pid_t main_pid = getpid();
  printf("主程序的进程号是：%d.\n", main_pid);
  system("ps");
  printf("\n");
  child_pid = fork();
  if (child_pid != 0) {
    printf("这是父进程，进程号是：%d.\n", getpid());
    printf("子进程的进程号是：%d.\n", child_pid);
    system("ps");
    printf("\n");
  } else {
    printf("这是子进程，进程号是：%d.\n", getpid());
  }
  system("ps");
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea8(%rip),%rdi
call   1040 <system@plt>
lea    0xee3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11e6 <main+0xa6>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe52(%rip),%rdi
call   1040 <system@plt>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11fd <main+0xbd>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe1c(%rip),%rdi
call   1040 <system@plt>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeb1(%rip),%rdi
call   1050 <system@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %ebx,%ebx
je     11ca <main+0x7a>
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xea0(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe67(%rip),%rdi
call   1050 <system@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11da <main+0x8a>
lea    0xe98(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3f(%rip),%rdi
call   1050 <system@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [32 x i8] c"\E4\B8\BB\E7\A8\8B\E5\BA\8F\E7\9A\84\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%d.\0A\00"
@.str.1 = [3 x i8] c"ps\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [38 x i8] c"\E8\BF\99\E6\98\AF\E7\88\B6\E8\BF\9B\E7\A8\8B\EF\BC\8C\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%d.\0A\00"
@.str.4 = [32 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E7\9A\84\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%d.\0A\00"
@.str.5 = [38 x i8] c"\E8\BF\99\E6\98\AF\E5\AD\90\E8\BF\9B\E7\A8\8B\EF\BC\8C\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%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
%8 = call i32 (...) @getpid()
store i32 %8, i32* %7
%9 = load i32, i32* %7
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 (...) @fork()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %6
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %26
23: ; preds = %2
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %16
%27 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"\E4\B8\BB\E7\A8\8B\E5\BA\8F\E7\9A\84\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%d.\0A\00"
@.str.1 = [3 x i8] c"ps\00"
@.str.3 = [38 x i8] c"\E8\BF\99\E6\98\AF\E7\88\B6\E8\BF\9B\E7\A8\8B\EF\BC\8C\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%d.\0A\00"
@.str.4 = [32 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B\E7\9A\84\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%d.\0A\00"
@.str.5 = [38 x i8] c"\E8\BF\99\E6\98\AF\E5\AD\90\E8\BF\9B\E7\A8\8B\EF\BC\8C\E8\BF\9B\E7\A8\8B\E5\8F\B7\E6\98\AF\EF\BC\9A%d.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @putchar(i32 10)
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
%9 = tail call i32 (...) @getpid()
br i1 %8, label %15, label %10
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %7)
%13 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @putchar(i32 10)
br label %17
15: ; preds = %2
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %9)
br label %17
17: ; preds = %15, %10
%18 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @putchar(i32 10)
ret i32 0
}