#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*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
void goto_background(void) {
  pid_t pid;
  pid = fork();
  if (pid == (pid_t)-1L) {
    perror("guard: fork() failed");
    exit(-1);
  }
  if (pid != (pid_t)0L)
    exit(0);
  setsid();
  pid = fork();
  if (pid == (pid_t)-1L) {
    perror("guard: fork() failed");
    exit(-1);
  }
  if (pid != (pid_t)0L)
    exit(0);
}


x86 assembly (clang, -O0):
<goto_background>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    1174 <goto_background+0x34>
lea    0xe9b(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     1186 <goto_background+0x46>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1030 <setsid@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    11b9 <goto_background+0x79>
lea    0xe56(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
je     11cb <goto_background+0x8b>
xor    %edi,%edi
call   1050 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<goto_background>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jne    1162 <goto_background+0x22>
xor    %eax,%eax
call   1030 <setsid@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jne    1162 <goto_background+0x22>
pop    %rax
ret
cmp    $0xffffffffffffffff,%rax
jne    117e <goto_background+0x3e>
lea    0xe91(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"guard: fork() failed\00"
define void @goto_background() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp eq i64 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %8
%14 = call i32 (...) @setsid()
%15 = call i64 (...) @fork()
store i64 %15, i64* %1
%16 = load i64, i64* %1
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %13
%22 = load i64, i64* %1
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %21
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"guard: fork() failed\00"
define void @goto_background() {
%1 = tail call i64 (...) @fork()
switch i64 %1, label %5 [
i64 -1, label %2
i64 0, label %7
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @exit(i32 -1)
unreachable
5: ; preds = %0
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = tail call i32 (...) @setsid()
%9 = tail call i64 (...) @fork()
switch i64 %9, label %13 [
i64 -1, label %10
i64 0, label %15
]
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %7
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %7
ret void
}