#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 */
struct sigaction {
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
  int /*<<< orphan*/ sa_handler;
};
struct rlimit {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_CONS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ err_quit(char *, char const *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char const *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int, int, int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(const char *cmd) {
  int i, fd0, fd1, fd2;
  pid_t pid;
  struct rlimit r1;
  struct sigaction sa;
  if (getrlimit(RLIMIT_NOFILE, &r1) < 0) {
    err_quit("%s: can't get file limit", cmd);
  }
  if ((pid = fork()) < 0) {
    err_quit("%s: can't fork", cmd);
  } else if (pid != 0) {
    exit(0);
  }
  umask(0);
  setsid();
  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(SIGHUP, &sa, NULL) < 0) {
    err_quit("%s: can't ignore SIGHUP", cmd);
  }
  if ((pid = fork()) < 0) {
    err_quit("%s: can't fork", cmd);
  } else if (pid != 0) {
    exit(0);
  }
  if (chdir("/") < 0) {
    err_quit("%s: can't change directory to /", cmd);
  }
  close(0);
  close(1);
  close(2);
  fd0 = open("/dev/null", O_RDWR);
  fd1 = dup(0);
  fd2 = dup(0);
  openlog(cmd, LOG_CONS, LOG_DAEMON);
  if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
    syslog(LOG_ERR, "unexpected file descriptors %d %d %d", fd0, fd1, fd2);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   10f0 <getrlimit@plt>
cmp    $0x0,%rax
jge    1218 <daemonize+0x38>
mov    -0x8(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1080 <err_quit@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1242 <daemonize+0x62>
mov    -0x8(%rbp),%rsi
lea    0xde1(%rip),%rdi
call   1080 <err_quit@plt>
jmp    1259 <daemonize+0x79>
cmpq   $0x0,-0x20(%rbp)
je     1254 <daemonize+0x74>
xor    %edi,%edi
call   10e0 <exit@plt>
jmp    1259 <daemonize+0x79>
xor    %edi,%edi
call   10a0 <umask@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x8,%rdi
call   1090 <sigemptyset@plt>
movq   $0x0,-0x38(%rbp)
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    12b8 <daemonize+0xd8>
mov    -0x8(%rbp),%rsi
lea    0xd75(%rip),%rdi
call   1080 <err_quit@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    12e2 <daemonize+0x102>
mov    -0x8(%rbp),%rsi
lea    0xd41(%rip),%rdi
call   1080 <err_quit@plt>
jmp    12f9 <daemonize+0x119>
cmpq   $0x0,-0x20(%rbp)
je     12f4 <daemonize+0x114>
xor    %edi,%edi
call   10e0 <exit@plt>
jmp    12f9 <daemonize+0x119>
lea    0xd5e(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    131f <daemonize+0x13f>
mov    -0x8(%rbp),%rsi
lea    0xd26(%rip),%rdi
call   1080 <err_quit@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
lea    0xd16(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2c47(%rip),%rax
mov    (%rax),%esi
mov    0x2c5e(%rip),%rax
mov    (%rax),%edx
call   10d0 <openlog@plt>
cmpl   $0x0,-0x10(%rbp)
jne    139f <daemonize+0x1bf>
cmpl   $0x1,-0x14(%rbp)
jne    139f <daemonize+0x1bf>
cmpl   $0x2,-0x18(%rbp)
je     13c8 <daemonize+0x1e8>
mov    0x2c02(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xcb1(%rip),%rsi
call   10b0 <syslog@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   10f0 <getrlimit@plt>
test   %rax,%rax
jns    1214 <daemonize+0x34>
lea    0xdf4(%rip),%rdi
mov    %r14,%rsi
call   1080 <err_quit@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
js     1224 <daemonize+0x44>
je     1233 <daemonize+0x53>
jmp    1295 <daemonize+0xb5>
lea    0xdee(%rip),%rdi
mov    %r14,%rsi
call   1080 <err_quit@plt>
xor    %edi,%edi
call   10a0 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1090 <sigemptyset@plt>
movq   $0x0,(%rsp)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
jns    1287 <daemonize+0xa7>
lea    0xda9(%rip),%rdi
mov    %r14,%rsi
call   1080 <err_quit@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
js     129c <daemonize+0xbc>
je     12ab <daemonize+0xcb>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    0xd76(%rip),%rdi
mov    %r14,%rsi
call   1080 <err_quit@plt>
lea    0xdac(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jns    12cb <daemonize+0xeb>
lea    0xd7d(%rip),%rdi
mov    %r14,%rsi
call   1080 <err_quit@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
lea    0xd6a(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%r15d
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,%ebp
mov    0x2ca1(%rip),%rax
mov    (%rax),%esi
mov    0x2cb8(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   10d0 <openlog@plt>
test   %r15d,%r15d
jne    1344 <daemonize+0x164>
cmp    $0x1,%ebx
jne    1344 <daemonize+0x164>
cmp    $0x2,%ebp
jne    1344 <daemonize+0x164>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
mov    %r15d,%edx
mov    %ebx,%ecx
mov    %ebp,%r8d
call   10b0 <syslog@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [25 x i8] c"%s: can't get file limit\00"
@.str.1 = [15 x i8] c"%s: can't fork\00"
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str.2 = [24 x i8] c"%s: can't ignore SIGHUP\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [32 x i8] c"%s: can't change directory to /\00"
@.str.5 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@LOG_CONS = global i32 0
@LOG_DAEMON = global i32 0
@LOG_ERR = global i32 0
@.str.6 = [37 x i8] c"unexpected file descriptors %d %d %d\00"
define void @daemonize(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.rlimit
%9 = alloca %struct.sigaction
store i8* %0, i8** %2
%10 = load i32, i32* @RLIMIT_NOFILE
%11 = call i64 @getrlimit(i32 %10, %struct.rlimit* %8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i32 @err_quit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %1
%17 = call i64 (...) @fork()
store i64 %17, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i8*, i8** %2
%21 = call i32 @err_quit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %28
22: ; preds = %16
%23 = load i64, i64* %7
%24 = icmp ne i64 %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, %19
%29 = call i32 @umask(i32 0)
%30 = call i32 (...) @setsid()
%31 = load i32, i32* @SIG_IGN
%32 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 2
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 1
%34 = call i32 @sigemptyset(i32* %33)
%35 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
store i64 0, i64* %35
%36 = load i32, i32* @SIGHUP
%37 = call i64 @sigaction(i32 %36, %struct.sigaction* %9, i32* null)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %28
%40 = load i8*, i8** %2
%41 = call i32 @err_quit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %28
%43 = call i64 (...) @fork()
store i64 %43, i64* %7
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i8*, i8** %2
%47 = call i32 @err_quit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %46)
br label %54
48: ; preds = %42
%49 = load i64, i64* %7
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %48
br label %54
54: ; preds = %53, %45
%55 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i8*, i8** %2
%59 = call i32 @err_quit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %57, %54
%61 = call i32 @close(i32 0)
%62 = call i32 @close(i32 1)
%63 = call i32 @close(i32 2)
%64 = load i32, i32* @O_RDWR
%65 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %64)
store i32 %65, i32* %4
%66 = call i32 @dup(i32 0)
store i32 %66, i32* %5
%67 = call i32 @dup(i32 0)
store i32 %67, i32* %6
%68 = load i8*, i8** %2
%69 = load i32, i32* @LOG_CONS
%70 = load i32, i32* @LOG_DAEMON
%71 = call i32 @openlog(i8* %68, i32 %69, i32 %70)
%72 = load i32, i32* %4
%73 = icmp ne i32 %72, 0
br i1 %73, label %80, label %74
74: ; preds = %60
%75 = load i32, i32* %5
%76 = icmp ne i32 %75, 1
br i1 %76, label %80, label %77
77: ; preds = %74
%78 = load i32, i32* %6
%79 = icmp ne i32 %78, 2
br i1 %79, label %80, label %87
80: ; preds = %77, %74, %60
%81 = load i32, i32* @LOG_ERR
%82 = load i32, i32* %4
%83 = load i32, i32* %5
%84 = load i32, i32* %6
%85 = call i32 @syslog(i32 %81, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %82, i32 %83, i32 %84)
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %77
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [25 x i8] c"%s: can't get file limit\00"
@.str.1 = [15 x i8] c"%s: can't fork\00"
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str.2 = [24 x i8] c"%s: can't ignore SIGHUP\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [32 x i8] c"%s: can't change directory to /\00"
@.str.5 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@LOG_CONS = global i32 0
@LOG_DAEMON = global i32 0
@LOG_ERR = global i32 0
@.str.6 = [37 x i8] c"unexpected file descriptors %d %d %d\00"
define void @daemonize(i8* %0) {
%2 = alloca %struct.rlimit
%3 = alloca %struct.sigaction
%4 = bitcast %struct.rlimit* %2 to i8*
%5 = bitcast %struct.sigaction* %3 to i8*
%6 = load i32, i32* @RLIMIT_NOFILE
%7 = call i64 @getrlimit(i32 %6, %struct.rlimit* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @err_quit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %1
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @err_quit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %20
16: ; preds = %11
%17 = icmp eq i64 %12, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %16, %14
%21 = call i32 @umask(i32 0)
%22 = call i32 (...) @setsid()
%23 = load i32, i32* @SIG_IGN
%24 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
%26 = call i32 @sigemptyset(i32* %25)
%27 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i64 0, i64* %27
%28 = load i32, i32* @SIGHUP
%29 = call i64 @sigaction(i32 %28, %struct.sigaction* %3, i32* null)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 @err_quit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %33
33: ; preds = %31, %20
%34 = call i64 (...) @fork()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @err_quit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %42
38: ; preds = %33
%39 = icmp eq i64 %34, 0
br i1 %39, label %42, label %40
40: ; preds = %38
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %38, %36
%43 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @err_quit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %47
47: ; preds = %45, %42
%48 = call i32 @close(i32 0)
%49 = call i32 @close(i32 1)
%50 = call i32 @close(i32 2)
%51 = load i32, i32* @O_RDWR
%52 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%53 = call i32 @dup(i32 0)
%54 = call i32 @dup(i32 0)
%55 = load i32, i32* @LOG_CONS
%56 = load i32, i32* @LOG_DAEMON
%57 = call i32 @openlog(i8* %0, i32 %55, i32 %56)
%58 = icmp ne i32 %52, 0
%59 = icmp ne i32 %53, 1
%60 = select i1 %58, i1 true, i1 %59
%61 = icmp ne i32 %54, 2
%62 = select i1 %60, i1 true, i1 %61
br i1 %62, label %63, label %67
63: ; preds = %47
%64 = load i32, i32* @LOG_ERR
%65 = call i32 @syslog(i32 %64, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %52, i32 %53, i32 %54)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %47
ret void
}