#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*/ SIGCHLD;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
scalar_t__ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int init_deamon(void) {
  int pid;
  int i;
  if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
    printf("Cant signal in init_daemon.");
    exit(1);
  }
  if (pid = fork())
    exit(0);
  else if (pid < 0) {
    perror("fail to fork1");
    exit(1);
  }
  setsid();
  if (pid = fork())
    exit(0);
  else if (pid < 0)
    exit(1);
  for (i = 0; i < 3; ++i)
    close(i);
  chdir("/tmp");
  umask(0);
  return 0;
}


x86 assembly (clang, -O0):
<init_deamon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    11d5 <init_deamon+0x45>
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     11ef <init_deamon+0x5f>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    120f <init_deamon+0x7f>
lea    0xe1c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1214 <init_deamon+0x84>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
je     1235 <init_deamon+0xa5>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jge    1249 <init_deamon+0xb9>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    124e <init_deamon+0xbe>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1275 <init_deamon+0xe5>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1255 <init_deamon+0xc5>
lea    0xdae(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_deamon>:
push   %rax
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
je     11fa <init_deamon+0x7a>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11f3 <init_deamon+0x73>
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    11f3 <init_deamon+0x73>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
lea    0xe39(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdff(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str = [28 x i8] c"Cant signal in init_daemon.\00"
@.str.1 = [14 x i8] c"fail to fork1\00"
@.str.2 = [5 x i8] c"/tmp\00"
define i32 @init_deamon() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @SIGCHLD
%4 = load i32, i32* @SIG_IGN
%5 = call i64 @signal(i32 %3, i32 %4)
%6 = load i64, i64* @SIG_ERR
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 (...) @fork()
store i32 %12, i32* %1
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %11
%17 = load i32, i32* %1
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
br label %23
23: ; preds = %22
%24 = call i32 (...) @setsid()
%25 = call i32 (...) @fork()
store i32 %25, i32* %1
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %23
%30 = load i32, i32* %1
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %29
br label %35
35: ; preds = %34
store i32 0, i32* %2
br label %36
36: ; preds = %42, %35
%37 = load i32, i32* %2
%38 = icmp slt i32 %37, 3
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39
%43 = load i32, i32* %2
%44 = add nsw i32 %43, 1
store i32 %44, i32* %2
br label %36
45: ; preds = %36
%46 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @umask(i32 0)
ret i32 0
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str = [28 x i8] c"Cant signal in init_daemon.\00"
@.str.2 = [5 x i8] c"/tmp\00"
define i32 @init_deamon() {
%1 = load i32, i32* @SIGCHLD
%2 = load i32, i32* @SIG_IGN
%3 = tail call i64 @signal(i32 %1, i32 %2)
%4 = load i64, i64* @SIG_ERR
%5 = icmp eq i64 %3, %4
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %9
%15 = tail call i32 (...) @setsid()
%16 = tail call i32 (...) @fork()
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = tail call i32 @close(i32 0)
%20 = tail call i32 @close(i32 1)
%21 = tail call i32 @close(i32 2)
%22 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @umask(i32 0)
ret i32 0
24: ; preds = %14
%25 = tail call i32 @exit(i32 0)
unreachable
}