#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*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int mydaemon(void) {
  pid_t pid;
  int fd;
  pid = fork();
  if (pid < 0) {
    syslog(LOG_ERR, "fork():%s", strerror(errno));
    return -1;
  }
  if (pid > 0)
    exit(0);
  fd = open("/dev/null", O_RDWR);
  if (fd < 0) {
    syslog(LOG_WARNING, "open():%s", strerror(errno));
    return -2;
  }
  dup2(fd, 0);
  dup2(fd, 1);
  dup2(fd, 2);
  setsid();
  chdir("/");
  umask(0);
  return 0;
}


x86 assembly (clang, -O0):
<mydaemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11e5 <mydaemon+0x55>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe2c(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <mydaemon+0x105>
cmpq   $0x0,-0x10(%rbp)
jle    11f7 <mydaemon+0x67>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe03(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1250 <mydaemon+0xc0>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xdd5(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1295 <mydaemon+0x105>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xd9c(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mydaemon>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11ff <mydaemon+0x6f>
jne    1262 <mydaemon+0xd2>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     122d <mydaemon+0x9d>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe2d(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <umask@plt>
jmp    1259 <mydaemon+0xc9>
mov    0x2dba(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde3(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1070 <syslog@plt>
mov    $0xffffffff,%ebx
jmp    1259 <mydaemon+0xc9>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebx
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc9(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1070 <syslog@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edi,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [10 x i8] c"fork():%s\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@LOG_WARNING = global i32 0
@.str.2 = [10 x i8] c"open():%s\00"
@.str.3 = [2 x i8] c"/\00"
define i32 @mydaemon() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = load i32, i32* @LOG_ERR
%5 = load i32, i32* @errno
%6 = tail call i32 @strerror(i32 %5)
%7 = tail call i32 @syslog(i32 %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %28
8: ; preds = %0
%9 = icmp eq i64 %1, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %8
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @LOG_WARNING
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %28
21: ; preds = %12
%22 = tail call i32 @dup2(i32 %14, i32 0)
%23 = tail call i32 @dup2(i32 %14, i32 1)
%24 = tail call i32 @dup2(i32 %14, i32 2)
%25 = tail call i32 (...) @setsid()
%26 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @umask(i32 0)
br label %28
28: ; preds = %21, %16, %3
%29 = phi i32 [ -1, %3 ], [ -2, %16 ], [ 0, %21 ]
ret i32 %29
}