#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 */

/* Variables and functions */
int NOFILE;
int /*<<< orphan*/ SIGCLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ setpgrp();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(void) {
  int i;
  if (fork() != 0)
    return -1;
  for (i = 0; i < NOFILE; i++)
    close(i);
  umask(0);
  signal(SIGCLD, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  setpgrp();
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     1175 <daemonize+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <daemonize+0x97>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%eax
jge    11a4 <daemonize+0x54>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    117c <daemonize+0x2c>
xor    %edi,%edi
call   1050 <umask@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    $0x0,%al
call   1060 <setpgrp@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11c8 <daemonize+0x78>
mov    0x2e54(%rip),%r14
cmpl   $0x0,(%r14)
jle    118f <daemonize+0x3f>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%ebx
cmp    (%r14),%ebx
jl     1180 <daemonize+0x30>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <umask@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%r14
mov    (%r14),%esi
call   1040 <signal@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1040 <signal@plt>
xor    %eax,%eax
call   1060 <setpgrp@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@NOFILE = global i32 0
@SIGCLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i32
%3 = call i64 (...) @fork()
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %6
5: ; preds = %0
store i32 -1, i32* %1
br label %26
6: ; preds = %0
store i32 0, i32* %2
br label %7
7: ; preds = %14, %6
%8 = load i32, i32* %2
%9 = load i32, i32* @NOFILE
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i32, i32* %2
%13 = call i32 @close(i32 %12)
br label %14
14: ; preds = %11
%15 = load i32, i32* %2
%16 = add nsw i32 %15, 1
store i32 %16, i32* %2
br label %7
17: ; preds = %7
%18 = call i32 @umask(i32 0)
%19 = load i32, i32* @SIGCLD
%20 = load i32, i32* @SIG_IGN
%21 = call i32 @signal(i32 %19, i32 %20)
%22 = load i32, i32* @SIGHUP
%23 = load i32, i32* @SIG_IGN
%24 = call i32 @signal(i32 %22, i32 %23)
%25 = call i32 (...) @setpgrp()
store i32 0, i32* %1
br label %26
26: ; preds = %17, %5
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@NOFILE = global i32 0
@SIGCLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
define i32 @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %21
3: ; preds = %0
%4 = load i32, i32* @NOFILE
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %3, %6
%7 = phi i32 [ %9, %6 ], [ 0, %3 ]
%8 = tail call i32 @close(i32 %7)
%9 = add nuw nsw i32 %7, 1
%10 = load i32, i32* @NOFILE
%11 = icmp slt i32 %9, %10
br i1 %11, label %6, label %12
12: ; preds = %6, %3
%13 = tail call i32 @umask(i32 0)
%14 = load i32, i32* @SIGCLD
%15 = load i32, i32* @SIG_IGN
%16 = tail call i32 @signal(i32 %14, i32 %15)
%17 = load i32, i32* @SIGHUP
%18 = load i32, i32* @SIG_IGN
%19 = tail call i32 @signal(i32 %17, i32 %18)
%20 = tail call i32 (...) @setpgrp()
br label %21
21: ; preds = %0, %12
%22 = phi i32 [ 0, %12 ], [ -1, %0 ]
ret i32 %22
}