#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*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fill_first_3_handles();
int fork();
int /*<<< orphan*/ setsid();
void go_background() {
  int pid;
  pid = fork();
  switch (pid) {
  case -1:
    error("fork failed");
    break;
  case 0:
    close(0);
    close(1);
    close(2);
    fill_first_3_handles();
    setsid();
    break;
  default:
    exit(0);
  }
}


x86 assembly (clang, -O0):
<go_background>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
sub    $0xffffffff,%eax
je     1196 <go_background+0x36>
jmp    1186 <go_background+0x26>
mov    -0x8(%rbp),%eax
test   %eax,%eax
je     11a7 <go_background+0x47>
jmp    11d5 <go_background+0x75>
lea    0xe63(%rip),%rdi
call   1060 <error@plt>
jmp    11dc <go_background+0x7c>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1030 <fill_first_3_handles@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
jmp    11dc <go_background+0x7c>
xor    %edi,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<go_background>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     117e <go_background+0x1e>
cmp    $0xffffffff,%eax
jne    11a8 <go_background+0x48>
lea    0xe88(%rip),%rdi
pop    %rax
jmp    1060 <error@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1030 <fill_first_3_handles@plt>
xor    %eax,%eax
pop    %rcx
jmp    1050 <setsid@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
define void @go_background() {
%1 = alloca i32
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
switch i32 %3, label %12 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = call i32 @error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %14
6: ; preds = %0
%7 = call i32 @close(i32 0)
%8 = call i32 @close(i32 1)
%9 = call i32 @close(i32 2)
%10 = call i32 (...) @fill_first_3_handles()
%11 = call i32 (...) @setsid()
br label %14
12: ; preds = %0
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %6, %4
ret void
}

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