#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);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
lea    0xe5f(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1060 <fopen@plt>
cmpq   $0x0,-0x10(%rbp)
jl     125a <main+0xda>
cmpq   $0x0,-0x10(%rbp)
jne    1201 <main+0x81>
lea    0xe36(%rip),%rdi
lea    0xe39(%rip),%rsi
call   1060 <fopen@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
lea    0xdf8(%rip),%rdi
lea    0xdfb(%rip),%rsi
call   1060 <fopen@plt>
lea    0xde5(%rip),%rdi
lea    0xde8(%rip),%rsi
call   1060 <fopen@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xdd5(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    126d <main+0xed>
lea    0xdca(%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 i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = 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))
%7 = load i64, i64* %2
%8 = icmp sge i64 %7, 0
br i1 %8, label %9, label %26
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 @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 = call i32 (...) @getpid()
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %9
%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 = 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))
%21 = load i32, i32* @stderr
%22 = call i32 (...) @getpid()
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 1)
br label %25
25: ; preds = %18
br label %29
26: ; preds = %0
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %25
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
}