#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void creat_daemon(void) {
  pid_t pid;
  pid = fork();
  if (pid == -1)
    do {
      perror("fork error");
      exit(EXIT_FAILURE);
    } while (0);
  ;
  if (pid > 0)
    exit(EXIT_SUCCESS);
  if (setsid() == -1)
    do {
      perror("SETSID ERROR");
      exit(EXIT_FAILURE);
    } while (0);
  ;
  chdir("/");
  int i;
  for (i = 0; i < 3; ++i) {
    close(i);
    open("/dev/null", O_RDWR);
    dup(0);
    dup(0);
  }
  umask(0);
  return;
}


x86 assembly (clang, -O0):
<creat_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11cb <creat_daemon+0x3b>
jmp    11b1 <creat_daemon+0x21>
lea    0xe48(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11e3 <creat_daemon+0x53>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1212 <creat_daemon+0x82>
jmp    11f8 <creat_daemon+0x68>
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdff(%rip),%rdi
call   1030 <chdir@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1268 <creat_daemon+0xd8>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    0xdd3(%rip),%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1225 <creat_daemon+0x95>
xor    %edi,%edi
call   1070 <umask@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<creat_daemon>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     124a <creat_daemon+0xba>
test   %eax,%eax
jg     1253 <creat_daemon+0xc3>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1261 <creat_daemon+0xd1>
lea    0xe55(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    0x2e12(%rip),%r14
mov    (%r14),%esi
lea    0xe3a(%rip),%rbx
mov    %rbx,%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <umask@plt>
lea    0xdaf(%rip),%rdi
jmp    1268 <creat_daemon+0xd8>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xda3(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [13 x i8] c"SETSID ERROR\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @creat_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @fork()
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %12
6: ; preds = %0
br label %7
7: ; preds = %6
%8 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; No predecessors!
br label %12
12: ; preds = %11, %0
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @EXIT_SUCCESS
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %12
%19 = call i32 (...) @setsid()
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %18
br label %22
22: ; preds = %21
%23 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; No predecessors!
br label %27
27: ; preds = %26, %18
%28 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %29
29: ; preds = %39, %27
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 3
br i1 %31, label %32, label %42
32: ; preds = %29
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = call i32 @dup(i32 0)
%38 = call i32 @dup(i32 0)
br label %39
39: ; preds = %32
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %29
42: ; preds = %29
%43 = call i32 @umask(i32 0)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [13 x i8] c"SETSID ERROR\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @creat_daemon() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = load i32, i32* @EXIT_SUCCESS
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %7
%13 = tail call i32 (...) @setsid()
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @close(i32 0)
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = tail call i32 @dup(i32 0)
%25 = tail call i32 @dup(i32 0)
%26 = tail call i32 @close(i32 1)
%27 = load i32, i32* @O_RDWR
%28 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = tail call i32 @dup(i32 0)
%30 = tail call i32 @dup(i32 0)
%31 = tail call i32 @close(i32 2)
%32 = load i32, i32* @O_RDWR
%33 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = tail call i32 @dup(i32 0)
%35 = tail call i32 @dup(i32 0)
%36 = tail call i32 @umask(i32 0)
ret void
}