#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void app_daemon(void) {
  int fr = 0;
  fr = fork();
  if (fr < 0) {
    fprintf(stderr, "fork() failed\n");
    exit(1);
  }
  if (fr > 0) {
    exit(0);
  }
  if (setsid() < 0) {
    fprintf(stderr, "setsid() failed\n");
    exit(1);
  }
  fr = fork();
  if (fr < 0) {
    fprintf(stderr, "fork() failed\n");
    exit(1);
  }
  if (fr > 0) {
    exit(0);
  }
  umask(0);
  chdir("/");
  close(0);
  close(1);
  close(2);
  open("/dev/null", O_RDWR);
  dup(0);
  dup(0);
}


x86 assembly (clang, -O0):
<app_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11d2 <app_daemon+0x42>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11e3 <app_daemon+0x53>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0x0,%rax
jge    1213 <app_daemon+0x83>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1246 <app_daemon+0xb6>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1257 <app_daemon+0xc7>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xdbb(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
lea    0xd8d(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<app_daemon>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1209 <app_daemon+0x79>
jne    121b <app_daemon+0x8b>
xor    %eax,%eax
call   1060 <setsid@plt>
test   %rax,%rax
js     1222 <app_daemon+0x92>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1209 <app_daemon+0x79>
jne    121b <app_daemon+0x8b>
xor    %edi,%edi
call   1080 <umask@plt>
lea    0xe5b(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1090 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
pop    %rax
jmp    1040 <dup@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
jmp    1232 <app_daemon+0xa2>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"fork() failed\0A\00"
@.str.1 = [17 x i8] c"setsid() failed\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @app_daemon() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 (...) @fork()
store i32 %2, i32* %1
%3 = load i32, i32* %1
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %9
%15 = call i64 (...) @setsid()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 (...) @fork()
store i32 %22, i32* %1
%23 = load i32, i32* %1
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* %1
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %29
%35 = call i32 @umask(i32 0)
%36 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @close(i32 0)
%38 = call i32 @close(i32 1)
%39 = call i32 @close(i32 2)
%40 = load i32, i32* @O_RDWR
%41 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = call i32 @dup(i32 0)
%43 = call i32 @dup(i32 0)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"fork() failed\0A\00"
@.str.1 = [17 x i8] c"setsid() failed\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @app_daemon() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = icmp eq i32 %1, 0
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %7
%12 = tail call i64 (...) @setsid()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = tail call i32 (...) @fork()
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %18
%26 = icmp eq i32 %19, 0
br i1 %26, label %29, label %27
27: ; preds = %25
%28 = tail call i32 @exit(i32 0)
unreachable
29: ; preds = %25
%30 = tail call i32 @umask(i32 0)
%31 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @close(i32 0)
%33 = tail call i32 @close(i32 1)
%34 = tail call i32 @close(i32 2)
%35 = load i32, i32* @O_RDWR
%36 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = tail call i32 @dup(i32 0)
%38 = tail call i32 @dup(i32 0)
ret void
}