#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*/ LOG_CONS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ RLIMIT_NOFILE;
int RLIM_INFINITY;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ openlog(char const *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
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 rl;
  struct sigaction sa;
  umask(0);
  if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
    printf("%s: can't get file limit\n", cmd);
  if ((pid = fork()) < 0)
    printf("%s: can't fork\n", cmd);
  else if (pid != 0)
    exit(0);
  setsid();
  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(SIGHUP, &sa, NULL) < 0)
    printf("%s: can't ignore SIGHUP\n", cmd);
  if ((pid = fork()) < 0)
    printf("%s: can't fork\n, cmd");
  else if (pid != 0)
    exit(0);
  if (chdir("/") < 0)
    printf("%s: can't change directory to //\n", cmd);
  if (rl.rlim_max == RLIM_INFINITY)
    rl.rlim_max = 1024;
  for (i - 0; i < rl.rlim_max; i++)
    close(i);
  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)
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   10d0 <getrlimit@plt>
cmp    $0x0,%rax
jge    1201 <daemonize+0x41>
mov    -0x8(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    122d <daemonize+0x6d>
mov    -0x8(%rbp),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1244 <daemonize+0x84>
cmpq   $0x0,-0x20(%rbp)
je     123f <daemonize+0x7f>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    1244 <daemonize+0x84>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x8,%rdi
call   1080 <sigemptyset@plt>
movq   $0x0,-0x38(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0x0,%rax
jge    129e <daemonize+0xde>
mov    -0x8(%rbp),%rsi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    12c6 <daemonize+0x106>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12dd <daemonize+0x11d>
cmpq   $0x0,-0x20(%rbp)
je     12d8 <daemonize+0x118>
xor    %edi,%edi
call   10c0 <exit@plt>
jmp    12dd <daemonize+0x11d>
lea    0xd74(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1305 <daemonize+0x145>
mov    -0x8(%rbp),%rsi
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
mov    0x2ca9(%rip),%rcx
cmp    (%rcx),%eax
jne    131e <daemonize+0x15e>
movl   $0x400,-0x28(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1340 <daemonize+0x180>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    131e <daemonize+0x15e>
mov    -0x8(%rbp),%rdi
mov    0x2c75(%rip),%rax
mov    (%rax),%esi
mov    0x2c8c(%rip),%rax
mov    (%rax),%edx
call   10b0 <openlog@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1379 <daemonize+0x1b9>
cmpl   $0x1,-0x14(%rbp)
jne    1379 <daemonize+0x1b9>
cmpl   $0x2,-0x18(%rbp)
je     13a2 <daemonize+0x1e2>
mov    0x2c28(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xce9(%rip),%rsi
call   10a0 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   10b0 <getrlimit@plt>
test   %rax,%rax
jns    11d8 <daemonize+0x38>
lea    0xe32(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     11e8 <daemonize+0x48>
je     11f9 <daemonize+0x59>
jmp    1259 <daemonize+0xb9>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1070 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
jns    124b <daemonize+0xab>
lea    0xde9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1260 <daemonize+0xc0>
je     126e <daemonize+0xce>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xddc(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xde3(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jns    1290 <daemonize+0xf0>
lea    0xdd4(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%eax
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%eax
jne    12a7 <daemonize+0x107>
movl   $0x400,0x8(%rsp)
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1090 <openlog@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [26 x i8] c"%s: can't get file limit\0A\00"
@.str.1 = [16 x i8] c"%s: can't fork\0A\00"
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str.2 = [25 x i8] c"%s: can't ignore SIGHUP\0A\00"
@.str.3 = [21 x i8] c"%s: can't fork\0A, cmd\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [34 x i8] c"%s: can't change directory to //\0A\00"
@RLIM_INFINITY = 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 = 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 %17
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %1
%18 = call i64 (...) @fork()
store i64 %18, i64* %7
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i8*, i8** %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %29
23: ; preds = %17
%24 = load i64, i64* %7
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %23
br label %29
29: ; preds = %28, %20
%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 = %29
%40 = load i8*, i8** %2
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %29
%43 = call i64 (...) @fork()
store i64 %43, i64* %7
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %53
47: ; preds = %42
%48 = load i64, i64* %7
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %47
br label %53
53: ; preds = %52, %45
%54 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i8*, i8** %2
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %56, %53
%60 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%61 = load i32, i32* %60
%62 = load i32, i32* @RLIM_INFINITY
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
store i32 1024, i32* %65
br label %66
66: ; preds = %64, %59
%67 = load i32, i32* %3
%68 = sub nsw i32 %67, 0
br label %69
69: ; preds = %77, %66
%70 = load i32, i32* %3
%71 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%72 = load i32, i32* %71
%73 = icmp slt i32 %70, %72
br i1 %73, label %74, label %80
74: ; preds = %69
%75 = load i32, i32* %3
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %74
%78 = load i32, i32* %3
%79 = add nsw i32 %78, 1
store i32 %79, i32* %3
br label %69
80: ; preds = %69
%81 = load i8*, i8** %2
%82 = load i32, i32* @LOG_CONS
%83 = load i32, i32* @LOG_DAEMON
%84 = call i32 @openlog(i8* %81, i32 %82, i32 %83)
%85 = load i32, i32* %4
%86 = icmp ne i32 %85, 0
br i1 %86, label %93, label %87
87: ; preds = %80
%88 = load i32, i32* %5
%89 = icmp ne i32 %88, 1
br i1 %89, label %93, label %90
90: ; preds = %87
%91 = load i32, i32* %6
%92 = icmp ne i32 %91, 2
br i1 %92, label %93, label %100
93: ; preds = %90, %87, %80
%94 = load i32, i32* @LOG_ERR
%95 = load i32, i32* %4
%96 = load i32, i32* %5
%97 = load i32, i32* %6
%98 = call i32 @syslog(i32 %94, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %95, i32 %96, i32 %97)
%99 = call i32 @exit(i32 1)
unreachable
100: ; preds = %90
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [26 x i8] c"%s: can't get file limit\0A\00"
@.str.1 = [16 x i8] c"%s: can't fork\0A\00"
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str.2 = [25 x i8] c"%s: can't ignore SIGHUP\0A\00"
@.str.3 = [21 x i8] c"%s: can't fork\0A, cmd\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [34 x i8] c"%s: can't change directory to //\0A\00"
@RLIM_INFINITY = global i32 0
@LOG_CONS = global i32 0
@LOG_DAEMON = global i32 0
@LOG_ERR = 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 %12
10: ; preds = %1
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %1
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
17: ; preds = %12
%18 = icmp eq i64 %13, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %17, %15
%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 = %21
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %33
33: ; preds = %31, %21
%34 = call i64 (...) @fork()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 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.4, i64 0, i64 0))
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %47
47: ; preds = %45, %42
%48 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%49 = load i32, i32* %48
%50 = load i32, i32* @RLIM_INFINITY
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 1024, i32* %48
br label %53
53: ; preds = %52, %47
%54 = load i32, i32* @LOG_CONS
%55 = load i32, i32* @LOG_DAEMON
%56 = call i32 @openlog(i8* %0, i32 %54, i32 %55)
ret void
}