#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 /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close_all_fds(int /*<<< orphan*/);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int fork();
scalar_t__ setsid();
int run_as_daemon() {
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  if (setsid() < 0)
    return -1;
  switch (fork()) {
  case 0:
    break;
  case -1:
    return -1;
  default:
    _exit(0);
  }
  chdir("/");
  close_all_fds(0);
  dup(0);
  dup(0);
  return 0;
}


x86 assembly (clang, -O0):
<run_as_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
sub    $0xffffffff,%eax
je     1197 <run_as_daemon+0x37>
jmp    1182 <run_as_daemon+0x22>
mov    -0x8(%rbp),%eax
test   %eax,%eax
jne    11a3 <run_as_daemon+0x43>
jmp    1192 <run_as_daemon+0x32>
jmp    11aa <run_as_daemon+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <run_as_daemon+0xd1>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    11c7 <run_as_daemon+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <run_as_daemon+0xd1>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11f6 <run_as_daemon+0x96>
jmp    11e1 <run_as_daemon+0x81>
mov    -0xc(%rbp),%eax
test   %eax,%eax
jne    1202 <run_as_daemon+0xa2>
jmp    11f1 <run_as_daemon+0x91>
jmp    1209 <run_as_daemon+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <run_as_daemon+0xd1>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xdf0(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1070 <close_all_fds@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_as_daemon>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11bb <run_as_daemon+0x5b>
test   %ebx,%ebx
jne    11bf <run_as_daemon+0x5f>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     11b6 <run_as_daemon+0x56>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11bb <run_as_daemon+0x5b>
test   %ebx,%ebx
jne    11bf <run_as_daemon+0x5f>
lea    0xe68(%rip),%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <close_all_fds@plt>
xor    %edi,%edi
call   1050 <dup@plt>
xor    %edi,%edi
call   1050 <dup@plt>
jmp    11bb <run_as_daemon+0x5b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i32 @run_as_daemon() {
%1 = alloca i32
%2 = call i32 (...) @fork()
switch i32 %2, label %5 [
i32 0, label %3
i32 -1, label %4
]
3: ; preds = %0
br label %7
4: ; preds = %0
store i32 -1, i32* %1
br label %22
5: ; preds = %0
%6 = call i32 @_exit(i32 0)
unreachable
7: ; preds = %3
%8 = call i64 (...) @setsid()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %7
store i32 -1, i32* %1
br label %22
11: ; preds = %7
%12 = call i32 (...) @fork()
switch i32 %12, label %15 [
i32 0, label %13
i32 -1, label %14
]
13: ; preds = %11
br label %17
14: ; preds = %11
store i32 -1, i32* %1
br label %22
15: ; preds = %11
%16 = call i32 @_exit(i32 0)
unreachable
17: ; preds = %13
%18 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @close_all_fds(i32 0)
%20 = call i32 @dup(i32 0)
%21 = call i32 @dup(i32 0)
store i32 0, i32* %1
br label %22
22: ; preds = %17, %14, %10, %4
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i32 @run_as_daemon() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 0, label %4
i32 -1, label %16
]
2: ; preds = %0
%3 = tail call i32 @_exit(i32 0)
unreachable
4: ; preds = %0
%5 = tail call i64 (...) @setsid()
%6 = icmp slt i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = tail call i32 (...) @fork()
switch i32 %8, label %9 [
i32 0, label %11
i32 -1, label %16
]
9: ; preds = %7
%10 = tail call i32 @_exit(i32 0)
unreachable
11: ; preds = %7
%12 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @close_all_fds(i32 0)
%14 = tail call i32 @dup(i32 0)
%15 = tail call i32 @dup(i32 0)
br label %16
16: ; preds = %7, %4, %0, %11
%17 = phi i32 [ 0, %11 ], [ %1, %0 ], [ -1, %4 ], [ %8, %7 ]
ret i32 %17
}