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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getdtablesize();
int getpid();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
void daemonize(void) {
  FILE *fp;
  int maxfd;
  int i;
  if ((i = fork()) < 0) {
    perror("couldn't fork");
    exit(2);
  } else if (i > 0) {
    _exit(0);
  }
  setsid();
  if ((i = fork()) < 0) {
    perror("couldn't fork #2");
    exit(2);
  } else if (i > 0) {
    _exit(0);
  }
  if ((fp = fopen("/var/run/hdidle.pid", "w")) != NULL) {
    fprintf(fp, "%d", getpid());
    fclose(fp);
  }
  chdir("/tmp");
  maxfd = getdtablesize();
  for (i = 0; i < maxfd; i++)
    close(i);
  open("/dev/null", O_RDONLY);
  open("/dev/null", O_WRONLY);
  open("/dev/null", O_WRONLY);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1201 <daemonize+0x31>
lea    0xe0e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jle    1212 <daemonize+0x42>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    1217 <daemonize+0x47>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1247 <daemonize+0x77>
lea    0xdd6(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jle    1258 <daemonize+0x88>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    125d <daemonize+0x8d>
lea    0xdbb(%rip),%rdi
lea    0xdc8(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     12a8 <daemonize+0xd8>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x18(%rbp),%rdi
mov    %eax,%edx
lea    0xd9b(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd89(%rip),%rdi
call   1060 <chdir@plt>
mov    $0x0,%al
call   10f0 <getdtablesize@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12e7 <daemonize+0x117>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12c5 <daemonize+0xf5>
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
lea    0xd46(%rip),%rdi
call   10a0 <open@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%esi
lea    0xd31(%rip),%rdi
call   10a0 <open@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
lea    0xd1c(%rip),%rdi
call   10a0 <open@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     12b0 <daemonize+0xe0>
jne    12a9 <daemonize+0xd9>
xor    %eax,%eax
call   1080 <setsid@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     12b9 <daemonize+0xe9>
jne    12a9 <daemonize+0xd9>
lea    0xe14(%rip),%rdi
lea    0xe21(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     123f <daemonize+0x6f>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe08(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xdf2(%rip),%rdi
call   1060 <chdir@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10f0 <getdtablesize@plt>
test   %eax,%eax
jle    126e <daemonize+0x9e>
mov    %eax,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1260 <daemonize+0x90>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
lea    0xdbf(%rip),%rbx
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    0x2d43(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    10a0 <open@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xd49(%rip),%rdi
jmp    12c0 <daemonize+0xf0>
lea    0xd4e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"couldn't fork\00"
@.str.1 = [17 x i8] c"couldn't fork\00"
@.str.2 = [20 x i8] c"/var/run/hdidle.pid\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [5 x i8] c"/tmp\00"
@.str.6 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define void @daemonize() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 2)
unreachable
9: ; preds = %0
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @_exit(i32 0)
unreachable
14: ; preds = %9
br label %15
15: ; preds = %14
%16 = call i32 (...) @setsid()
%17 = call i32 (...) @fork()
store i32 %17, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %15
%23 = load i32, i32* %3
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @_exit(i32 0)
unreachable
27: ; preds = %22
br label %28
28: ; preds = %27
%29 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %29, i32** %1
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = load i32*, i32** %1
%33 = call i32 (...) @getpid()
%34 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = load i32*, i32** %1
%36 = call i32 @fclose(i32* %35)
br label %37
37: ; preds = %31, %28
%38 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 (...) @getdtablesize()
store i32 %39, i32* %2
store i32 0, i32* %3
br label %40
40: ; preds = %47, %37
%41 = load i32, i32* %3
%42 = load i32, i32* %2
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44
%48 = load i32, i32* %3
%49 = add nsw i32 %48, 1
store i32 %49, i32* %3
br label %40
50: ; preds = %40
%51 = load i32, i32* @O_RDONLY
%52 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %51)
%53 = load i32, i32* @O_WRONLY
%54 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %53)
%55 = load i32, i32* @O_WRONLY
%56 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %55)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"couldn't fork\00"
@.str.1 = [17 x i8] c"couldn't fork\00"
@.str.2 = [20 x i8] c"/var/run/hdidle.pid\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [5 x i8] c"/tmp\00"
@.str.6 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define void @daemonize() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 2)
unreachable
6: ; preds = %0
%7 = icmp eq i32 %1, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @_exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i32 (...) @setsid()
%12 = tail call i32 (...) @fork()
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %10
%18 = icmp eq i32 %12, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i32 @_exit(i32 0)
unreachable
21: ; preds = %17
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = tail call i32 @fclose(i32* %22)
br label %28
28: ; preds = %24, %21
%29 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%30 = tail call i32 (...) @getdtablesize()
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28, %32
%33 = phi i32 [ %35, %32 ], [ 0, %28 ]
%34 = tail call i32 @close(i32 %33)
%35 = add nuw nsw i32 %33, 1
%36 = icmp eq i32 %35, %30
br i1 %36, label %37, label %32
37: ; preds = %32, %28
%38 = load i32, i32* @O_RDONLY
%39 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @O_WRONLY
%41 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %40)
%42 = load i32, i32* @O_WRONLY
%43 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %42)
ret void
}