#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 */

/* Variables and functions */
scalar_t__ ECHILD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ _error(char *, int);
int /*<<< orphan*/ _oinit();
scalar_t__ errno;
int getpid();
int open(char const *, int /*<<< orphan*/);
int wait(int /*<<< orphan*/ *);
int main(void) {
  const char console[] = "/dev/console";
  if (getpid() == 1) {
    if (open(console, O_RDONLY) != 0)
      _error("stdin", 1);
    if (open(console, O_WRONLY) != 1)
      _error("stdout", 1);
    if (open(console, O_WRONLY) != 2)
      _error("stderr", 1);
  }
  _oinit();
  for (;;)
    if (wait(NULL) == -1) {
      if (errno == ECHILD)
        _oinit();
      else
        _error("wait", 1);
    }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xe98(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    0xe93(%rip),%al
mov    %al,-0x5(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
cmp    $0x1,%eax
jne    1215 <main+0xc5>
lea    -0x11(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0x0,%eax
je     11b8 <main+0x68>
lea    0xe5f(%rip),%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
lea    -0x11(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0x1,%eax
je     11e4 <main+0x94>
lea    0xe39(%rip),%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
lea    -0x11(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0x2,%eax
je     1210 <main+0xc0>
lea    0xe14(%rip),%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
jmp    1215 <main+0xc5>
mov    $0x0,%al
call   1030 <_oinit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
jne    126a <main+0x11a>
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%rax
jne    1254 <main+0x104>
mov    $0x0,%al
call   1030 <_oinit@plt>
jmp    1265 <main+0x115>
lea    0xdc6(%rip),%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
jmp    126a <main+0x11a>
jmp    121c <main+0xcc>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x656c6f736e6f63,%rax
mov    %rax,0x5(%rsp)
movabs $0x6e6f632f7665642f,%rax
63 6f 6e
mov    %rax,(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
cmp    $0x1,%eax
jne    11ef <main+0x9f>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
test   %eax,%eax
je     11a8 <main+0x58>
lea    0xe6f(%rip),%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
mov    0x2e11(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
cmp    $0x1,%eax
je     11cf <main+0x7f>
lea    0xe4e(%rip),%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
mov    (%rbx),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
cmp    $0x2,%eax
je     11ef <main+0x9f>
lea    0xe35(%rip),%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
xor    %eax,%eax
call   1030 <_oinit@plt>
mov    0x2ddb(%rip),%r15
mov    0x2dcc(%rip),%rbx
lea    0xe16(%rip),%r14
jmp    1220 <main+0xd0>
mov    %r14,%rdi
mov    $0x1,%esi
call   1050 <_error@plt>
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0xd0>
mov    (%r15),%rax
cmp    (%rbx),%rax
jne    120d <main+0xbd>
xor    %eax,%eax
call   1030 <_oinit@plt>
jmp    1220 <main+0xd0>

LLVM IR (-O0):
@__const.main.console = [13 x i8] c"/dev/console\00"
@O_RDONLY = global i32 0
@.str = [6 x i8] c"stdin\00"
@O_WRONLY = global i32 0
@.str.1 = [7 x i8] c"stdout\00"
@.str.2 = [7 x i8] c"stderr\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.3 = [5 x i8] c"wait\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [13 x i8]
store i32 0, i32* %1
%3 = bitcast [13 x i8]* %2 to i8*
%4 = call i32 (...) @getpid()
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %28
6: ; preds = %0
%7 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i32 @_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 1)
br label %13
13: ; preds = %11, %6
%14 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* %14, i32 %15)
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %20
20: ; preds = %18, %13
%21 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* %21, i32 %22)
%24 = icmp ne i32 %23, 2
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %27
27: ; preds = %25, %20
br label %28
28: ; preds = %27, %0
%29 = call i32 (...) @_oinit()
br label %30
30: ; preds = %42, %28
%31 = call i32 @wait(i32* null)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %42
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @ECHILD
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (...) @_oinit()
br label %41
39: ; preds = %33
%40 = call i32 @_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %41
41: ; preds = %39, %37
br label %42
42: ; preds = %41, %30
br label %30
}

LLVM IR (-O2):
@__const.main.console = [13 x i8] c"/dev/console\00"
@O_RDONLY = global i32 0
@.str = [6 x i8] c"stdin\00"
@O_WRONLY = global i32 0
@.str.1 = [7 x i8] c"stdout\00"
@.str.2 = [7 x i8] c"stderr\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.3 = [5 x i8] c"wait\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %23
5: ; preds = %0
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %2, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 1)
br label %11
11: ; preds = %9, %5
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* %2, i32 %12)
%14 = icmp eq i32 %13, 1
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %17
17: ; preds = %15, %11
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %2, i32 %18)
%20 = icmp eq i32 %19, 2
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = tail call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %23
23: ; preds = %17, %21, %0
%24 = tail call i32 (...) @_oinit()
br label %25
25: ; preds = %34, %23
%26 = tail call i32 @wait(i32* null)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @ECHILD
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = tail call i32 (...) @_oinit()
br label %34
34: ; preds = %32, %35, %25
br label %25
35: ; preds = %28
%36 = tail call i32 @_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %34
}