#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 */
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  pid_t fpid;
  int count = 0;
  fpid = fork();
  if (fpid < 0)
    printf("error in fork!");
  else if (fpid == 0) {
    printf("i am the child process, my process id is %d\n", getpid());
    printf("我是爹的儿子\n");
    count++;
  } else {
    printf("i am the parent process, my process id is %d\n", getpid());
    printf("我是孩子他爹\n");
    count++;
  }
  printf("统计结果是: %d\n", count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    116f <main+0x3f>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e0 <main+0xb0>
cmpq   $0x0,-0x10(%rbp)
jne    11ad <main+0x7d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11db <main+0xab>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e0 <main+0xb0>
mov    -0x14(%rbp),%esi
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1179 <main+0x39>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     1189 <main+0x49>
lea    0xed5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf08(%rip),%rdi
jmp    11a0 <main+0x60>
lea    0xe80(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11aa <main+0x6a>
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xef2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
lea    0xeb9(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"error in fork!\00"
@.str.1 = [45 x i8] c"i am the child process, my process id is %d\0A\00"
@.str.2 = [20 x i8] c"\E6\88\91\E6\98\AF\E7\88\B9\E7\9A\84\E5\84\BF\E5\AD\90\0A\00"
@.str.3 = [46 x i8] c"i am the parent process, my process id is %d\0A\00"
@.str.4 = [20 x i8] c"\E6\88\91\E6\98\AF\E5\AD\A9\E5\AD\90\E4\BB\96\E7\88\B9\0A\00"
@.str.5 = [21 x i8] c"\E7\BB\9F\E8\AE\A1\E7\BB\93\E6\9E\9C\E6\98\AF: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %25
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
store i32 %17, i32* %3
br label %24
18: ; preds = %9
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %24
24: ; preds = %18, %12
br label %25
25: ; preds = %24, %7
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %26)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"error in fork!\00"
@.str.1 = [45 x i8] c"i am the child process, my process id is %d\0A\00"
@.str.3 = [46 x i8] c"i am the parent process, my process id is %d\0A\00"
@.str.5 = [21 x i8] c"\E7\BB\9F\E8\AE\A1\E7\BB\93\E6\9E\9C\E6\98\AF: %d\0A\00"
@str = [19 x i8] c"\E6\88\91\E6\98\AF\E5\AD\A9\E5\AD\90\E4\BB\96\E7\88\B9\00"
@str.6 = [19 x i8] c"\E6\88\91\E6\98\AF\E7\88\B9\E7\9A\84\E5\84\BF\E5\AD\90\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %14
5: ; preds = %0
%6 = icmp eq i64 %1, 0
%7 = tail call i32 (...) @getpid()
br i1 %6, label %8, label %11
8: ; preds = %5
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %14
11: ; preds = %5
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %14
14: ; preds = %8, %11, %3
%15 = phi i32 [ 0, %3 ], [ 1, %8 ], [ 1, %11 ]
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %15)
ret i32 0
}