#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*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, char *args[]) {
  pid_t pid = fork();
  if (pid < 0) {
    fprintf(stderr, "fork error:%s\n", strerror(errno));
    exit(1);
  } else if (pid > 0) {
    wait(0);
    while (1) {
      sleep(1);
    }
    printf("parent pid:%d, ppid:%d,  child pid:%d\n", getpid(), getppid(), pid);
  } else {
    printf("child pid:%d, ppid:%d\n", getpid(), getppid());
    exit(1);
  }
  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   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11f2 <main+0x62>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe1d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1213 <main+0x83>
xor    %edi,%edi
call   10a0 <wait@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1204 <main+0x74>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11bc <main+0x2c>
je     11ee <main+0x5e>
xor    %edi,%edi
call   10a0 <wait@plt>
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11b0 <main+0x20>
mov    0x2e1d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe26(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe0a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"fork error:%s\0A\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"child pid:%d, ppid:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i64, i64* %6
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = call i32 @wait(i32 0)
br label %21
21: ; preds = %19, %21
%22 = call i32 @sleep(i32 1)
br label %21
23: ; preds = %16
%24 = call i32 (...) @getpid()
%25 = call i32 (...) @getppid()
%26 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
%27 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"fork error:%s\0A\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"child pid:%d, ppid:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = icmp eq i64 %3, 0
br i1 %12, label %17, label %13
13: ; preds = %11
%14 = tail call i32 @wait(i32 0)
br label %15
15: ; preds = %15, %13
%16 = tail call i32 @sleep(i32 1)
br label %15
17: ; preds = %11
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (...) @getppid()
%20 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = tail call i32 @exit(i32 1)
unreachable
}