#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_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  printf("fork之前!pid=%d\n", getpid());
  if ((pid = fork()) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day05/fork.c",
              21, "fork", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  if (pid == 0) {
    printf("在子进程中pid=%d,ppid=%d\n", getpid(), getppid());
  } else if (pid > 0) {
    sleep(1000);
    printf("在父进程中pid=%d, cid=%d\n", getpid(), pid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    120b <main+0x8b>
jmp    11be <main+0x3e>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%r9
lea    0xe2e(%rip),%rsi
lea    0xe37(%rip),%rdx
mov    $0x15,%ecx
lea    0xebe(%rip),%r8
call   1050 <fprintf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    123e <main+0xbe>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1271 <main+0xf1>
cmpl   $0x0,-0x8(%rbp)
jle    126c <main+0xec>
mov    $0x3e8,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1271 <main+0xf1>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11cc <main+0x4c>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11f2 <main+0x72>
test   %ebx,%ebx
jle    11ee <main+0x6e>
mov    $0x3e8,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xf14(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
jmp    11e7 <main+0x67>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xed8(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2de7(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe03(%rip),%rsi
lea    0xe0c(%rip),%rdx
lea    0xe98(%rip),%r8
mov    %ebx,%edi
mov    $0x15,%ecx
mov    %rax,%r9
call   1050 <fprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"fork\E4\B9\8B\E5\89\8D!pid=%d\0A\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day05/fork.c\00"
@.str.3 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [31 x i8] c"\E5\9C\A8\E5\AD\90\E8\BF\9B\E7\A8\8B\E4\B8\ADpid=%d,ppid=%d\0A\00"
@.str.5 = [31 x i8] c"\E5\9C\A8\E7\88\B6\E8\BF\9B\E7\A8\8B\E4\B8\ADpid=%d, cid=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %16
7: ; preds = %0
br label %8
8: ; preds = %7
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; No predecessors!
br label %16
16: ; preds = %15, %0
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = call i32 (...) @getpid()
%21 = call i32 (...) @getppid()
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %21)
br label %32
23: ; preds = %16
%24 = load i32, i32* %2
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = call i32 @sleep(i32 1000)
%28 = call i32 (...) @getpid()
%29 = load i32, i32* %2
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %29)
br label %31
31: ; preds = %26, %23
br label %32
32: ; preds = %31, %19
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"fork\E4\B9\8B\E5\89\8D!pid=%d\0A\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day05/fork.c\00"
@.str.3 = [5 x i8] c"fork\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.4 = [31 x i8] c"\E5\9C\A8\E5\AD\90\E8\BF\9B\E7\A8\8B\E4\B8\ADpid=%d,ppid=%d\0A\00"
@.str.5 = [31 x i8] c"\E5\9C\A8\E7\88\B6\E8\BF\9B\E7\A8\8B\E4\B8\ADpid=%d, cid=%d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @fork()
switch i32 %3, label %15 [
i32 -1, label %4
i32 0, label %11
]
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %12, i32 %13)
br label %21
15: ; preds = %0
%16 = icmp sgt i32 %3, 0
br i1 %16, label %17, label %21
17: ; preds = %15
%18 = tail call i32 @sleep(i32 1000)
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %19, i32 %3)
br label %21
21: ; preds = %15, %17, %11
ret i32 0
}