#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*/ 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);
scalar_t__ fork();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int sigaction(int /*<<< orphan*/, struct sigaction *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(const char *cmd) {
  int i, fd0, fd1, fd2;
  pid_t pid;
  struct rlimit r1;
  struct sigaction sact;
  umask(0);
  if (getrlimit(RLIMIT_NOFILE, &r1) < 0) {
    exit(1);
  }
  if ((pid = fork()) < 0) {
    exit(1);
  } else if (pid != 0)
    exit(0);
  setsid();
  sact.sa_handler = SIG_IGN;
  sigemptyset(&sact.sa_mask);
  sact.sa_flags = 0;
  if (sigaction(SIGHUP, &sact, NULL) == -1) {
    exit(1);
  }
  if ((pid = fork()) < 0) {
    exit(1);
  } else if (pid != 0)
    exit(0);
  if (chdir("/") < 0) {
    exit(1);
  }
  if (r1.rlim_max == RLIM_INFINITY)
    r1.rlim_max = 20;
  for (i = 0; i < r1.rlim_max; i++)
    close(i);
  fd0 = open("/dev/null", O_RDWR);
  fd1 = dup(0);
  fd2 = dup(0);
  if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
    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    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   10c0 <getrlimit@plt>
cmp    $0x0,%rax
jge    11e9 <daemonize+0x39>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1208 <daemonize+0x58>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
je     121a <daemonize+0x6a>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    121f <daemonize+0x6f>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    0x2da3(%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    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
jne    1270 <daemonize+0xc0>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    128f <daemonize+0xdf>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
je     12a1 <daemonize+0xf1>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    12a6 <daemonize+0xf6>
lea    0xd53(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    12c6 <daemonize+0x116>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%eax
mov    0x2cf0(%rip),%rcx
cmp    (%rcx),%eax
jne    12df <daemonize+0x12f>
movl   $0x14,-0x28(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1308 <daemonize+0x158>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12e6 <daemonize+0x136>
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
lea    0xcea(%rip),%rdi
call   10a0 <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)
cmpl   $0x0,-0x10(%rbp)
jne    1352 <daemonize+0x1a2>
cmpl   $0x1,-0x14(%rbp)
jne    1352 <daemonize+0x1a2>
cmpl   $0x2,-0x18(%rbp)
je     135c <daemonize+0x1ac>
mov    $0x1,%edi
call   10b0 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   10c0 <getrlimit@plt>
test   %rax,%rax
js     12cb <daemonize+0x11b>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12cb <daemonize+0x11b>
jne    12d5 <daemonize+0x125>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdi
call   1080 <sigemptyset@plt>
movq   $0x0,0x8(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
cmp    $0xffffffff,%eax
je     12cb <daemonize+0x11b>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12cb <daemonize+0x11b>
jne    12d5 <daemonize+0x125>
lea    0xdb1(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     12cb <daemonize+0x11b>
mov    (%rsp),%eax
mov    0x2d5d(%rip),%rcx
cmp    (%rcx),%eax
jne    1270 <daemonize+0xc0>
movl   $0x14,(%rsp)
jmp    1274 <daemonize+0xc4>
test   %eax,%eax
jle    128f <daemonize+0xdf>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    (%rsp),%ebx
jl     1280 <daemonize+0xd0>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
lea    0xd63(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1050 <dup@plt>
test   %ebp,%ebp
jne    12cb <daemonize+0x11b>
cmp    $0x1,%ebx
jne    12cb <daemonize+0x11b>
cmp    $0x2,%eax
jne    12cb <daemonize+0x11b>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [2 x i8] c"/\00"
@RLIM_INFINITY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = 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 %16
14: ; preds = %1
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = call i64 (...) @fork()
store i64 %17, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %16
%22 = load i64, i64* %7
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %21
br label %27
27: ; preds = %26
%28 = call i32 (...) @setsid()
%29 = load i32, i32* @SIG_IGN
%30 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 1
%32 = call i32 @sigemptyset(i32* %31)
%33 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %9, i32 0, i32 0
store i64 0, i64* %33
%34 = load i32, i32* @SIGHUP
%35 = call i32 @sigaction(i32 %34, %struct.sigaction* %9, i32* null)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %27
%40 = call i64 (...) @fork()
store i64 %40, i64* %7
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %39
%45 = load i64, i64* %7
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %44
br label %50
50: ; preds = %49
%51 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %50
%56 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load i32, i32* @RLIM_INFINITY
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %55
%61 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
store i32 20, i32* %61
br label %62
62: ; preds = %60, %55
store i32 0, i32* %3
br label %63
63: ; preds = %71, %62
%64 = load i32, i32* %3
%65 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
%66 = load i32, i32* %65
%67 = icmp slt i32 %64, %66
br i1 %67, label %68, label %74
68: ; preds = %63
%69 = load i32, i32* %3
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68
%72 = load i32, i32* %3
%73 = add nsw i32 %72, 1
store i32 %73, i32* %3
br label %63
74: ; preds = %63
%75 = load i32, i32* @O_RDWR
%76 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %75)
store i32 %76, i32* %4
%77 = call i32 @dup(i32 0)
store i32 %77, i32* %5
%78 = call i32 @dup(i32 0)
store i32 %78, i32* %6
%79 = load i32, i32* %4
%80 = icmp ne i32 %79, 0
br i1 %80, label %87, label %81
81: ; preds = %74
%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 %89
87: ; preds = %84, %81, %74
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %84
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
%struct.sigaction = type { i64, i32, i32 }
@RLIMIT_NOFILE = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [2 x i8] c"/\00"
@RLIM_INFINITY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @daemonize(i8* nocapture readnone %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 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 1)
unreachable
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
%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 i32 @sigaction(i32 %28, %struct.sigaction* %3, i32* null)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %21
%34 = call i64 (...) @fork()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @exit(i32 1)
unreachable
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
%43 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %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 20, i32* %48
br label %55
53: ; preds = %47
%54 = icmp sgt i32 %49, 0
br i1 %54, label %55, label %62
55: ; preds = %52, %53
br label %56
56: ; preds = %55, %56
%57 = phi i32 [ %59, %56 ], [ 0, %55 ]
%58 = call i32 @close(i32 %57)
%59 = add nuw nsw i32 %57, 1
%60 = load i32, i32* %48
%61 = icmp slt i32 %59, %60
br i1 %61, label %56, label %62
62: ; preds = %56, %53
%63 = load i32, i32* @O_RDWR
%64 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %63)
%65 = call i32 @dup(i32 0)
%66 = call i32 @dup(i32 0)
%67 = icmp ne i32 %64, 0
%68 = icmp ne i32 %65, 1
%69 = select i1 %67, i1 true, i1 %68
%70 = icmp ne i32 %66, 2
%71 = select i1 %69, i1 true, i1 %70
br i1 %71, label %72, label %74
72: ; preds = %62
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %62
ret void
}