#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*/ SIGHUP;
int /*<<< orphan*/ SIGTSTP;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int fork();
int getppid();
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int detach(void) {
  signal(SIGHUP, SIG_IGN);
  if (getppid() != 1) {
    int childpid;
    signal(SIGTTOU, SIG_IGN);
    signal(SIGTTIN, SIG_IGN);
    signal(SIGTSTP, SIG_IGN);
    if ((childpid = fork()) > 0) {
      return 0;
    }
    setsid();
  }
  close(0);
  umask(0);
  chdir("/");
  return 1;
}


x86 assembly (clang, -O0):
<detach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   1080 <getppid@plt>
cmp    $0x1,%eax
je     120a <detach+0x9a>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    1203 <detach+0x93>
movl   $0x0,-0x4(%rbp)
jmp    122b <detach+0xbb>
mov    $0x0,%al
call   1050 <setsid@plt>
xor    %edi,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xde1(%rip),%rdi
call   1030 <chdir@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach>:
push   %r14
push   %rbx
push   %rax
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%r14
mov    (%r14),%esi
call   1060 <signal@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
cmp    $0x1,%eax
je     11df <detach+0x6f>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1060 <signal@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1060 <signal@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jg     11fe <detach+0x8e>
xor    %eax,%eax
call   1050 <setsid@plt>
xor    %edi,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe0c(%rip),%rdi
call   1030 <chdir@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIGTTOU = global i32 0
@SIGTTIN = global i32 0
@SIGTSTP = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @detach() {
%1 = load i32, i32* @SIGHUP
%2 = load i32, i32* @SIG_IGN
%3 = tail call i32 @signal(i32 %1, i32 %2)
%4 = tail call i32 (...) @getppid()
%5 = icmp eq i32 %4, 1
br i1 %5, label %20, label %6
6: ; preds = %0
%7 = load i32, i32* @SIGTTOU
%8 = load i32, i32* @SIG_IGN
%9 = tail call i32 @signal(i32 %7, i32 %8)
%10 = load i32, i32* @SIGTTIN
%11 = load i32, i32* @SIG_IGN
%12 = tail call i32 @signal(i32 %10, i32 %11)
%13 = load i32, i32* @SIGTSTP
%14 = load i32, i32* @SIG_IGN
%15 = tail call i32 @signal(i32 %13, i32 %14)
%16 = tail call i32 (...) @fork()
%17 = icmp sgt i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %6
%19 = tail call i32 (...) @setsid()
br label %20
20: ; preds = %18, %0
%21 = tail call i32 @close(i32 0)
%22 = tail call i32 @umask(i32 0)
%23 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %6, %20
%25 = phi i32 [ 1, %20 ], [ 0, %6 ]
ret i32 %25
}