#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_TLOCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int getpid();
int getppid();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ umask(int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void daemonize() {
  if (getppid() == 1)
    return;
  pid_t pid = fork();
  if (pid < 0)
    exit(1);
  if (pid > 0)
    exit(0);
  setsid();
  umask(022);
  freopen("/var/log/macfanctl.log", "w", stdout);
  freopen("/dev/null", "r", stdin);
  chdir("/");
  int lock_fd = open("/var/run/macfanctld.pid", O_RDWR | O_CREAT, 0640);
  if (lock_fd < 0)
    exit(1);
  if (lockf(lock_fd, F_TLOCK, 0) < 0)
    exit(0);
  char str[32];
  sprintf(str, "%d\n", getpid());
  write(lock_fd, str, strlen(str));
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    $0x0,%al
call   10d0 <getppid@plt>
cmp    $0x1,%eax
jne    11ed <daemonize+0x1d>
jmp    1318 <daemonize+0x148>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    120d <daemonize+0x3d>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    121f <daemonize+0x4f>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    $0x0,%al
call   1090 <setsid@plt>
mov    $0x12,%edi
call   10a0 <umask@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
lea    0xdc0(%rip),%rdi
lea    0xdd0(%rip),%rsi
call   1080 <freopen@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
lea    0xdbd(%rip),%rdi
lea    0xdc0(%rip),%rsi
call   1080 <freopen@plt>
lea    0xdb6(%rip),%rdi
call   1060 <chdir@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    0x2d74(%rip),%rax
or     (%rax),%esi
lea    0xd9a(%rip),%rdi
mov    $0x1a0,%edx
call   10b0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12ae <daemonize+0xde>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <lockf@plt>
cmp    $0x0,%rax
jge    12d2 <daemonize+0x102>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xd51(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
sub    $0x28,%rsp
xor    %eax,%eax
call   10d0 <getppid@plt>
cmp    $0x1,%eax
je     12c1 <daemonize+0xf1>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     12c9 <daemonize+0xf9>
jne    12d3 <daemonize+0x103>
xor    %eax,%eax
call   1090 <setsid@plt>
mov    $0x12,%edi
call   10a0 <umask@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    0xde2(%rip),%rdi
lea    0xdf2(%rip),%rsi
call   1080 <freopen@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
lea    0xddf(%rip),%rdi
lea    0xde2(%rip),%rsi
call   1080 <freopen@plt>
lea    0xdd8(%rip),%rdi
call   1060 <chdir@plt>
mov    0x2d7f(%rip),%rax
mov    0x2d98(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdbc(%rip),%rdi
mov    $0x1a0,%edx
call   10b0 <open@plt>
test   %eax,%eax
js     12c9 <daemonize+0xf9>
mov    %eax,%ebx
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <lockf@plt>
test   %rax,%rax
js     12d3 <daemonize+0x103>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd9f(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   10c0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"/var/log/macfanctl.log\00"
@.str.1 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [24 x i8] c"/var/run/macfanctld.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_TLOCK = global i32 0
@.str.6 = [4 x i8] c"%d\0A\00"
define void @daemonize() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = call i32 (...) @getppid()
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %7
6: ; preds = %0
br label %50
7: ; preds = %0
%8 = call i64 (...) @fork()
store i64 %8, i64* %1
%9 = load i64, i64* %1
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = load i64, i64* %1
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %13
%19 = call i32 (...) @setsid()
%20 = call i32 @umask(i32 18)
%21 = load i32, i32* @stdout
%22 = call i32 @freopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @stdin
%24 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 416)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %18
%35 = load i32, i32* %2
%36 = load i32, i32* @F_TLOCK
%37 = call i64 @lockf(i32 %35, i32 %36, i32 0)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %34
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%43 = call i32 (...) @getpid()
%44 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %2
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = call i32 @write(i32 %45, i8* %46, i32 %48)
br label %50
50: ; preds = %41, %6
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"/var/log/macfanctl.log\00"
@.str.1 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.2 = [10 x i8] c"/dev/null\00"
@.str.3 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [24 x i8] c"/var/run/macfanctld.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_TLOCK = global i32 0
@.str.6 = [4 x i8] c"%d\0A\00"
define void @daemonize() {
%1 = alloca [32 x i8]
%2 = tail call i32 (...) @getppid()
%3 = icmp eq i32 %2, 1
br i1 %3, label %40, label %4
4: ; preds = %0
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %4
%10 = icmp eq i64 %5, 0
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %9
%14 = tail call i32 (...) @setsid()
%15 = tail call i32 @umask(i32 18)
%16 = load i32, i32* @stdout
%17 = tail call i32 @freopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @stdin
%19 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %23, i32 416)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %13
%29 = load i32, i32* @F_TLOCK
%30 = tail call i64 @lockf(i32 %24, i32 %29, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @exit(i32 0)
unreachable
34: ; preds = %28
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%36 = tail call i32 (...) @getpid()
%37 = call i32 @sprintf(i8* %35, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %36)
%38 = call i32 @strlen(i8* %35)
%39 = call i32 @write(i32 %24, i8* %35, i32 %38)
br label %40
40: ; preds = %0, %34
ret void
}