#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 /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int make_daemon() {
  int i, fd0, fd1, fd2;
  pid_t pid;
  struct rlimit rl;
  struct sigaction sa;
  umask(0);
  if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
    return -1;
  if ((pid = fork()) < 0)
    return -1;
  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)
    return -1;
  if ((pid = fork()) < 0)
    return -1;
  else if (pid != 0)
    exit(0);
  if (chdir("/") < 0)
    return -1;
  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);
  return 0;
}


x86 assembly (clang, -O0):
<make_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   10c0 <getrlimit@plt>
cmp    $0x0,%rax
jge    11e7 <make_daemon+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <make_daemon+0x192>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1208 <make_daemon+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <make_daemon+0x192>
cmpq   $0x0,-0x20(%rbp)
je     121a <make_daemon+0x6a>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    121f <make_daemon+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    $0x0,%rax
jge    1273 <make_daemon+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <make_daemon+0x192>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1294 <make_daemon+0xe4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <make_daemon+0x192>
cmpq   $0x0,-0x20(%rbp)
je     12a6 <make_daemon+0xf6>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    12ab <make_daemon+0xfb>
lea    0xd4e(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    12cd <make_daemon+0x11d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <make_daemon+0x192>
mov    -0x28(%rbp),%eax
mov    0x2ce9(%rip),%rcx
cmp    (%rcx),%eax
jne    12e6 <make_daemon+0x136>
movl   $0x400,-0x28(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    130f <make_daemon+0x15f>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12ed <make_daemon+0x13d>
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
lea    0xce3(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x10(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_daemon>:
push   %rbx
sub    $0x20,%rsp
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   10c0 <getrlimit@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     12b5 <make_daemon+0x105>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12b5 <make_daemon+0x105>
jne    12bd <make_daemon+0x10d>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1080 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
test   %rax,%rax
js     12b5 <make_daemon+0x105>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12b5 <make_daemon+0x105>
jne    12bd <make_daemon+0x10d>
lea    0xdb3(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
js     12b5 <make_daemon+0x105>
mov    0x8(%rsp),%eax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%eax
jne    1270 <make_daemon+0xc0>
movl   $0x400,0x8(%rsp)
jmp    1274 <make_daemon+0xc4>
test   %eax,%eax
jle    1290 <make_daemon+0xe0>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    0x8(%rsp),%ebx
jl     1280 <make_daemon+0xd0>
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
lea    0xd62(%rip),%rdi
call   10a0 <open@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
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 i32 @make_daemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.rlimit
%8 = alloca %struct.sigaction
%9 = call i32 @umask(i32 0)
%10 = load i32, i32* @RLIMIT_NOFILE
%11 = call i64 @getrlimit(i32 %10, %struct.rlimit* %7)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 -1, i32* %1
br label %73
14: ; preds = %0
%15 = call i64 (...) @fork()
store i64 %15, i64* %6
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 -1, i32* %1
br label %73
18: ; preds = %14
%19 = load i64, i64* %6
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %18
br label %24
24: ; preds = %23
%25 = call i32 (...) @setsid()
%26 = load i32, i32* @SIG_IGN
%27 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %8, i32 0, i32 2
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %8, i32 0, i32 1
%29 = call i32 @sigemptyset(i32* %28)
%30 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %8, i32 0, i32 0
store i64 0, i64* %30
%31 = load i32, i32* @SIGHUP
%32 = call i64 @sigaction(i32 %31, %struct.sigaction* %8, i32* null)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %24
store i32 -1, i32* %1
br label %73
35: ; preds = %24
%36 = call i64 (...) @fork()
store i64 %36, i64* %6
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 -1, i32* %1
br label %73
39: ; preds = %35
%40 = load i64, i64* %6
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %39
br label %45
45: ; preds = %44
%46 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
store i32 -1, i32* %1
br label %73
49: ; preds = %45
%50 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i32, i32* @RLIM_INFINITY
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i32 1024, i32* %55
br label %56
56: ; preds = %54, %49
store i32 0, i32* %2
br label %57
57: ; preds = %65, %56
%58 = load i32, i32* %2
%59 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%60 = load i32, i32* %59
%61 = icmp slt i32 %58, %60
br i1 %61, label %62, label %68
62: ; preds = %57
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62
%66 = load i32, i32* %2
%67 = add nsw i32 %66, 1
store i32 %67, i32* %2
br label %57
68: ; preds = %57
%69 = load i32, i32* @O_RDWR
%70 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %69)
store i32 %70, i32* %3
%71 = call i32 @dup(i32 0)
store i32 %71, i32* %4
%72 = call i32 @dup(i32 0)
store i32 %72, i32* %5
store i32 0, i32* %1
br label %73
73: ; preds = %68, %48, %38, %34, %17, %13
%74 = load i32, i32* %1
ret i32 %74
}

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 i32 @make_daemon() {
%1 = alloca %struct.rlimit
%2 = alloca %struct.sigaction
%3 = bitcast %struct.rlimit* %1 to i8*
%4 = bitcast %struct.sigaction* %2 to i8*
%5 = tail call i32 @umask(i32 0)
%6 = load i32, i32* @RLIMIT_NOFILE
%7 = call i64 @getrlimit(i32 %6, %struct.rlimit* %1)
%8 = icmp slt i64 %7, 0
br i1 %8, label %56, label %9
9: ; preds = %0
%10 = call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %56, label %12
12: ; preds = %9
%13 = icmp eq i64 %10, 0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %12
%17 = call i32 (...) @setsid()
%18 = load i32, i32* @SIG_IGN
%19 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 1
%21 = call i32 @sigemptyset(i32* %20)
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 0
store i64 0, i64* %22
%23 = load i32, i32* @SIGHUP
%24 = call i64 @sigaction(i32 %23, %struct.sigaction* %2, i32* null)
%25 = icmp slt i64 %24, 0
br i1 %25, label %56, label %26
26: ; preds = %16
%27 = call i64 (...) @fork()
%28 = icmp slt i64 %27, 0
br i1 %28, label %56, label %29
29: ; preds = %26
%30 = icmp eq i64 %27, 0
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %29
%34 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%35 = icmp slt i64 %34, 0
br i1 %35, label %56, label %36
36: ; preds = %33
%37 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%38 = load i32, i32* %37
%39 = load i32, i32* @RLIM_INFINITY
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 1024, i32* %37
br label %44
42: ; preds = %36
%43 = icmp sgt i32 %38, 0
br i1 %43, label %44, label %51
44: ; preds = %41, %42
br label %45
45: ; preds = %44, %45
%46 = phi i32 [ %48, %45 ], [ 0, %44 ]
%47 = call i32 @close(i32 %46)
%48 = add nuw nsw i32 %46, 1
%49 = load i32, i32* %37
%50 = icmp slt i32 %48, %49
br i1 %50, label %45, label %51
51: ; preds = %45, %42
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = call i32 @dup(i32 0)
%55 = call i32 @dup(i32 0)
br label %56
56: ; preds = %33, %26, %16, %9, %0, %51
%57 = phi i32 [ 0, %51 ], [ -1, %0 ], [ -1, %9 ], [ -1, %16 ], [ -1, %26 ], [ -1, %33 ]
ret i32 %57
}