#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int EX_OSERR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int daemonmode(void) {
  pid_t pid;
  int status;
  switch (pid = fork()) {
  case -1: {
    perror(NULL);
    exit(EX_OSERR);
  }
  case 0: {
    if (setsid() == -1) {
      perror(NULL);
      exit(EX_OSERR);
    }
    switch (pid = fork()) {
    case -1: {
      perror(NULL);
      exit(EX_OSERR);
    }
    case 0: {
      umask(0);
      if (freopen("/dev/null", "r", stdin) == NULL) {
        perror("/dev/null");
      }
      if (freopen("/dev/console", "w", stdout) == NULL) {
        perror("/dev/console");
      }
      if (freopen("/dev/console", "w", stderr) == NULL) {
        perror("/dev/console");
      }
      return (0);
    }
    default: {
      exit(EXIT_SUCCESS);
    }
    }
  }
  default: {
    waitpid(pid, &status, 0);
    exit(status);
  }
  }
}


x86 assembly (clang, -O0):
<daemonmode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11a5 <daemonmode+0x35>
jmp    1195 <daemonmode+0x25>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     11bc <daemonmode+0x4c>
jmp    12da <daemonmode+0x16a>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11e3 <daemonmode+0x73>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     1210 <daemonmode+0xa0>
jmp    1200 <daemonmode+0x90>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     1227 <daemonmode+0xb7>
jmp    12cc <daemonmode+0x15c>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1050 <umask@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
lea    0xdc2(%rip),%rdi
lea    0xdc5(%rip),%rsi
call   1030 <freopen@plt>
cmp    $0x0,%rax
jne    1260 <daemonmode+0xf0>
lea    0xda5(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
lea    0xd9c(%rip),%rdi
lea    0xda2(%rip),%rsi
call   1030 <freopen@plt>
cmp    $0x0,%rax
jne    1292 <daemonmode+0x122>
lea    0xd7f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
lea    0xd6a(%rip),%rdi
lea    0xd70(%rip),%rsi
call   1030 <freopen@plt>
cmp    $0x0,%rax
jne    12c4 <daemonmode+0x154>
lea    0xd4d(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    -0x8(%rbp),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<daemonmode>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    1237 <daemonmode+0xc7>
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
je     1258 <daemonmode+0xe8>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    1253 <daemonmode+0xe3>
xor    %edi,%edi
call   1050 <umask@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
lea    0xe4a(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1030 <freopen@plt>
test   %rax,%rax
je     120d <daemonmode+0x9d>
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
lea    0xe35(%rip),%rdi
lea    0xe3b(%rip),%rsi
call   1030 <freopen@plt>
test   %rax,%rax
je     121b <daemonmode+0xab>
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
lea    0xe14(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1030 <freopen@plt>
test   %rax,%rax
je     1229 <daemonmode+0xb9>
xor    %eax,%eax
pop    %rcx
ret
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
jmp    11c7 <daemonmode+0x57>
lea    0xdea(%rip),%rdi
call   1070 <perror@plt>
jmp    11e8 <daemonmode+0x78>
lea    0xddc(%rip),%rdi
call   1070 <perror@plt>
jmp    1209 <daemonmode+0x99>
cmp    $0xffffffff,%eax
je     1258 <daemonmode+0xe8>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1080 <exit@plt>
cmp    $0xffffffff,%eax
jne    126d <daemonmode+0xfd>
xor    %edi,%edi
call   1070 <perror@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@EX_OSERR = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [13 x i8] c"/dev/console\00"
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @daemonmode() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
store i32 %3, i32* %1
switch i32 %3, label %44 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %0
%5 = call i32 @perror(i8* null)
%6 = load i32, i32* @EX_OSERR
%7 = call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = call i32 (...) @setsid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 @perror(i8* null)
%13 = load i32, i32* @EX_OSERR
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = call i32 (...) @fork()
store i32 %16, i32* %1
switch i32 %16, label %41 [
i32 -1, label %17
i32 0, label %21
]
17: ; preds = %15
%18 = call i32 @perror(i8* null)
%19 = load i32, i32* @EX_OSERR
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = call i32 @umask(i32 0)
%23 = load i32, i32* @stdin
%24 = call i32* @freopen(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), i32 %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* @stdout
%30 = call i32* @freopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = load i32, i32* @stderr
%36 = call i32* @freopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
ret i32 0
41: ; preds = %15
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %0
%45 = load i32, i32* %1
%46 = call i32 @waitpid(i32 %45, i32* %2, i32 0)
%47 = load i32, i32* %2
%48 = call i32 @exit(i32 %47)
unreachable
}

LLVM IR (-O2):
@EX_OSERR = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [13 x i8] c"/dev/console\00"
@.str.3 = [2 x i8] c"w\00"
@stdout = global i32 0
@stderr = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @daemonmode() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %44 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* null)
%6 = load i32, i32* @EX_OSERR
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 (...) @setsid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = tail call i32 @perror(i8* null)
%13 = load i32, i32* @EX_OSERR
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = tail call i32 (...) @fork()
switch i32 %16, label %41 [
i32 -1, label %17
i32 0, label %21
]
17: ; preds = %15
%18 = tail call i32 @perror(i8* null)
%19 = load i32, i32* @EX_OSERR
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = tail call i32 @umask(i32 0)
%23 = load i32, i32* @stdin
%24 = tail call i32* @freopen(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), i32 %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* @stdout
%30 = tail call i32* @freopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = load i32, i32* @stderr
%36 = tail call i32* @freopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
ret i32 0
41: ; preds = %15
%42 = load i32, i32* @EXIT_SUCCESS
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %0
%45 = call i32 @waitpid(i32 %3, i32* %1, i32 0)
%46 = load i32, i32* %1
%47 = call i32 @exit(i32 %46)
unreachable
}