#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 EXIT_SUCCESS;
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  printf("begin...\n");
  pid_t child = fork();
  if (child == -1) {
    printf("进程创建失败!%m\n");
  } else if (child == 0) {
    printf("子进程:pid = %d, ppid = %d\n", getpid(), getppid());
  } else {
    printf("父进程:pid = %d, ppid = %d\n", getpid(), getppid());
  }
  printf("end...\n");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1184 <main+0x44>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e0 <main+0xa0>
cmpl   $0x0,-0x8(%rbp)
jne    11b7 <main+0x77>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11db <main+0x9b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e0 <main+0xa0>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xefd(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     117d <main+0x2d>
cmp    $0xffffffff,%eax
jne    1196 <main+0x46>
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11b8 <main+0x68>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe83(%rip),%rdi
jmp    11ad <main+0x5d>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe89(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"begin...\0A\00"
@.str.1 = [23 x i8] c"\E8\BF\9B\E7\A8\8B\E5\88\9B\E5\BB\BA\E5\A4\B1\E8\B4\A5!%m\0A\00"
@.str.2 = [31 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B:pid = %d, ppid = %d\0A\00"
@.str.3 = [31 x i8] c"\E7\88\B6\E8\BF\9B\E7\A8\8B:pid = %d, ppid = %d\0A\00"
@.str.4 = [8 x i8] c"end...\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %21
9: ; preds = %0
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = call i32 (...) @getpid()
%14 = call i32 (...) @getppid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14)
br label %20
16: ; preds = %9
%17 = call i32 (...) @getpid()
%18 = call i32 (...) @getppid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %18)
br label %20
20: ; preds = %16, %12
br label %21
21: ; preds = %20, %7
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%23 = load i32, i32* @EXIT_SUCCESS
ret i32 %23
}

LLVM IR (-O2):
@.str.1 = [23 x i8] c"\E8\BF\9B\E7\A8\8B\E5\88\9B\E5\BB\BA\E5\A4\B1\E8\B4\A5!%m\0A\00"
@.str.2 = [31 x i8] c"\E5\AD\90\E8\BF\9B\E7\A8\8B:pid = %d, ppid = %d\0A\00"
@.str.3 = [31 x i8] c"\E7\88\B6\E8\BF\9B\E7\A8\8B:pid = %d, ppid = %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [9 x i8] c"begin...\00"
@str.5 = [7 x i8] c"end...\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
switch i32 %2, label %9 [
i32 -1, label %3
i32 0, label %5
]
3: ; preds = %0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %13
5: ; preds = %0
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (...) @getppid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %6, i32 %7)
br label %13
9: ; preds = %0
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (...) @getppid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %10, i32 %11)
br label %13
13: ; preds = %5, %9, %3
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.5, i64 0, i64 0))
%15 = load i32, i32* @EXIT_SUCCESS
ret i32 %15
}