#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*/ abort();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main() {
  pid_t childpid;
  int retval;
  int status;
  childpid = fork();
  fopen("/dev/null", "r");
  if (childpid >= 0) {
    if (childpid == 0) {
      fopen("/dev/null", "r");
      fprintf(stderr, "CHILD: pid=%d\n", getpid());
      abort();
      sleep(1);
    } else {
      fopen("/dev/null", "r");
      fopen("/dev/null", "r");
      fprintf(stderr, "PARENT: pid=%d\n", getpid());
      sleep(1);
    }
  } else {
    perror("fork");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
lea    0xe66(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1060 <fopen@plt>
cmpq   $0x0,-0x8(%rbp)
jl     1253 <main+0xd3>
cmpq   $0x0,-0x8(%rbp)
jne    11fa <main+0x7a>
lea    0xe3d(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1060 <fopen@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xe1e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0xdff(%rip),%rdi
lea    0xe02(%rip),%rsi
call   1060 <fopen@plt>
lea    0xdec(%rip),%rdi
lea    0xdef(%rip),%rsi
call   1060 <fopen@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xddc(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1266 <main+0xe6>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
lea    0xe6e(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1060 <fopen@plt>
test   %rbx,%rbx
js     11fc <main+0x7c>
lea    0xe56(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1060 <fopen@plt>
test   %rbx,%rbx
je     120f <main+0x8f>
lea    0xe3e(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1060 <fopen@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe36(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"CHILD: pid=%d\0A\00"
@.str.3 = [16 x i8] c"PARENT: pid=%d\0A\00"
@.str.4 = [5 x i8] c"fork\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = call i64 (...) @fork()
store i64 %4, i64* %1
%5 = call i32 @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = load i64, i64* %1
%7 = icmp sge i64 %6, 0
br i1 %7, label %8, label %25
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = call i32 @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = call i32 (...) @getpid()
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %8
%18 = call i32 @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = call i32 (...) @getpid()
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = call i32 @sleep(i32 1)
br label %24
24: ; preds = %17
br label %28
25: ; preds = %0
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"CHILD: pid=%d\0A\00"
@.str.3 = [16 x i8] c"PARENT: pid=%d\0A\00"
@.str.4 = [5 x i8] c"fork\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = tail call i32 @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp sgt i64 %1, -1
br i1 %3, label %4, label %18
4: ; preds = %0
%5 = icmp eq i64 %1, 0
%6 = tail call i32 @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br i1 %5, label %7, label %12
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %4
%13 = tail call i32 @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = tail call i32 @sleep(i32 1)
ret i32 0
18: ; preds = %0
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%20 = tail call i32 @exit(i32 0)
unreachable
}