#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int rfork();
scalar_t__ setsid();
int rdaemon() {
  int pid, fd;
  if ((pid = rfork()) < 0) {
    return -1;
  }
  if (!pid) {
    if (setsid() < 0)
      perror("setsid");
    if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
      dup2(fd, 0);
      dup2(fd, 1);
      dup2(fd, 2);
    }
    if (fd > 2)
      close(fd);
  }
  return pid;
}


x86 assembly (clang, -O0):
<rdaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <rfork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1187 <rdaemon+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <rdaemon+0xb2>
cmpl   $0x0,-0x8(%rbp)
jne    120c <rdaemon+0xac>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    11ae <rdaemon+0x4e>
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
je     11f5 <rdaemon+0x95>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0xc(%rbp)
jle    1207 <rdaemon+0xa7>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
jmp    120c <rdaemon+0xac>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdaemon>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <rfork@plt>
test   %eax,%eax
js     11cf <rdaemon+0x6f>
mov    %eax,%ebx
jne    11d4 <rdaemon+0x74>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     11dd <rdaemon+0x7d>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d4 <rdaemon+0x74>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmp    $0x3,%ebp
jl     11d4 <rdaemon+0x74>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11d4 <rdaemon+0x74>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe1c(%rip),%rdi
call   1070 <perror@plt>
jmp    1180 <rdaemon+0x20>

LLVM IR (-O0):
@.str = [7 x i8] c"setsid\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define i32 @rdaemon() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @rfork()
store i32 %4, i32* %2
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 -1, i32* %1
br label %35
7: ; preds = %0
%8 = load i32, i32* %2
%9 = icmp ne i32 %8, 0
br i1 %9, label %33, label %10
10: ; preds = %7
%11 = call i64 (...) @setsid()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 0)
store i32 %17, i32* %3
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i32, i32* %3
%21 = call i32 @dup2(i32 %20, i32 0)
%22 = load i32, i32* %3
%23 = call i32 @dup2(i32 %22, i32 1)
%24 = load i32, i32* %3
%25 = call i32 @dup2(i32 %24, i32 2)
br label %26
26: ; preds = %19, %15
%27 = load i32, i32* %3
%28 = icmp sgt i32 %27, 2
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29, %26
br label %33
33: ; preds = %32, %7
%34 = load i32, i32* %2
store i32 %34, i32* %1
br label %35
35: ; preds = %33, %6
%36 = load i32, i32* %1
ret i32 %36
}

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