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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setpgrp();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  int life_universe_and_everything;
  int fd;
  switch (fork()) {
  case -1:
    perror(argv[0]);
    exit(1);
    break;
  case 0:
    chdir("/");
    umask(0);
    setpgrp();
    setsid();
    switch (pid = fork()) {
    case -1:
      break;
    case 0:
      close(0);
      open("/dev/null", 0);
      close(1);
      if (open("/dev/null", O_WRONLY) < 0) {
        perror("/dev/null");
        exit(1);
      }
      fflush(stdout);
      close(2);
      dup(1);
      for (fd = 3; fd < 256; fd++) {
        close(fd);
      }
      while (1) {
        life_universe_and_everything = 42 * 2;
      }
    default:
      printf("%d\n", pid);
      exit(0);
      break;
    }
    break;
  default:
    exit(0);
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     1200 <main+0x40>
jmp    11f0 <main+0x30>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     1216 <main+0x56>
jmp    133d <main+0x17d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xde3(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
mov    %edi,-0x2c(%rbp)
call   1080 <umask@plt>
mov    -0x2c(%rbp),%eax
mov    %al,-0x25(%rbp)
call   10d0 <setpgrp@plt>
mov    -0x25(%rbp),%al
call   1070 <setsid@plt>
mov    -0x25(%rbp),%al
call   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     126d <main+0xad>
jmp    125d <main+0x9d>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     1272 <main+0xb2>
jmp    1322 <main+0x162>
jmp    1338 <main+0x178>
xor    %edi,%edi
call   1060 <close@plt>
lea    0xd82(%rip),%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
lea    0xd61(%rip),%rdi
call   10a0 <open@plt>
cmp    $0x0,%eax
jge    12c5 <main+0x105>
lea    0xd4c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1050 <dup@plt>
movl   $0x3,-0x1c(%rbp)
cmpl   $0x100,-0x1c(%rbp)
jge    1311 <main+0x151>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12ee <main+0x12e>
jmp    1316 <main+0x156>
movl   $0x54,-0x18(%rbp)
jmp    1316 <main+0x156>
mov    -0x14(%rbp),%esi
lea    0xce0(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    1344 <main+0x184>
xor    %edi,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    1296 <main+0xd6>
lea    0xe26(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   10d0 <setpgrp@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
cmp    $0xffffffff,%eax
je     1292 <main+0xd2>
test   %eax,%eax
jne    12a0 <main+0xe0>
xor    %edi,%edi
call   1060 <close@plt>
lea    0xde8(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <open@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12b5 <main+0xf5>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1050 <dup@plt>
mov    $0x3,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    $0x100,%ebx
jne    1270 <main+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
jmp    1290 <main+0xd0>
xor    %eax,%eax
pop    %rbx
ret
cmp    $0xffffffff,%eax
jne    12ae <main+0xee>
mov    (%rbx),%rdi
jmp    12bc <main+0xfc>
lea    0xd65(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd46(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@stdout = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
switch i32 %9, label %54 [
i32 -1, label %10
i32 0, label %16
]
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @perror(i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @umask(i32 0)
%19 = call i32 (...) @setpgrp()
%20 = call i32 (...) @setsid()
%21 = call i32 (...) @fork()
store i32 %21, i32* %6
switch i32 %21, label %49 [
i32 -1, label %22
i32 0, label %23
]
22: ; preds = %16
br label %53
23: ; preds = %16
%24 = call i32 @close(i32 0)
%25 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 0)
%26 = call i32 @close(i32 1)
%27 = load i32, i32* @O_WRONLY
%28 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
%36 = call i32 @close(i32 2)
%37 = call i32 @dup(i32 1)
store i32 3, i32* %8
br label %38
38: ; preds = %44, %33
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 256
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %41
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %38
47: ; preds = %38
br label %48
48: ; preds = %47, %48
store i32 84, i32* %7
br label %48
49: ; preds = %16
%50 = load i32, i32* %6
%51 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %22
br label %56
54: ; preds = %2
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %53
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@stdout = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %38 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @perror(i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @umask(i32 0)
%11 = tail call i32 (...) @setpgrp()
%12 = tail call i32 (...) @setsid()
%13 = tail call i32 (...) @fork()
switch i32 %13, label %35 [
i32 -1, label %40
i32 0, label %14
]
14: ; preds = %8
%15 = tail call i32 @close(i32 0)
%16 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 0)
%17 = tail call i32 @close(i32 1)
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i32, i32* @stdout
%26 = tail call i32 @fflush(i32 %25)
%27 = tail call i32 @close(i32 2)
%28 = tail call i32 @dup(i32 1)
br label %29
29: ; preds = %24, %29
%30 = phi i32 [ 3, %24 ], [ %32, %29 ]
%31 = tail call i32 @close(i32 %30)
%32 = add nuw nsw i32 %30, 1
%33 = icmp eq i32 %32, 256
br i1 %33, label %34, label %29
34: ; preds = %29, %34
br label %34
35: ; preds = %8
%36 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %2
%39 = tail call i32 @exit(i32 0)
unreachable
40: ; preds = %8
ret i32 0
}