#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*/ O_RDWR;
int /*<<< orphan*/ SIGCLD;
int /*<<< orphan*/ SIGTSTP;
int /*<<< orphan*/ SIGTTIN;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getppid();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int setpgrp();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemon_start(int ignsigcld) {
  int childpid, fd;
  if (getppid() == 1)
    goto out;
  signal(SIGTTOU, SIG_IGN);
  signal(SIGTTIN, SIG_IGN);
  signal(SIGTSTP, SIG_IGN);
  if ((childpid = fork()) < 0) {
    return 0;
  } else if (childpid > 0) {
    exit(0);
  }
  if (setpgrp() == -1)
    return 0;
  if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
    ioctl(fd, TIOCNOTTY, NULL);
    close(fd);
  }
out:
  chdir("/");
  umask(0);
  if (ignsigcld)
    signal(SIGCLD, SIG_IGN);
  return 1;
}


x86 assembly (clang, -O0):
<daemon_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
cmp    $0x1,%eax
jne    11c0 <daemon_start+0x20>
jmp    1299 <daemon_start+0xf9>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1224 <daemon_start+0x84>
movl   $0x0,-0x4(%rbp)
jmp    12d4 <daemon_start+0x134>
cmpl   $0x0,-0xc(%rbp)
jle    1235 <daemon_start+0x95>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    123a <daemon_start+0x9a>
mov    $0x0,%al
call   10b0 <setpgrp@plt>
cmp    $0xffffffff,%eax
jne    1256 <daemon_start+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    12d4 <daemon_start+0x134>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
lea    0xd9a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jl     1294 <daemon_start+0xf4>
mov    -0x10(%rbp),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
jmp    1299 <daemon_start+0xf9>
lea    0xd69(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
cmpl   $0x0,-0x8(%rbp)
je     12cd <daemon_start+0x12d>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_start>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <getppid@plt>
cmp    $0x1,%eax
jne    11f2 <daemon_start+0x52>
lea    0xe4d(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x1,%ebp
test   %r14d,%r14d
je     1281 <daemon_start+0xe1>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
jmp    1281 <daemon_start+0xe1>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2db6(%rip),%rbx
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1060 <signal@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     1281 <daemon_start+0xe1>
jne    1288 <daemon_start+0xe8>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <setpgrp@plt>
cmp    $0xffffffff,%eax
je     1281 <daemon_start+0xe1>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0xdac(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11b5 <daemon_start+0x15>
mov    %eax,%ebp
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11b5 <daemon_start+0x15>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@SIGTTOU = global i32 0
@SIG_IGN = global i32 0
@SIGTTIN = global i32 0
@SIGTSTP = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@TIOCNOTTY = global i32 0
@.str.1 = [2 x i8] c"/\00"
@SIGCLD = global i32 0
define i32 @daemon_start(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i32 (...) @getppid()
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %9
8: ; preds = %1
br label %43
9: ; preds = %1
%10 = load i32, i32* @SIGTTOU
%11 = load i32, i32* @SIG_IGN
%12 = call i32 @signal(i32 %10, i32 %11)
%13 = load i32, i32* @SIGTTIN
%14 = load i32, i32* @SIG_IGN
%15 = call i32 @signal(i32 %13, i32 %14)
%16 = load i32, i32* @SIGTSTP
%17 = load i32, i32* @SIG_IGN
%18 = call i32 @signal(i32 %16, i32 %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %9
store i32 0, i32* %2
br label %53
22: ; preds = %9
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %22
br label %28
28: ; preds = %27
%29 = call i32 (...) @setpgrp()
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 0, i32* %2
br label %53
32: ; preds = %28
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 %34, i32* %5
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i32, i32* %5
%38 = load i32, i32* @TIOCNOTTY
%39 = call i32 @ioctl(i32 %37, i32 %38, i32* null)
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %36, %32
br label %43
43: ; preds = %42, %8
%44 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%45 = call i32 @umask(i32 0)
%46 = load i32, i32* %3
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32, i32* @SIGCLD
%50 = load i32, i32* @SIG_IGN
%51 = call i32 @signal(i32 %49, i32 %50)
br label %52
52: ; preds = %48, %43
store i32 1, i32* %2
br label %53
53: ; preds = %52, %31, %21
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@SIGTTOU = global i32 0
@SIG_IGN = global i32 0
@SIGTTIN = global i32 0
@SIGTSTP = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@TIOCNOTTY = global i32 0
@.str.1 = [2 x i8] c"/\00"
@SIGCLD = global i32 0
define i32 @daemon_start(i32 %0) {
%2 = tail call i32 (...) @getppid()
%3 = icmp eq i32 %2, 1
br i1 %3, label %31, label %4
4: ; preds = %1
%5 = load i32, i32* @SIGTTOU
%6 = load i32, i32* @SIG_IGN
%7 = tail call i32 @signal(i32 %5, i32 %6)
%8 = load i32, i32* @SIGTTIN
%9 = load i32, i32* @SIG_IGN
%10 = tail call i32 @signal(i32 %8, i32 %9)
%11 = load i32, i32* @SIGTSTP
%12 = load i32, i32* @SIG_IGN
%13 = tail call i32 @signal(i32 %11, i32 %12)
%14 = tail call i32 (...) @fork()
%15 = icmp slt i32 %14, 0
br i1 %15, label %39, label %16
16: ; preds = %4
%17 = icmp eq i32 %14, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %16
%21 = tail call i32 (...) @setpgrp()
%22 = icmp eq i32 %21, -1
br i1 %22, label %39, label %23
23: ; preds = %20
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @TIOCNOTTY
%29 = tail call i32 @ioctl(i32 %25, i32 %28, i32* null)
%30 = tail call i32 @close(i32 %25)
br label %31
31: ; preds = %23, %27, %1
%32 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = tail call i32 @umask(i32 0)
%34 = icmp eq i32 %0, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* @SIGCLD
%37 = load i32, i32* @SIG_IGN
%38 = tail call i32 @signal(i32 %36, i32 %37)
br label %39
39: ; preds = %31, %35, %20, %4
%40 = phi i32 [ 0, %4 ], [ 0, %20 ], [ 1, %35 ], [ 1, %31 ]
ret i32 %40
}