#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int background(void) {
  pid_t pid;
  pid = fork();
  if (pid < 0)
    return -1;
  if (pid > 0)
    exit(0);
  pid = fork();
  if (pid < 0)
    return -1;
  if (pid > 0)
    exit(0);
  setsid();
  chdir("/");
  umask(0);
  close(0);
  close(1);
  close(2);
  return 0;
}


x86 assembly (clang, -O0):
<background>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    118a <background+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <background+0xac>
cmpq   $0x0,-0x10(%rbp)
jle    119c <background+0x3c>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11be <background+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <background+0xac>
cmpq   $0x0,-0x10(%rbp)
jle    11d0 <background+0x70>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xe22(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<background>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     11b9 <background+0x59>
jne    11bd <background+0x5d>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11b9 <background+0x59>
jne    11bd <background+0x5d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe6e(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1070 <exit@plt>

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

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