#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*/ fflush(int /*<<< orphan*/);
int fork();
int setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int daemonise() {
  int rc;
  fflush(stdout);
  fflush(stderr);
  rc = fork();
  if (rc < 0)
    return -1;
  if (rc > 0)
    exit(0);
  rc = setsid();
  if (rc < 0)
    return -1;
  return 1;
}


x86 assembly (clang, -O0):
<daemonise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1184 <daemonise+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <daemonise+0x7c>
cmpl   $0x0,-0x8(%rbp)
jle    1195 <daemonise+0x55>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1030 <setsid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b5 <daemonise+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <daemonise+0x7c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonise>:
push   %rax
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
js     1179 <daemonise+0x39>
jne    1180 <daemonise+0x40>
xor    %eax,%eax
call   1030 <setsid@plt>
sar    $0x1f,%eax
or     $0x1,%eax
pop    %rcx
ret
mov    $0xffffffff,%eax
pop    %rcx
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@stderr = global i32 0
define i32 @daemonise() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @stdout
%4 = call i32 @fflush(i32 %3)
%5 = load i32, i32* @stderr
%6 = call i32 @fflush(i32 %5)
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %22
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %11
%17 = call i32 (...) @setsid()
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %1
br label %22
21: ; preds = %16
store i32 1, i32* %1
br label %22
22: ; preds = %21, %20, %10
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@stdout = global i32 0
@stderr = global i32 0
define i32 @daemonise() {
%1 = load i32, i32* @stdout
%2 = tail call i32 @fflush(i32 %1)
%3 = load i32, i32* @stderr
%4 = tail call i32 @fflush(i32 %3)
%5 = tail call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %0
%8 = icmp eq i32 %5, 0
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %7
%12 = tail call i32 (...) @setsid()
%13 = icmp sgt i32 %12, -1
%14 = select i1 %13, i32 1, i32 -1
br label %15
15: ; preds = %11, %0
%16 = phi i32 [ -1, %0 ], [ %14, %11 ]
ret i32 %16
}