#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ setsid();
int xdaemon(const char *pid_file) {
  pid_t pid = fork();
  if (pid < 0)
    return -1;
  if (pid > 0) {
    if (pid_file) {
      FILE *fp = fopen(pid_file, "w");
      if (!fp) {
        perror(pid_file);
      } else {
        fprintf(fp, "%ld\n", (long)pid);
        fclose(fp);
      }
    }
    exit(EXIT_SUCCESS);
  }
  setsid();
  chdir("/");
  int fd = open("/dev/null", O_RDWR);
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<xdaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11de <xdaemon+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <xdaemon+0x10e>
cmpq   $0x0,-0x18(%rbp)
jle    1251 <xdaemon+0xa1>
cmpq   $0x0,-0x10(%rbp)
je     1243 <xdaemon+0x93>
mov    -0x10(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1221 <xdaemon+0x71>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
jmp    123e <xdaemon+0x8e>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdd2(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1243 <xdaemon+0x93>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
lea    0xda8(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
lea    0xd95(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xdaemon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     122d <xdaemon+0x7d>
jne    123f <xdaemon+0x8f>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xe2f(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1232 <xdaemon+0x82>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r14,%r14
je     1263 <xdaemon+0xb3>
mov    %rax,%rbx
lea    0xdb2(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
jne    1271 <xdaemon+0xc1>
mov    %r14,%rdi
call   10b0 <perror@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    %rax,%r15
lea    0xd87(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    1263 <xdaemon+0xb3>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"%ld\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @xdaemon(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i64 (...) @fork()
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %51
11: ; preds = %1
%12 = load i64, i64* %4
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %35
14: ; preds = %11
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %32
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp ne i32* %20, null
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = load i8*, i8** %3
%24 = call i32 @perror(i8* %23)
br label %31
25: ; preds = %17
%26 = load i32*, i32** %5
%27 = load i64, i64* %4
%28 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %27)
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %25, %22
br label %32
32: ; preds = %31, %14
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %11
%36 = call i32 (...) @setsid()
%37 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @O_RDWR
%39 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %38)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = load i32, i32* @STDIN_FILENO
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = load i32, i32* %6
%44 = load i32, i32* @STDOUT_FILENO
%45 = call i32 @dup2(i32 %43, i32 %44)
%46 = load i32, i32* %6
%47 = load i32, i32* @STDERR_FILENO
%48 = call i32 @dup2(i32 %46, i32 %47)
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
store i32 0, i32* %2
br label %51
51: ; preds = %35, %10
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"%ld\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @xdaemon(i8* %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %31, label %4
4: ; preds = %1
%5 = icmp eq i64 %2, 0
br i1 %5, label %19, label %6
6: ; preds = %4
%7 = icmp eq i8* %0, null
br i1 %7, label %16, label %8
8: ; preds = %6
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @perror(i8* %0)
br label %16
13: ; preds = %8
%14 = tail call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %2)
%15 = tail call i32 @fclose(i32* %9)
br label %16
16: ; preds = %11, %13, %6
%17 = load i32, i32* @EXIT_SUCCESS
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %4
%20 = tail call i32 (...) @setsid()
%21 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @STDIN_FILENO
%25 = tail call i32 @dup2(i32 %23, i32 %24)
%26 = load i32, i32* @STDOUT_FILENO
%27 = tail call i32 @dup2(i32 %23, i32 %26)
%28 = load i32, i32* @STDERR_FILENO
%29 = tail call i32 @dup2(i32 %23, i32 %28)
%30 = tail call i32 @close(i32 %23)
br label %31
31: ; preds = %1, %19
%32 = phi i32 [ 0, %19 ], [ -1, %1 ]
ret i32 %32
}