#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_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
void detachFromTerminal(void) {
  if (setsid() == -1) {
    perror("setsid()");
    exit(EXIT_FAILURE);
  }
  if (freopen("/dev/null", "r", stdin) == NULL) {
    fprintf(stderr, "Unable to replace stdin with /dev/null: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (freopen("/dev/null", "w", stdout) == NULL) {
    fprintf(stderr, "Unable to replace stdout with /dev/null: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (freopen("/dev/null", "w", stderr) == NULL) {
    fprintf(stderr, "Unable to replace stderr with /dev/null: %s\n",
            strerror(errno));
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<detachFromTerminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1192 <detachFromTerminal+0x32>
lea    0xe81(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
lea    0xe67(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1030 <freopen@plt>
cmp    $0x0,%rax
jne    11f2 <detachFromTerminal+0x92>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x4(%rbp),%edi
mov    %rax,%rdx
lea    0xe36(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0xe07(%rip),%rdi
lea    0xe38(%rip),%rsi
call   1030 <freopen@plt>
cmp    $0x0,%rax
jne    1252 <detachFromTerminal+0xf2>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x8(%rbp),%edi
mov    %rax,%rdx
lea    0xe04(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
lea    0xda7(%rip),%rdi
lea    0xdd8(%rip),%rsi
call   1030 <freopen@plt>
cmp    $0x0,%rax
jne    12b2 <detachFromTerminal+0x152>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%rdx
lea    0xdd1(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detachFromTerminal>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <setsid@plt>
cmp    $0xffffffff,%eax
je     11de <detachFromTerminal+0x7e>
mov    0x2e71(%rip),%rax
mov    (%rax),%edx
lea    0xe89(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1030 <freopen@plt>
test   %rax,%rax
je     11ec <detachFromTerminal+0x8c>
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
lea    0xe68(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1030 <freopen@plt>
mov    0x2e24(%rip),%r14
mov    (%r14),%ebx
test   %rax,%rax
je     120c <detachFromTerminal+0xac>
lea    0xe46(%rip),%rdi
lea    0xe77(%rip),%rsi
mov    %ebx,%edx
call   1030 <freopen@plt>
test   %rax,%rax
je     1223 <detachFromTerminal+0xc3>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
jmp    1245 <detachFromTerminal+0xe5>
mov    0x2de5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0b(%rip),%rsi
jmp    123b <detachFromTerminal+0xdb>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe22(%rip),%rsi
jmp    123b <detachFromTerminal+0xdb>
mov    (%r14),%ebx
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe35(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"setsid()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@stderr = global i32 0
@.str.3 = [44 x i8] c"Unable to replace stdin with /dev/null: %s\0A\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.5 = [45 x i8] c"Unable to replace stdout with /dev/null: %s\0A\00"
@.str.6 = [45 x i8] c"Unable to replace stderr with /dev/null: %s\0A\00"
define void @detachFromTerminal() {
%1 = call i32 (...) @setsid()
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = load i32, i32* @stdin
%9 = call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %7
%19 = load i32, i32* @stdout
%20 = call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %29
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"setsid()\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32 0
@stderr = global i32 0
@.str.3 = [44 x i8] c"Unable to replace stdin with /dev/null: %s\0A\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.5 = [45 x i8] c"Unable to replace stdout with /dev/null: %s\0A\00"
@.str.6 = [45 x i8] c"Unable to replace stderr with /dev/null: %s\0A\00"
define void @detachFromTerminal() {
%1 = tail call i32 (...) @setsid()
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = load i32, i32* @stdin
%9 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %7
%19 = load i32, i32* @stdout
%20 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = icmp eq i32* %20, null
%22 = load i32, i32* @stderr
br i1 %21, label %23, label %29
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = tail call i32* @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %29
ret void
}