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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_CONS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
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*/ stderr;
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 rl;
  struct sigaction sa;
  umask(0);
  if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
    fprintf(stderr, "%s: can't get file limit", cmd);
    exit(EXIT_FAILURE);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "%s: can't fork", cmd);
    exit(EXIT_FAILURE);
  } else if (pid != 0)
    exit(EXIT_SUCCESS);
  setsid();
  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(SIGHUP, &sa, NULL) < 0) {
    fprintf(stderr, "%s: can't ignore SIGHUP", cmd);
    exit(EXIT_FAILURE);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "%s: cannot fork", cmd);
    exit(EXIT_FAILURE);
  } else
    exit(EXIT_SUCCESS);
  if (chdir("/") < 0) {
    fprintf(stderr, "%s: cannot change directory to /", cmd);
    exit(EXIT_FAILURE);
  }
  if (rl.rlim_max == RLIM_INFINITY)
    rl.rlim_max = 1024;
  for (i = 0; i < rl.rlim_max; ++i) {
    close(i);
  }
  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(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1090 <getrlimit@plt>
cmp    $0x0,%rax
jge    11d6 <daemonize+0x56>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3d(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1212 <daemonize+0x92>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
je     122b <daemonize+0xab>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1230 <daemonize+0xb0>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x8,%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,-0x38(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    129f <daemonize+0x11f>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd9c(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    12db <daemonize+0x15b>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd78(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%rsi
call   1090 <getrlimit@plt>
test   %rax,%rax
jns    11b9 <daemonize+0x39>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
jmp    11d5 <daemonize+0x55>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11eb <daemonize+0x6b>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jne    1261 <daemonize+0xe1>
xor    %eax,%eax
call   1040 <setsid@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,0x8(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
jns    1240 <daemonize+0xc0>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
jmp    11d5 <daemonize+0x55>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    1261 <daemonize+0xe1>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
jmp    11d5 <daemonize+0x55>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"%s: can't get file limit\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"%s: can't fork\00"
@EXIT_SUCCESS = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str.2 = [24 x i8] c"%s: can't ignore SIGHUP\00"
@.str.3 = [16 x i8] c"%s: cannot fork\00"
@LOG_CONS = global i32 0
@LOG_DAEMON = global i32 0
@LOG_ERR = global i32 0
@O_RDWR = global i32 0
@RLIM_INFINITY = global i32 0
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 = call i32 @umask(i32 0)
%11 = load i32, i32* @RLIMIT_NOFILE
%12 = call i64 @getrlimit(i32 %11, %struct.rlimit* %8)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %2
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
%21 = call i64 (...) @fork()
store i64 %21, i64* %7
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %2
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %20
%30 = load i64, i64* %7
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = call i32 (...) @setsid()
%38 = load i32, i32* @SIG_IGN
%39 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 2
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 1
%41 = call i32 @sigemptyset(i32* %40)
%42 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
store i64 0, i64* %42
%43 = load i32, i32* @SIGHUP
%44 = call i64 @sigaction(i32 %43, %struct.sigaction* %9, i32* null)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %36
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %2
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %36
%53 = call i64 (...) @fork()
store i64 %53, i64* %7
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %2
%58 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %52
%62 = load i32, i32* @EXIT_SUCCESS
%63 = call i32 @exit(i32 %62)
unreachable
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"%s: can't get file limit\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"%s: can't fork\00"
@EXIT_SUCCESS = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str.2 = [24 x i8] c"%s: can't ignore SIGHUP\00"
@.str.3 = [16 x i8] c"%s: cannot fork\00"
@LOG_CONS = global i32 0
@LOG_DAEMON = global i32 0
@LOG_ERR = global i32 0
@O_RDWR = global i32 0
@RLIM_INFINITY = global i32 0
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 = tail call i32 @umask(i32 0)
%7 = load i32, i32* @RLIMIT_NOFILE
%8 = call i64 @getrlimit(i32 %7, %struct.rlimit* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = icmp eq i64 %16, 0
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = load i32, i32* @EXIT_SUCCESS
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %23
%29 = call i32 (...) @setsid()
%30 = load i32, i32* @SIG_IGN
%31 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 2
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 1
%33 = call i32 @sigemptyset(i32* %32)
%34 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
store i64 0, i64* %34
%35 = load i32, i32* @SIGHUP
%36 = call i64 @sigaction(i32 %35, %struct.sigaction* %3, i32* null)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %28
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %28
%44 = call i64 (...) @fork()
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %43
%52 = load i32, i32* @EXIT_SUCCESS
%53 = call i32 @exit(i32 %52)
unreachable
}