#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
void my_daemon() {
  pid_t pid = fork();
  if (pid > 0) {
    exit(1);
  }
  setsid();
  if (chdir("/") < 0) {
    perror("chdir");
    return;
  }
  close(0);
  close(1);
  close(2);
  signal(SIGCHLD, SIG_IGN);
}


x86 assembly (clang, -O0):
<my_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jle    1198 <my_daemon+0x28>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xe5a(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    11c6 <my_daemon+0x56>
lea    0xe46(%rip),%rdi
call   1070 <perror@plt>
jmp    11f8 <my_daemon+0x88>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_daemon>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jg     11d5 <my_daemon+0x65>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe75(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11c8 <my_daemon+0x58>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
pop    %rax
jmp    1060 <signal@plt>
lea    0xe33(%rip),%rdi
pop    %rax
jmp    1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"chdir\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
define void @my_daemon() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp sgt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = call i32 (...) @setsid()
%9 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
13: ; preds = %7
%14 = call i32 @close(i32 0)
%15 = call i32 @close(i32 1)
%16 = call i32 @close(i32 2)
%17 = load i32, i32* @SIGCHLD
%18 = load i32, i32* @SIG_IGN
%19 = call i32 @signal(i32 %17, i32 %18)
br label %20
20: ; preds = %13, %11
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [6 x i8] c"chdir\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
define void @my_daemon() {
%1 = tail call i64 (...) @fork()
%2 = icmp sgt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 1)
unreachable
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
%7 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %18
11: ; preds = %5
%12 = tail call i32 @close(i32 0)
%13 = tail call i32 @close(i32 1)
%14 = tail call i32 @close(i32 2)
%15 = load i32, i32* @SIGCHLD
%16 = load i32, i32* @SIG_IGN
%17 = tail call i32 @signal(i32 %15, i32 %16)
br label %18
18: ; preds = %11, %9
ret void
}