#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(void) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    return 0;
  }
  if (pid > 0) {
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    return 0;
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return 1;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    117a <daemonize+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <daemonize+0x96>
cmpq   $0x0,-0x10(%rbp)
jle    118c <daemonize+0x3c>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1050 <umask@plt>
mov    $0x0,%al
call   1040 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11b5 <daemonize+0x65>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <daemonize+0x96>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11a5 <daemonize+0x55>
jne    11a9 <daemonize+0x59>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <umask@plt>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
js     11a5 <daemonize+0x55>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 0, i32* %1
br label %26
8: ; preds = %0
%9 = load i64, i64* %2
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i32 @umask(i32 0)
%15 = call i64 (...) @setsid()
store i64 %15, i64* %3
%16 = load i64, i64* %3
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 0, i32* %1
br label %26
19: ; preds = %13
%20 = load i32, i32* @STDIN_FILENO
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @STDERR_FILENO
%25 = call i32 @close(i32 %24)
store i32 1, i32* %1
br label %26
26: ; preds = %19, %18, %7
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @daemonize() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %18, label %3
3: ; preds = %0
%4 = icmp eq i64 %1, 0
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %3
%8 = tail call i32 @umask(i32 0)
%9 = tail call i64 (...) @setsid()
%10 = icmp slt i64 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = load i32, i32* @STDIN_FILENO
%13 = tail call i32 @close(i32 %12)
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @close(i32 %14)
%16 = load i32, i32* @STDERR_FILENO
%17 = tail call i32 @close(i32 %16)
br label %18
18: ; preds = %7, %0, %11
%19 = phi i32 [ 1, %11 ], [ 0, %0 ], [ 0, %7 ]
ret i32 %19
}