#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 */
int NOFILE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIGCHLD;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int open(char *, int /*<<< orphan*/);
int setsid();
scalar_t__ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int umask(int /*<<< orphan*/);
void my_daemon() {
  int pid, fd;
  if ((pid = fork()) == -1)
    exit(1);
  if (pid != 0)
    exit(0);
  if (setsid() == -1)
    exit(1);
  if ((pid = fork()) == -1)
    exit(1);
  if (pid != 0)
    exit(0);
  for (int i = 0; i < NOFILE; i++)
    close(i);
  if (chdir("/") == -1)
    exit(1);
  if (umask(0) == -1)
    exit(1);
  if ((fd = open("/dev/null", O_RDWR)) == -1)
    exit(1);
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  close(fd);
  if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
    exit(1);
}


x86 assembly (clang, -O0):
<my_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11b5 <my_daemon+0x25>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     11c6 <my_daemon+0x36>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11e0 <my_daemon+0x50>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11fd <my_daemon+0x6d>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     120e <my_daemon+0x7e>
xor    %edi,%edi
call   10a0 <exit@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%eax
jge    123d <my_daemon+0xad>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1215 <my_daemon+0x85>
lea    0xdbc(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    125c <my_daemon+0xcc>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
cmp    $0xffffffff,%eax
jne    1276 <my_daemon+0xe6>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
lea    0xd7c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    12a1 <my_daemon+0x111>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2cc6(%rip),%rcx
cmp    (%rcx),%rax
jne    130d <my_daemon+0x17d>
mov    $0x1,%edi
call   10a0 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_daemon>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1292 <my_daemon+0x102>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1297 <my_daemon+0x107>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1292 <my_daemon+0x102>
mov    0x2dd7(%rip),%r14
cmpl   $0x0,(%r14)
jle    11ef <my_daemon+0x5f>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    (%r14),%ebx
jl     11e0 <my_daemon+0x50>
lea    0xe0a(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     1297 <my_daemon+0x107>
xor    %edi,%edi
call   1080 <umask@plt>
cmp    $0xffffffff,%eax
je     1297 <my_daemon+0x107>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1297 <my_daemon+0x107>
mov    %eax,%ebx
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2d3b(%rip),%rcx
cmp    (%rcx),%rax
je     1297 <my_daemon+0x107>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
cmp    $0xffffffff,%eax
jne    12a1 <my_daemon+0x111>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@NOFILE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
define void @my_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* %1
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i32 (...) @setsid()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %13
%19 = call i32 (...) @fork()
store i32 %19, i32* %1
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %18
%24 = load i32, i32* %1
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %23
store i32 0, i32* %3
br label %29
29: ; preds = %36, %28
%30 = load i32, i32* %3
%31 = load i32, i32* @NOFILE
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 1
store i32 %38, i32* %3
br label %29
39: ; preds = %29
%40 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %39
%45 = call i32 @umask(i32 0)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %44
%50 = load i32, i32* @O_RDWR
%51 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %50)
store i32 %51, i32* %2
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
%56 = load i32, i32* %2
%57 = load i32, i32* @STDIN_FILENO
%58 = call i32 @dup2(i32 %56, i32 %57)
%59 = load i32, i32* %2
%60 = load i32, i32* @STDOUT_FILENO
%61 = call i32 @dup2(i32 %59, i32 %60)
%62 = load i32, i32* %2
%63 = load i32, i32* @STDERR_FILENO
%64 = call i32 @dup2(i32 %62, i32 %63)
%65 = load i32, i32* %2
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* @SIGCHLD
%68 = load i32, i32* @SIG_IGN
%69 = call i64 @signal(i32 %67, i32 %68)
%70 = load i64, i64* @SIG_ERR
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %74
72: ; preds = %55
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %55
ret void
}

LLVM IR (-O2):
@NOFILE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
define void @my_daemon() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %4 [
i32 -1, label %2
i32 0, label %6
]
2: ; preds = %0
%3 = tail call i32 @exit(i32 1)
unreachable
4: ; preds = %0
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %0
%7 = tail call i32 (...) @setsid()
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %6
%12 = tail call i32 (...) @fork()
switch i32 %12, label %18 [
i32 -1, label %16
i32 0, label %13
]
13: ; preds = %11
%14 = load i32, i32* @NOFILE
%15 = icmp sgt i32 %14, 0
br i1 %15, label %23, label %20
16: ; preds = %11
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %23, %13
%21 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i32 %21, -1
br i1 %22, label %29, label %31
23: ; preds = %13, %23
%24 = phi i32 [ %26, %23 ], [ 0, %13 ]
%25 = tail call i32 @close(i32 %24)
%26 = add nuw nsw i32 %24, 1
%27 = load i32, i32* @NOFILE
%28 = icmp slt i32 %26, %27
br i1 %28, label %23, label %20
29: ; preds = %20
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = tail call i32 @umask(i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %31
%37 = load i32, i32* @O_RDWR
%38 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = load i32, i32* @STDIN_FILENO
%44 = tail call i32 @dup2(i32 %38, i32 %43)
%45 = load i32, i32* @STDOUT_FILENO
%46 = tail call i32 @dup2(i32 %38, i32 %45)
%47 = load i32, i32* @STDERR_FILENO
%48 = tail call i32 @dup2(i32 %38, i32 %47)
%49 = tail call i32 @close(i32 %38)
%50 = load i32, i32* @SIGCHLD
%51 = load i32, i32* @SIG_IGN
%52 = tail call i64 @signal(i32 %50, i32 %51)
%53 = load i64, i64* @SIG_ERR
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %42
%56 = tail call i32 @exit(i32 1)
unreachable
57: ; preds = %42
ret void
}