#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int setsid();
int Daemon_Init(void) {
  int devnullfd;
  chdir("/");
  switch (fork()) {
  case -1:
    printf("Daemon_Init: error in fork\r\n");
    exit(1);
  case 0:
    break;
  default:
    exit(0);
  }
  if (setsid() == -1) {
    printf("Daemon_Init: setsid error\r\n");
    exit(1);
  }
  devnullfd = open("/dev/null", O_RDWR);
  if (devnullfd == -1) {
    printf("Null device open failed\r\n");
    return (1);
  }
  close(0);
  close(1);
  close(2);
  dup2(devnullfd, 0);
  dup2(devnullfd, 1);
  dup2(devnullfd, 2);
  close(devnullfd);
  return (0);
}


x86 assembly (clang, -O0):
<Daemon_Init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
call   1030 <chdir@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11be <Daemon_Init+0x3e>
jmp    11ae <Daemon_Init+0x2e>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     11d4 <Daemon_Init+0x54>
jmp    11d9 <Daemon_Init+0x59>
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11e0 <Daemon_Init+0x60>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1206 <Daemon_Init+0x86>
lea    0xe28(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0xe25(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1240 <Daemon_Init+0xc0>
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128e <Daemon_Init+0x10e>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Daemon_Init>:
push   %rbp
push   %rbx
push   %rax
lea    0xe76(%rip),%rdi
call   1040 <chdir@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    122b <Daemon_Init+0xab>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     1239 <Daemon_Init+0xb9>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1211 <Daemon_Init+0x91>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1222 <Daemon_Init+0xa2>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
cmp    $0xffffffff,%eax
jne    124f <Daemon_Init+0xcf>
lea    0xe09(%rip),%rdi
jmp    1240 <Daemon_Init+0xc0>
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [29 x i8] c"Daemon_Init: error in fork\0D\0A\00"
@.str.2 = [28 x i8] c"Daemon_Init: setsid error\0D\0A\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.4 = [26 x i8] c"Null device open failed\0D\0A\00"
define i32 @Daemon_Init() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @fork()
switch i32 %4, label %9 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %0
%6 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
br label %11
9: ; preds = %0
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %8
%12 = call i32 (...) @setsid()
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %18)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %36
24: ; preds = %17
%25 = call i32 @close(i32 0)
%26 = call i32 @close(i32 1)
%27 = call i32 @close(i32 2)
%28 = load i32, i32* %2
%29 = call i32 @dup2(i32 %28, i32 0)
%30 = load i32, i32* %2
%31 = call i32 @dup2(i32 %30, i32 1)
%32 = load i32, i32* %2
%33 = call i32 @dup2(i32 %32, i32 2)
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
store i32 0, i32* %1
br label %36
36: ; preds = %24, %22
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@str = [25 x i8] c"Null device open failed\0D\00"
@str.5 = [27 x i8] c"Daemon_Init: setsid error\0D\00"
@str.6 = [28 x i8] c"Daemon_Init: error in fork\0D\00"
define i32 @Daemon_Init() {
%1 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
switch i32 %2, label %6 [
i32 -1, label %3
i32 0, label %8
]
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = tail call i32 (...) @setsid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %28
20: ; preds = %14
%21 = tail call i32 @close(i32 0)
%22 = tail call i32 @close(i32 1)
%23 = tail call i32 @close(i32 2)
%24 = tail call i32 @dup2(i32 %16, i32 0)
%25 = tail call i32 @dup2(i32 %16, i32 1)
%26 = tail call i32 @dup2(i32 %16, i32 2)
%27 = tail call i32 @close(i32 %16)
br label %28
28: ; preds = %20, %18
%29 = phi i32 [ 1, %18 ], [ 0, %20 ]
ret i32 %29
}