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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  int count = 0;
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    printf("执行fork时出错！退出\n");
    exit(-1);
  }
  if (pid == 0)
    printf("我是子进程！,count: %d, pid: %d\n", count, getpid());
  else
    printf("我是父进程!, count: %d, pid: %d\n", ++count, getpid());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1184 <main+0x44>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11b4 <main+0x74>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11da <main+0x9a>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1190 <main+0x40>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     117a <main+0x2a>
lea    0xeb4(%rip),%rdi
mov    $0x1,%esi
jmp    1183 <main+0x33>
lea    0xe7f(%rip),%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xeb6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"\E6\89\A7\E8\A1\8Cfork\E6\97\B6\E5\87\BA\E9\94\99\EF\BC\81\E9\80\80\E5\87\BA\0A\00"
@.str.1 = [39 x i8] c"\E6\88\91\E6\98\AF\E5\AD\90\E8\BF\9B\E7\A8\8B\EF\BC\81,count: %d, pid: %d\0A\00"
@.str.2 = [38 x i8] c"\E6\88\91\E6\98\AF\E7\88\B6\E8\BF\9B\E7\A8\8B!, count: %d, pid: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load i64, i64* %3
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* %2
%15 = call i32 (...) @getpid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15)
br label %22
17: ; preds = %10
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
store i32 %19, i32* %2
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
br label %22
22: ; preds = %17, %13
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [39 x i8] c"\E6\88\91\E6\98\AF\E5\AD\90\E8\BF\9B\E7\A8\8B\EF\BC\81,count: %d, pid: %d\0A\00"
@.str.2 = [38 x i8] c"\E6\88\91\E6\98\AF\E7\88\B6\E8\BF\9B\E7\A8\8B!, count: %d, pid: %d\0A\00"
@str = [29 x i8] c"\E6\89\A7\E8\A1\8Cfork\E6\97\B6\E5\87\BA\E9\94\99\EF\BC\81\E9\80\80\E5\87\BA\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
%8 = tail call i32 (...) @getpid()
br i1 %7, label %9, label %11
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %8)
br label %13
11: ; preds = %6
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %8)
br label %13
13: ; preds = %11, %9
ret i32 0
}