#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 dup(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getdtablesize();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
void do_background(void) {
  int i;
  printf("Janus is now going in foreground, use SIGTERM to stop it.\n");
  if (fork())
    exit(0);
  setsid();
  for (i = getdtablesize(); i >= 0; --i)
    close(i);
  if ((i = open("/dev/null", O_RDWR)) != 0 || dup(i) != 1 || dup(i) != 2) {
    printf("error while closing stdin, stdout and stderr\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<do_background>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     11ac <do_background+0x2c>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    $0x0,%al
call   10a0 <getdtablesize@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     11dd <do_background+0x5d>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <do_background+0x3d>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jne    1220 <do_background+0xa0>
mov    -0x4(%rbp),%edi
call   1040 <dup@plt>
cmp    $0x1,%eax
jne    1220 <do_background+0xa0>
mov    -0x4(%rbp),%edi
call   1040 <dup@plt>
cmp    $0x2,%eax
je     1236 <do_background+0xb6>
lea    0xe1e(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_background>:
push   %rbx
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1205 <do_background+0x85>
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %eax,%eax
call   10a0 <getdtablesize@plt>
test   %eax,%eax
js     11bc <do_background+0x3c>
mov    %eax,%ebx
nopl   (%rax)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0xffffffff,%ebx
jb     11b0 <do_background+0x30>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jne    11ef <do_background+0x6f>
xor    %edi,%edi
call   1040 <dup@plt>
cmp    $0x1,%eax
jne    11ef <do_background+0x6f>
xor    %edi,%edi
call   1040 <dup@plt>
cmp    $0x2,%eax
jne    11ef <do_background+0x6f>
pop    %rbx
ret
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [59 x i8] c"Janus is now going in foreground, use SIGTERM to stop it.\0A\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.2 = [46 x i8] c"error while closing stdin, stdout and stderr\0A\00"
define void @do_background() {
%1 = alloca i32
%2 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%3 = call i64 (...) @fork()
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = call i32 (...) @setsid()
%9 = call i32 (...) @getdtablesize()
store i32 %9, i32* %1
br label %10
10: ; preds = %16, %7
%11 = load i32, i32* %1
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* %1
%15 = call i32 @close(i32 %14)
br label %16
16: ; preds = %13
%17 = load i32, i32* %1
%18 = add nsw i32 %17, -1
store i32 %18, i32* %1
br label %10
19: ; preds = %10
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 %21, i32* %1
%22 = icmp ne i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %19
%24 = load i32, i32* %1
%25 = call i32 @dup(i32 %24)
%26 = icmp ne i32 %25, 1
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i32, i32* %1
%29 = call i32 @dup(i32 %28)
%30 = icmp ne i32 %29, 2
br i1 %30, label %31, label %34
31: ; preds = %27, %23, %19
%32 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
ret void
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@str = [58 x i8] c"Janus is now going in foreground, use SIGTERM to stop it.\00"
@str.3 = [45 x i8] c"error while closing stdin, stdout and stderr\00"
define void @do_background() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %0
%7 = tail call i32 (...) @setsid()
%8 = tail call i32 (...) @getdtablesize()
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %6, %10
%11 = phi i32 [ %13, %10 ], [ %8, %6 ]
%12 = tail call i32 @close(i32 %11)
%13 = add nsw i32 %11, -1
%14 = icmp eq i32 %11, 0
br i1 %14, label %15, label %10
15: ; preds = %10, %6
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = tail call i32 @dup(i32 0)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @dup(i32 0)
%24 = icmp eq i32 %23, 2
br i1 %24, label %28, label %25
25: ; preds = %22, %19, %15
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.3, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %22
ret void
}