#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*/ O_RDWR;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int serverDaemonize(void) {
  pid_t id;
  umask(0);
  close(0);
  close(1);
  close(2);
  if (open("/dev/null", O_RDWR) == 0) {
    dup(0);
    dup(0);
    if (chdir("/") == 0) {
      id = fork();
      if (id == 0) {
        setsid();
        return 1;
      } else if (id > 0) {
        exit(0);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<serverDaemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
call   1070 <umask@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1080 <open@plt>
cmp    $0x0,%rax
jne    1237 <serverDaemonize+0xb7>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
lea    0xe2c(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jne    1232 <serverDaemonize+0xb2>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1216 <serverDaemonize+0x96>
mov    $0x0,%al
call   1060 <setsid@plt>
movl   $0x1,-0x4(%rbp)
jmp    123e <serverDaemonize+0xbe>
cmpq   $0x0,-0x10(%rbp)
jle    1228 <serverDaemonize+0xa8>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    122d <serverDaemonize+0xad>
jmp    1232 <serverDaemonize+0xb2>
jmp    1237 <serverDaemonize+0xb7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverDaemonize>:
push   %rbx
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <umask@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1080 <open@plt>
test   %rax,%rax
jne    1203 <serverDaemonize+0x83>
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
lea    0xe34(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jne    1203 <serverDaemonize+0x83>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11f7 <serverDaemonize+0x77>
jle    1203 <serverDaemonize+0x83>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
define i32 @serverDaemonize() {
%1 = alloca i32
%2 = alloca i64
%3 = call i32 @umask(i32 0)
%4 = call i32 @close(i32 0)
%5 = call i32 @close(i32 1)
%6 = call i32 @close(i32 2)
%7 = load i32, i32* @O_RDWR
%8 = call i64 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %29
10: ; preds = %0
%11 = call i32 @dup(i32 0)
%12 = call i32 @dup(i32 0)
%13 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %10
%16 = call i64 (...) @fork()
store i64 %16, i64* %2
%17 = load i64, i64* %2
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 (...) @setsid()
store i32 1, i32* %1
br label %30
21: ; preds = %15
%22 = load i64, i64* %2
%23 = icmp sgt 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
br label %28
28: ; preds = %27, %10
br label %29
29: ; preds = %28, %0
store i32 0, i32* %1
br label %30
30: ; preds = %29, %19
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"/\00"
define i32 @serverDaemonize() {
%1 = tail call i32 @umask(i32 0)
%2 = tail call i32 @close(i32 0)
%3 = tail call i32 @close(i32 1)
%4 = tail call i32 @close(i32 2)
%5 = load i32, i32* @O_RDWR
%6 = tail call i64 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %22
8: ; preds = %0
%9 = tail call i32 @dup(i32 0)
%10 = tail call i32 @dup(i32 0)
%11 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %8
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 (...) @setsid()
br label %22
18: ; preds = %13
%19 = icmp sgt i64 %14, 0
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %0, %18, %8, %16
%23 = phi i32 [ 1, %16 ], [ 0, %8 ], [ 0, %18 ], [ 0, %0 ]
ret i32 %23
}