#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*/ startup_rc;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int pipe(int *);
int /*<<< orphan*/ procd_handle_signals();
int read(int, int *, int);
scalar_t__ setsid();
scalar_t__ store_pid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int write(int, int *, int);
void procd_daemonize(void) {
  int pipe_fds[2], startup_rc = 1;
  pid_t pid;
  if (pipe(pipe_fds) == -1) {
    syslog(LOG_ERR, "pipe error: %s\n", strerror(errno));
    exit(1);
  }
  pid = fork();
  if (pid < 0) {
    syslog(LOG_ERR, "fork error: %s\n", strerror(errno));
    exit(1);
  }
  if (pid > 0) {
    if (read(pipe_fds[0], &startup_rc, sizeof(startup_rc)) == -1)
      syslog(LOG_ERR, "pipe read error: %s\n", strerror(errno));
    exit(startup_rc);
  }
  umask(0);
  procd_handle_signals();
  if (setsid() < 0) {
    syslog(LOG_ERR, "setsid error: %s\n", strerror(errno));
    goto notify_parent;
  }
  if (chdir("/") < 0) {
    syslog(LOG_ERR, "chdir error: %s\n", strerror(errno));
    goto notify_parent;
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  if (store_pid() < 0)
    goto notify_parent;
  startup_rc = 0;
notify_parent:
  if (write(pipe_fds[1], &startup_rc, sizeof(startup_rc)) == -1) {
    syslog(LOG_ERR, "pipe write error: %s\n", strerror(errno));
    exit(1);
  }
  if (startup_rc != 0)
    exit(startup_rc);
}


x86 assembly (clang, -O0):
<procd_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x1,-0xc(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1226 <procd_daemonize+0x56>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xde9(%rip),%rsi
call   10c0 <syslog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1271 <procd_daemonize+0xa1>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xdae(%rip),%rsi
call   10c0 <syslog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    12c9 <procd_daemonize+0xf9>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    12c1 <procd_daemonize+0xf1>
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xd64(%rip),%rsi
call   10c0 <syslog@plt>
mov    -0xc(%rbp),%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   10b0 <umask@plt>
mov    $0x0,%al
call   10a0 <procd_handle_signals@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1318 <procd_daemonize+0x148>
mov    0x2cc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
lea    0xd27(%rip),%rsi
call   10c0 <syslog@plt>
jmp    13a5 <procd_daemonize+0x1d5>
lea    0xd28(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    135e <procd_daemonize+0x18e>
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xcf5(%rip),%rsi
call   10c0 <syslog@plt>
jmp    13a5 <procd_daemonize+0x1d5>
mov    0x2c63(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1080 <store_pid@plt>
cmp    $0x0,%rax
jge    139e <procd_daemonize+0x1ce>
jmp    13a5 <procd_daemonize+0x1d5>
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    13f4 <procd_daemonize+0x224>
mov    0x2bf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c06(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xc75(%rip),%rsi
call   10c0 <syslog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
je     1406 <procd_daemonize+0x236>
mov    -0xc(%rbp),%edi
call   10d0 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procd_daemonize>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     129f <procd_daemonize+0xcf>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     12c2 <procd_daemonize+0xf2>
jne    12e5 <procd_daemonize+0x115>
xor    %edi,%edi
call   10b0 <umask@plt>
xor    %eax,%eax
call   10a0 <procd_handle_signals@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     132d <procd_daemonize+0x15d>
lea    0xe1b(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     1336 <procd_daemonize+0x166>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
xor    %eax,%eax
call   1080 <store_pid@plt>
test   %rax,%rax
js     1360 <procd_daemonize+0x190>
movl   $0x0,0xc(%rsp)
mov    0x14(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1378 <procd_daemonize+0x1a8>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d12(%rip),%rax
mov    (%rax),%ebx
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd43(%rip),%rsi
jmp    1396 <procd_daemonize+0x1c6>
mov    0x2cef(%rip),%rax
mov    (%rax),%ebx
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd30(%rip),%rsi
jmp    1396 <procd_daemonize+0x1c6>
mov    0x10(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    1324 <procd_daemonize+0x154>
mov    0x2cb4(%rip),%rax
mov    (%rax),%ebx
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd05(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   10c0 <syslog@plt>
mov    0xc(%rsp),%edi
call   10d0 <exit@plt>
lea    0xd01(%rip),%rbx
jmp    133d <procd_daemonize+0x16d>
lea    0xd0c(%rip),%rbx
mov    0x2c74(%rip),%rax
mov    (%rax),%ebp
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   10c0 <syslog@plt>
mov    0x14(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    139f <procd_daemonize+0x1cf>
mov    0x2c39(%rip),%rax
mov    (%rax),%ebx
mov    0x2c50(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xcc4(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   10c0 <syslog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [16 x i8] c"pipe error: %s\0A\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"fork error: %s\0A\00"
@.str.2 = [21 x i8] c"pipe read error: %s\0A\00"
@.str.3 = [18 x i8] c"setsid error: %s\0A\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [17 x i8] c"chdir error: %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.6 = [22 x i8] c"pipe write error: %s\0A\00"
define void @procd_daemonize() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca i64
store i32 1, i32* %2
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i32, i32* @LOG_ERR
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 @syslog(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i64 (...) @fork()
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* @LOG_ERR
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @syslog(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = load i64, i64* %3
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @read(i32 %28, i32* %2, i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i32, i32* @LOG_ERR
%33 = load i32, i32* @errno
%34 = call i32 @strerror(i32 %33)
%35 = call i32 @syslog(i32 %32, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %31, %26
%37 = load i32, i32* %2
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %23
%40 = call i32 @umask(i32 0)
%41 = call i32 (...) @procd_handle_signals()
%42 = call i64 (...) @setsid()
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i32, i32* @LOG_ERR
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 @syslog(i32 %45, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %68
49: ; preds = %39
%50 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = load i32, i32* @LOG_ERR
%54 = load i32, i32* @errno
%55 = call i32 @strerror(i32 %54)
%56 = call i32 @syslog(i32 %53, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %55)
br label %68
57: ; preds = %49
%58 = load i32, i32* @STDIN_FILENO
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* @STDOUT_FILENO
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* @STDERR_FILENO
%63 = call i32 @close(i32 %62)
%64 = call i64 (...) @store_pid()
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %57
br label %68
67: ; preds = %57
store i32 0, i32* %2
br label %68
68: ; preds = %67, %66, %52, %44
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%70 = load i32, i32* %69
%71 = call i32 @write(i32 %70, i32* %2, i32 4)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %79
73: ; preds = %68
%74 = load i32, i32* @LOG_ERR
%75 = load i32, i32* @errno
%76 = call i32 @strerror(i32 %75)
%77 = call i32 @syslog(i32 %74, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %76)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %68
%80 = load i32, i32* %2
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i32, i32* %2
%84 = call i32 @exit(i32 %83)
unreachable
85: ; preds = %79
ret void
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [16 x i8] c"pipe error: %s\0A\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"fork error: %s\0A\00"
@.str.2 = [21 x i8] c"pipe read error: %s\0A\00"
@.str.3 = [18 x i8] c"setsid error: %s\0A\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [17 x i8] c"chdir error: %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.6 = [22 x i8] c"pipe write error: %s\0A\00"
define void @procd_daemonize() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = bitcast [2 x i32]* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i32, i32* @LOG_ERR
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = call i32 @syslog(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* @LOG_ERR
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @syslog(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = icmp eq i64 %15, 0
br i1 %24, label %37, label %25
25: ; preds = %23
%26 = load i32, i32* %5
%27 = call i32 @read(i32 %26, i32* %2, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* @LOG_ERR
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @syslog(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %29, %25
%35 = load i32, i32* %2
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %23
%38 = call i32 @umask(i32 0)
%39 = call i32 (...) @procd_handle_signals()
%40 = call i64 (...) @setsid()
%41 = icmp slt i64 %40, 0
br i1 %41, label %54, label %42
42: ; preds = %37
%43 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%44 = icmp slt i64 %43, 0
br i1 %44, label %54, label %45
45: ; preds = %42
%46 = load i32, i32* @STDIN_FILENO
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @STDERR_FILENO
%51 = call i32 @close(i32 %50)
%52 = call i64 (...) @store_pid()
%53 = icmp slt i64 %52, 0
br i1 %53, label %60, label %65
54: ; preds = %42, %37
%55 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), %37 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), %42 ]
%56 = load i32, i32* @LOG_ERR
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 @syslog(i32 %56, i8* %55, i32 %58)
br label %60
60: ; preds = %54, %45
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @write(i32 %62, i32* %2, i32 4)
%64 = icmp eq i32 %63, -1
br i1 %64, label %70, label %76
65: ; preds = %45
store i32 0, i32* %2
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 @write(i32 %67, i32* %2, i32 4)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %78
70: ; preds = %65, %60
%71 = load i32, i32* @LOG_ERR
%72 = load i32, i32* @errno
%73 = call i32 @strerror(i32 %72)
%74 = call i32 @syslog(i32 %71, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %60
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %65
ret void
}