#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 {
  int dummy;
};
struct rlimit {
  int rlim_max;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ LOG_CONS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_DEBUG;
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();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char const *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
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) {
    perror("getrlimit");
  }
  if ((pid = fork()) < 0) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(0);
  }
  setsid();
  signal(SIGHUP, SIG_IGN);
  if ((pid = fork()) < 0) {
    perror("fork2");
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(0);
  }
  if (chdir("/") < 0) {
    perror("chdir to root");
  }
  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 descritors %d, %d, %d", fd0, fd1, fd2);
    exit(EXIT_FAILURE);
  }
  syslog(LOG_DEBUG, "deamon ok.");
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   10e0 <getrlimit@plt>
cmp    $0x0,%rax
jge    120b <daemonize+0x3b>
lea    0xdfa(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    123a <daemonize+0x6a>
lea    0xde3(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    124c <daemonize+0x7c>
xor    %edi,%edi
call   10d0 <exit@plt>
jmp    1251 <daemonize+0x81>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    129e <daemonize+0xce>
lea    0xd84(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    12b0 <daemonize+0xe0>
xor    %edi,%edi
call   10d0 <exit@plt>
jmp    12b5 <daemonize+0xe5>
lea    0xd59(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    12d7 <daemonize+0x107>
lea    0xd45(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x28(%rbp),%eax
mov    0x2cc7(%rip),%rcx
cmp    (%rcx),%eax
jne    12f0 <daemonize+0x120>
movl   $0x400,-0x28(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1319 <daemonize+0x149>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12f7 <daemonize+0x127>
mov    0x2cc0(%rip),%rax
mov    (%rax),%esi
lea    0xcfc(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2c68(%rip),%rax
mov    (%rax),%esi
mov    0x2c7f(%rip),%rax
mov    (%rax),%edx
call   10c0 <openlog@plt>
cmpl   $0x0,-0x10(%rbp)
jne    137e <daemonize+0x1ae>
cmpl   $0x1,-0x14(%rbp)
jne    137e <daemonize+0x1ae>
cmpl   $0x2,-0x18(%rbp)
je     13ad <daemonize+0x1dd>
mov    0x2c0b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xc97(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    0x2c0a(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2bec(%rip),%rax
mov    (%rax),%edi
lea    0xc98(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   10e0 <getrlimit@plt>
test   %rax,%rax
js     1304 <daemonize+0x134>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     132d <daemonize+0x15d>
jne    1326 <daemonize+0x156>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     1336 <daemonize+0x166>
jne    1326 <daemonize+0x156>
lea    0xdc9(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     1315 <daemonize+0x145>
mov    (%rsp),%eax
mov    0x2d44(%rip),%rcx
cmp    (%rcx),%eax
jne    1271 <daemonize+0xa1>
movl   $0x400,(%rsp)
jmp    1275 <daemonize+0xa5>
test   %eax,%eax
jle    128f <daemonize+0xbf>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    (%rsp),%ebx
jl     1280 <daemonize+0xb0>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
lea    0xd86(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%r15d
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%ebp
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   10c0 <openlog@plt>
test   %r15d,%r15d
jne    1344 <daemonize+0x174>
cmp    $0x1,%ebx
jne    1344 <daemonize+0x174>
cmp    $0x2,%ebp
jne    1344 <daemonize+0x174>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
xor    %eax,%eax
call   1090 <syslog@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf5(%rip),%rdi
call   10b0 <perror@plt>
jmp    11fb <daemonize+0x2b>
lea    0xcfb(%rip),%rdi
call   10b0 <perror@plt>
jmp    125a <daemonize+0x8a>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xcd6(%rip),%rdi
jmp    133d <daemonize+0x16d>
lea    0xcd2(%rip),%rdi
call   10b0 <perror@plt>
jmp    1363 <daemonize+0x193>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
lea    0xcdb(%rip),%rsi
mov    %r15d,%edx
mov    %ebx,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1090 <syslog@plt>
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [6 x i8] c"fork2\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [14 x i8] c"chdir to root\00"
@RLIM_INFINITY = global i32 0
@.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 = [38 x i8] c"unexpected file descritors %d, %d, %d\00"
@LOG_DEBUG = global i32 0
@.str.7 = [11 x i8] c"deamon ok.\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 %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = call i64 (...) @fork()
store i64 %17, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %16
%24 = load i64, i64* %7
%25 = icmp sgt 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
%30 = call i32 (...) @setsid()
%31 = load i32, i32* @SIGHUP
%32 = load i32, i32* @SIG_IGN
%33 = call i32 @signal(i32 %31, i32 %32)
%34 = call i64 (...) @fork()
store i64 %34, i64* %7
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %29
%41 = load i64, i64* %7
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %40
br label %46
46: ; preds = %45
%47 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* @RLIM_INFINITY
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
store i32 1024, i32* %57
br label %58
58: ; preds = %56, %51
store i32 0, i32* %3
br label %59
59: ; preds = %67, %58
%60 = load i32, i32* %3
%61 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%62 = load i32, i32* %61
%63 = icmp slt i32 %60, %62
br i1 %63, label %64, label %70
64: ; preds = %59
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %64
%68 = load i32, i32* %3
%69 = add nsw i32 %68, 1
store i32 %69, i32* %3
br label %59
70: ; preds = %59
%71 = load i32, i32* @O_RDWR
%72 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %71)
store i32 %72, i32* %4
%73 = call i32 @dup(i32 0)
store i32 %73, i32* %5
%74 = call i32 @dup(i32 0)
store i32 %74, i32* %6
%75 = load i8*, i8** %2
%76 = load i32, i32* @LOG_CONS
%77 = load i32, i32* @LOG_DAEMON
%78 = call i32 @openlog(i8* %75, i32 %76, i32 %77)
%79 = load i32, i32* %4
%80 = icmp ne i32 %79, 0
br i1 %80, label %87, label %81
81: ; preds = %70
%82 = load i32, i32* %5
%83 = icmp ne i32 %82, 1
br i1 %83, label %87, label %84
84: ; preds = %81
%85 = load i32, i32* %6
%86 = icmp ne i32 %85, 2
br i1 %86, label %87, label %95
87: ; preds = %84, %81, %70
%88 = load i32, i32* @LOG_ERR
%89 = load i32, i32* %4
%90 = load i32, i32* %5
%91 = load i32, i32* %6
%92 = call i32 (i32, i8*, ...) @syslog(i32 %88, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i32 %89, i32 %90, i32 %91)
%93 = load i32, i32* @EXIT_FAILURE
%94 = call i32 @exit(i32 %93)
unreachable
95: ; preds = %84
%96 = load i32, i32* @LOG_DEBUG
%97 = call i32 (i32, i8*, ...) @syslog(i32 %96, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_NOFILE = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [6 x i8] c"fork2\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [14 x i8] c"chdir to root\00"
@RLIM_INFINITY = global i32 0
@.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 = [38 x i8] c"unexpected file descritors %d, %d, %d\00"
@LOG_DEBUG = global i32 0
@.str.7 = [11 x i8] c"deamon ok.\00"
define void @daemonize(i8* %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = tail call i32 @umask(i32 0)
%5 = load i32, i32* @RLIMIT_NOFILE
%6 = call i64 @getrlimit(i32 %5, %struct.rlimit* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = icmp eq i64 %11, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %17
%22 = call i32 (...) @setsid()
%23 = load i32, i32* @SIGHUP
%24 = load i32, i32* @SIG_IGN
%25 = call i32 @signal(i32 %23, i32 %24)
%26 = call i64 (...) @fork()
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = icmp eq i64 %26, 0
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %32
%37 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%43 = load i32, i32* %42
%44 = load i32, i32* @RLIM_INFINITY
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 1024, i32* %42
br label %49
47: ; preds = %41
%48 = icmp sgt i32 %43, 0
br i1 %48, label %49, label %56
49: ; preds = %46, %47
br label %50
50: ; preds = %49, %50
%51 = phi i32 [ %53, %50 ], [ 0, %49 ]
%52 = call i32 @close(i32 %51)
%53 = add nuw nsw i32 %51, 1
%54 = load i32, i32* %42
%55 = icmp slt i32 %53, %54
br i1 %55, label %50, label %56
56: ; preds = %50, %47
%57 = load i32, i32* @O_RDWR
%58 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %57)
%59 = call i32 @dup(i32 0)
%60 = call i32 @dup(i32 0)
%61 = load i32, i32* @LOG_CONS
%62 = load i32, i32* @LOG_DAEMON
%63 = call i32 @openlog(i8* %0, i32 %61, i32 %62)
%64 = icmp ne i32 %58, 0
%65 = icmp ne i32 %59, 1
%66 = select i1 %64, i1 true, i1 %65
%67 = icmp ne i32 %60, 2
%68 = select i1 %66, i1 true, i1 %67
br i1 %68, label %69, label %74
69: ; preds = %56
%70 = load i32, i32* @LOG_ERR
%71 = call i32 (i32, i8*, ...) @syslog(i32 %70, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i32 %58, i32 %59, i32 %60)
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %56
%75 = load i32, i32* @LOG_DEBUG
%76 = call i32 (i32, i8*, ...) @syslog(i32 %75, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
ret void
}