#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize() {
  pid_t pid;
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  if (setsid() < 0)
    exit(EXIT_FAILURE);
  signal(SIGCHLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid < 0)
    exit(EXIT_FAILURE);
  if (pid > 0)
    exit(EXIT_SUCCESS);
  umask(0);
  openlog("grive-daemon", LOG_PID, LOG_DAEMON);
  int x;
  for (x = sysconf(_SC_OPEN_MAX); x > 0; x--) {
    close(x);
  }
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11ac <daemonize+0x2c>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11c5 <daemonize+0x45>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    11e4 <daemonize+0x64>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1236 <daemonize+0xb6>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    124f <daemonize+0xcf>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
lea    0xd91(%rip),%rdi
call   1080 <openlog@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    12a5 <daemonize+0x125>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1285 <daemonize+0x105>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1233 <daemonize+0xb3>
jne    1241 <daemonize+0xc1>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
js     1233 <daemonize+0xb3>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rbx
mov    (%rbx),%esi
call   1050 <signal@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1233 <daemonize+0xb3>
jne    1241 <daemonize+0xc1>
xor    %edi,%edi
call   1060 <umask@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
lea    0xe04(%rip),%rdi
call   1080 <openlog@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
test   %eax,%eax
jle    1231 <daemonize+0xb1>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1030 <close@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     1220 <daemonize+0xa0>
pop    %rbx
ret
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [13 x i8] c"grive-daemon\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
@_SC_OPEN_MAX = global i32 0
define void @daemonize() {
%1 = alloca i64
%2 = alloca i32
%3 = call i64 (...) @fork()
store i64 %3, i64* %1
%4 = load i64, i64* %1
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i64, i64* %1
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @EXIT_SUCCESS
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %9
%16 = call i64 (...) @setsid()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = load i32, i32* @SIGCHLD
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = load i32, i32* @SIGHUP
%26 = load i32, i32* @SIG_IGN
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = call i64 (...) @fork()
store i64 %28, i64* %1
%29 = load i64, i64* %1
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %21
%35 = load i64, i64* %1
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %34
%41 = call i32 @umask(i32 0)
%42 = load i32, i32* @LOG_PID
%43 = load i32, i32* @LOG_DAEMON
%44 = call i32 @openlog(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %42, i32 %43)
%45 = load i32, i32* @_SC_OPEN_MAX
%46 = call i32 @sysconf(i32 %45)
store i32 %46, i32* %2
br label %47
47: ; preds = %53, %40
%48 = load i32, i32* %2
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %47
%51 = load i32, i32* %2
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %50
%54 = load i32, i32* %2
%55 = add nsw i32 %54, -1
store i32 %55, i32* %2
br label %47
56: ; preds = %47
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [13 x i8] c"grive-daemon\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
@_SC_OPEN_MAX = global i32 0
define void @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @EXIT_FAILURE
%5 = tail call i32 @exit(i32 %4)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %6
%12 = tail call i64 (...) @setsid()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %11
%18 = load i32, i32* @SIGCHLD
%19 = load i32, i32* @SIG_IGN
%20 = tail call i32 @signal(i32 %18, i32 %19)
%21 = load i32, i32* @SIGHUP
%22 = load i32, i32* @SIG_IGN
%23 = tail call i32 @signal(i32 %21, i32 %22)
%24 = tail call i64 (...) @fork()
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %17
%30 = icmp eq i64 %24, 0
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = load i32, i32* @EXIT_SUCCESS
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %29
%35 = tail call i32 @umask(i32 0)
%36 = load i32, i32* @LOG_PID
%37 = load i32, i32* @LOG_DAEMON
%38 = tail call i32 @openlog(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %36, i32 %37)
%39 = load i32, i32* @_SC_OPEN_MAX
%40 = tail call i32 @sysconf(i32 %39)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %34, %42
%43 = phi i32 [ %45, %42 ], [ %40, %34 ]
%44 = tail call i32 @close(i32 %43)
%45 = add nsw i32 %43, -1
%46 = icmp ugt i32 %43, 1
br i1 %46, label %42, label %47
47: ; preds = %42, %34
ret void
}