#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 mode_t;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ O_RDWR;
int S_IFCHR;
scalar_t__ access(char *const, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exec_init(char **, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ makedev(int, int);
int /*<<< orphan*/ mknod(char *const, int, int /*<<< orphan*/);
int open(char *const, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int umask(int);
int main(int argc, char **argv, char **envp) {
  char *const console_dev = "/dev/console";
  char *const null_dev = "/dev/null";
  if (getpid() == 1) {
    int fh;
    mode_t old = umask(0000);
    if (access(console_dev, F_OK)) {
      mknod(console_dev, S_IFCHR | 0600, makedev(5, 1));
      fh = open(console_dev, O_RDWR);
      if (fh) {
        close(fh);
      } else {
        (void)dup(0);
        (void)dup(0);
      }
    }
    if (access(null_dev, F_OK))
      mknod(null_dev, S_IFCHR | 0666, makedev(1, 3));
    umask(old);
    exec_init(argv, envp);
  } else {
    fprintf(stderr, "This program is supposed to be run by the kernel.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe3f(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe41(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
cmp    $0x1,%eax
jne    12e6 <main+0x146>
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,-0x30(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
call   10b0 <access@plt>
cmp    $0x0,%rax
je     127c <main+0xdc>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
or     $0x180,%eax
mov    %eax,-0x34(%rbp)
mov    $0x5,%edi
mov    $0x1,%esi
call   10c0 <makedev@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xdcb(%rip),%rdi
call   1090 <mknod@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0xdb6(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1269 <main+0xc9>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
jmp    1277 <main+0xd7>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
jmp    127c <main+0xdc>
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
lea    0xd81(%rip),%rdi
call   10b0 <access@plt>
cmp    $0x0,%rax
je     12cc <main+0x12c>
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
or     $0x1b6,%eax
mov    %eax,-0x38(%rbp)
mov    $0x1,%edi
mov    $0x3,%esi
call   10c0 <makedev@plt>
mov    -0x38(%rbp),%esi
mov    %eax,%edx
lea    0xd46(%rip),%rdi
call   1090 <mknod@plt>
mov    -0x30(%rbp),%edi
call   1080 <umask@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <exec_init@plt>
jmp    12fb <main+0x15b>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd21(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    $0x1,%eax
jne    1231 <main+0x91>
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,%r15d
mov    0x2df2(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe27(%rip),%rdi
call   10b0 <access@plt>
test   %rax,%rax
je     1256 <main+0xb6>
mov    0x2de6(%rip),%rax
mov    $0x180,%ebp
or     (%rax),%ebp
mov    $0x5,%edi
mov    $0x1,%esi
call   10c0 <makedev@plt>
lea    0xdf9(%rip),%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <mknod@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
je     1248 <main+0xa8>
mov    %eax,%edi
call   1060 <close@plt>
jmp    1256 <main+0xb6>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12ab <main+0x10b>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
mov    0x0(%r13),%esi
lea    0xdac(%rip),%rdi
call   10b0 <access@plt>
test   %rax,%rax
je     1298 <main+0xf8>
mov    0x2d5e(%rip),%rax
mov    $0x1b6,%ebx
or     (%rax),%ebx
mov    $0x1,%edi
mov    $0x3,%esi
call   10c0 <makedev@plt>
lea    0xd7e(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1090 <mknod@plt>
mov    %r15d,%edi
call   1080 <umask@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <exec_init@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/console\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@F_OK = global i32 0
@S_IFCHR = global i32 0
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.2 = [51 x i8] c"This program is supposed to be run by the kernel.\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %8
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %9
%12 = call i32 (...) @getpid()
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %50
14: ; preds = %3
%15 = call i32 @umask(i32 0)
store i32 %15, i32* %11
%16 = load i32, i32* @F_OK
%17 = call i64 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %14
%20 = load i32, i32* @S_IFCHR
%21 = or i32 %20, 384
%22 = call i32 @makedev(i32 5, i32 1)
%23 = call i32 @mknod(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i32 %21, i32 %22)
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load i32, i32* %10
%30 = call i32 @close(i32 %29)
br label %34
31: ; preds = %19
%32 = call i32 @dup(i32 0)
%33 = call i32 @dup(i32 0)
br label %34
34: ; preds = %31, %28
br label %35
35: ; preds = %34, %14
%36 = load i32, i32* @F_OK
%37 = call i64 @access(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i32 0, i32 0), i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i32, i32* @S_IFCHR
%41 = or i32 %40, 438
%42 = call i32 @makedev(i32 1, i32 3)
%43 = call i32 @mknod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i32 0, i32 0), i32 %41, i32 %42)
br label %44
44: ; preds = %39, %35
%45 = load i32, i32* %11
%46 = call i32 @umask(i32 %45)
%47 = load i8**, i8*** %6
%48 = load i8**, i8*** %7
%49 = call i32 @exec_init(i8** %47, i8** %48)
br label %53
50: ; preds = %3
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %50, %44
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/console\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@F_OK = global i32 0
@S_IFCHR = global i32 0
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.2 = [51 x i8] c"This program is supposed to be run by the kernel.\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = tail call i32 (...) @getpid()
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %36
6: ; preds = %3
%7 = tail call i32 @umask(i32 0)
%8 = load i32, i32* @F_OK
%9 = tail call i64 @access(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %6
%12 = load i32, i32* @S_IFCHR
%13 = or i32 %12, 384
%14 = tail call i32 @makedev(i32 5, i32 1)
%15 = tail call i32 @mknod(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %11
%20 = tail call i32 @close(i32 %17)
br label %24
21: ; preds = %11
%22 = tail call i32 @dup(i32 0)
%23 = tail call i32 @dup(i32 0)
br label %24
24: ; preds = %19, %21, %6
%25 = load i32, i32* @F_OK
%26 = tail call i64 @access(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32, i32* @S_IFCHR
%30 = or i32 %29, 438
%31 = tail call i32 @makedev(i32 1, i32 3)
%32 = tail call i32 @mknod(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %31)
br label %33
33: ; preds = %28, %24
%34 = tail call i32 @umask(i32 %7)
%35 = tail call i32 @exec_init(i8** %1, i8** %2)
br label %39
36: ; preds = %3
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %36, %33
ret i32 0
}