#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*/ O_RDWR;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ plm_log_syslog(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int plm_daemonize() {
  int fd;
  pid_t pid;
  pid = fork();
  if (pid < 0)
    return (-1);
  if (pid > 0)
    exit(0);
  setsid();
  if (chdir("/") < 0)
    return (-1);
  close(fileno(stdin));
  fd = open("/dev/null", O_RDWR);
  if (fd < 0) {
    plm_log_syslog("open /dev/null failed: %s", strerror(errno));
    return (-1);
  }
  dup2(fd, fileno(stdout));
  dup2(fd, fileno(stderr));
  return (0);
}


x86 assembly (clang, -O0):
<plm_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ca <plm_daemonize+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <plm_daemonize+0x107>
cmpq   $0x0,-0x10(%rbp)
jle    11dc <plm_daemonize+0x3c>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
lea    0xe16(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1205 <plm_daemonize+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <plm_daemonize+0x107>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
call   1060 <close@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1264 <plm_daemonize+0xc4>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xdb9(%rip),%rdi
call   1030 <plm_log_syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <plm_daemonize+0x107>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%esi
call   1050 <dup2@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%esi
call   1050 <dup2@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plm_daemonize>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     1254 <plm_daemonize+0xb4>
jne    125d <plm_daemonize+0xbd>
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xe34(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1254 <plm_daemonize+0xb4>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
call   1060 <close@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1238 <plm_daemonize+0x98>
mov    %eax,%ebp
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <dup2@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <dup2@plt>
xor    %ebx,%ebx
jmp    1254 <plm_daemonize+0xb4>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdbf(%rip),%rdi
mov    %eax,%esi
call   1030 <plm_log_syslog@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@stdin = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [26 x i8] c"open /dev/null failed: %s\00"
@errno = global i32 0
@stdout = global i32 0
@stderr = global i32 0
define i32 @plm_daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %39
8: ; preds = %0
%9 = load i64, i64* %3
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i32 (...) @setsid()
%15 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %1
br label %39
18: ; preds = %13
%19 = load i32, i32* @stdin
%20 = call i32 @fileno(i32 %19)
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 @plm_log_syslog(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 -1, i32* %1
br label %39
30: ; preds = %18
%31 = load i32, i32* %2
%32 = load i32, i32* @stdout
%33 = call i32 @fileno(i32 %32)
%34 = call i32 @dup2(i32 %31, i32 %33)
%35 = load i32, i32* %2
%36 = load i32, i32* @stderr
%37 = call i32 @fileno(i32 %36)
%38 = call i32 @dup2(i32 %35, i32 %37)
store i32 0, i32* %1
br label %39
39: ; preds = %30, %26, %17, %7
%40 = load i32, i32* %1
ret i32 %40
}

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