#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
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 errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(char *working_dir) {
  int ret_error;
  pid_t child_pid;
  int null_fd_rd, null_fd_wr;
  child_pid = fork();
  if (child_pid != -1) {
    if (child_pid > 0)
      exit(EXIT_SUCCESS);
    if (setsid() != -1) {
      signal(SIGCHLD, SIG_IGN);
      signal(SIGHUP, SIG_IGN);
      child_pid = fork();
      if (child_pid != -1) {
        if (child_pid > 0)
          exit(EXIT_SUCCESS);
        umask(0);
        chdir(working_dir);
        null_fd_rd = open("/dev/null", O_RDONLY);
        if (null_fd_rd != -1) {
          dup2(null_fd_rd, STDIN_FILENO);
          close(null_fd_rd);
        } else
          perror("alarm4pi daemon init error: could not open null device for "
                 "reading");
        null_fd_wr = open("/dev/null", O_WRONLY);
        if (null_fd_wr != -1) {
          dup2(null_fd_wr, STDERR_FILENO);
          dup2(null_fd_wr, STDOUT_FILENO);
          close(null_fd_wr);
        } else
          perror("alarm4pi daemon init error: could not open null device for "
                 "writing");
      } else {
        ret_error = errno;
        fprintf(stderr,
                "alarm4pi daemon init error: second fork failed. errno=%d\n",
                errno);
      }
    } else {
      ret_error = errno;
      fprintf(stderr,
              "alarm4pi daemon init error: child process could become session "
              "leader. errno=%d\n",
              errno);
    }
  } else {
    ret_error = errno;
    fprintf(stderr, "alarm4pi daemon init error: first fork failed. errno=%d\n",
            errno);
  }
  return (ret_error);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     136e <daemonize+0x1be>
cmpl   $0x0,-0x10(%rbp)
jle    11e8 <daemonize+0x38>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     133f <daemonize+0x18f>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     1310 <daemonize+0x160>
cmpl   $0x0,-0x10(%rbp)
jle    1252 <daemonize+0xa2>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    -0x8(%rbp),%rdi
call   1030 <chdir@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    0xd8e(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     12a2 <daemonize+0xf2>
mov    -0x14(%rbp),%edi
mov    0x2d0a(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    12ae <daemonize+0xfe>
lea    0xd61(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
lea    0xd42(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     12ff <daemonize+0x14f>
mov    -0x18(%rbp),%edi
mov    0x2cce(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    130b <daemonize+0x15b>
lea    0xd47(%rip),%rdi
call   10b0 <perror@plt>
jmp    133a <daemonize+0x18a>
mov    0x2c99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
mov    0x2c84(%rip),%rax
mov    (%rax),%edx
lea    0xd5b(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1369 <daemonize+0x1b9>
mov    0x2c6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
mov    0x2c55(%rip),%rax
mov    (%rax),%edx
lea    0xd66(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1398 <daemonize+0x1e8>
mov    0x2c3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2c37(%rip),%rax
mov    (%rax),%edi
mov    0x2c26(%rip),%rax
mov    (%rax),%edx
lea    0xd88(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     12ad <daemonize+0xfd>
test   %eax,%eax
jg     132c <daemonize+0x17c>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     12c8 <daemonize+0x118>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rbx
mov    (%rbx),%esi
call   1070 <signal@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     12e3 <daemonize+0x133>
test   %eax,%eax
jg     132c <daemonize+0x17c>
xor    %edi,%edi
call   1090 <umask@plt>
mov    %r14,%rdi
call   1030 <chdir@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xdc3(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     130d <daemonize+0x15d>
mov    %eax,%ebx
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
lea    0xd8c(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     131e <daemonize+0x16e>
mov    %eax,%ebx
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1303 <daemonize+0x153>
mov    0x2cfc(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
jmp    12fc <daemonize+0x14c>
mov    0x2ce1(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
jmp    12fc <daemonize+0x14c>
mov    0x2cc6(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
mov    %ebx,%edx
call   1080 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xcf6(%rip),%rdi
call   10b0 <perror@plt>
jmp    1264 <daemonize+0xb4>
lea    0xd28(%rip),%rdi
call   10b0 <perror@plt>
jmp    1303 <daemonize+0x153>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [67 x i8] c"alarm4pi daemon init error: could not open null device for reading\00"
@O_WRONLY = global i32 0
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [67 x i8] c"alarm4pi daemon init error: could not open null device for writing\00"
@errno = global i32 0
@stderr = global i32 0
@.str.3 = [58 x i8] c"alarm4pi daemon init error: second fork failed. errno=%d\0A\00"
@.str.4 = [81 x i8] c"alarm4pi daemon init error: child process could become session leader. errno=%d\0A\00"
@.str.5 = [57 x i8] c"alarm4pi daemon init error: first fork failed. errno=%d\0A\00"
define i32 @daemonize(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %80
10: ; preds = %1
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EXIT_SUCCESS
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %10
%17 = call i32 (...) @setsid()
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %74
19: ; preds = %16
%20 = load i32, i32* @SIGCHLD
%21 = load i32, i32* @SIG_IGN
%22 = call i32 @signal(i32 %20, i32 %21)
%23 = load i32, i32* @SIGHUP
%24 = load i32, i32* @SIG_IGN
%25 = call i32 @signal(i32 %23, i32 %24)
%26 = call i32 (...) @fork()
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %68
29: ; preds = %19
%30 = load i32, i32* %4
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %29
%36 = call i32 @umask(i32 0)
%37 = load i8*, i8** %2
%38 = call i32 @chdir(i8* %37)
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %39)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp ne i32 %41, -1
br i1 %42, label %43, label %49
43: ; preds = %35
%44 = load i32, i32* %5
%45 = load i32, i32* @STDIN_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = load i32, i32* %5
%48 = call i32 @close(i32 %47)
br label %51
49: ; preds = %35
%50 = call i32 @perror(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %43
%52 = load i32, i32* @O_WRONLY
%53 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp ne i32 %54, -1
br i1 %55, label %56, label %65
56: ; preds = %51
%57 = load i32, i32* %6
%58 = load i32, i32* @STDERR_FILENO
%59 = call i32 @dup2(i32 %57, i32 %58)
%60 = load i32, i32* %6
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i32 @dup2(i32 %60, i32 %61)
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
br label %67
65: ; preds = %51
%66 = call i32 @perror(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65, %56
br label %73
68: ; preds = %19
%69 = load i32, i32* @errno
store i32 %69, i32* %3
%70 = load i32, i32* @stderr
%71 = load i32, i32* @errno
%72 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %68, %67
br label %79
74: ; preds = %16
%75 = load i32, i32* @errno
store i32 %75, i32* %3
%76 = load i32, i32* @stderr
%77 = load i32, i32* @errno
%78 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.4, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %74, %73
br label %85
80: ; preds = %1
%81 = load i32, i32* @errno
store i32 %81, i32* %3
%82 = load i32, i32* @stderr
%83 = load i32, i32* @errno
%84 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %83)
br label %85
85: ; preds = %80, %79
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIGHUP = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [67 x i8] c"alarm4pi daemon init error: could not open null device for reading\00"
@O_WRONLY = global i32 0
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [67 x i8] c"alarm4pi daemon init error: could not open null device for writing\00"
@errno = global i32 0
@stderr = global i32 0
@.str.3 = [58 x i8] c"alarm4pi daemon init error: second fork failed. errno=%d\0A\00"
@.str.4 = [81 x i8] c"alarm4pi daemon init error: child process could become session leader. errno=%d\0A\00"
@.str.5 = [57 x i8] c"alarm4pi daemon init error: first fork failed. errno=%d\0A\00"
define i32 @daemonize(i8* %0) {
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, -1
br i1 %3, label %58, label %4
4: ; preds = %1
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %9
6: ; preds = %4
%7 = load i32, i32* @EXIT_SUCCESS
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %4
%10 = tail call i32 (...) @setsid()
%11 = icmp eq i32 %10, -1
br i1 %11, label %54, label %12
12: ; preds = %9
%13 = load i32, i32* @SIGCHLD
%14 = load i32, i32* @SIG_IGN
%15 = tail call i32 @signal(i32 %13, i32 %14)
%16 = load i32, i32* @SIGHUP
%17 = load i32, i32* @SIG_IGN
%18 = tail call i32 @signal(i32 %16, i32 %17)
%19 = tail call i32 (...) @fork()
%20 = icmp eq i32 %19, -1
br i1 %20, label %50, label %21
21: ; preds = %12
%22 = icmp sgt i32 %19, 0
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = load i32, i32* @EXIT_SUCCESS
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %21
%27 = tail call i32 @umask(i32 0)
%28 = tail call i32 @chdir(i8* %0)
%29 = load i32, i32* @O_RDONLY
%30 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %36, label %32
32: ; preds = %26
%33 = load i32, i32* @STDIN_FILENO
%34 = tail call i32 @dup2(i32 %30, i32 %33)
%35 = tail call i32 @close(i32 %30)
br label %38
36: ; preds = %26
%37 = tail call i32 @perror(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = load i32, i32* @O_WRONLY
%40 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %48, label %42
42: ; preds = %38
%43 = load i32, i32* @STDERR_FILENO
%44 = tail call i32 @dup2(i32 %40, i32 %43)
%45 = load i32, i32* @STDOUT_FILENO
%46 = tail call i32 @dup2(i32 %40, i32 %45)
%47 = tail call i32 @close(i32 %40)
br label %62
48: ; preds = %38
%49 = tail call i32 @perror(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
br label %62
50: ; preds = %12
%51 = load i32, i32* @errno
%52 = load i32, i32* @stderr
%53 = tail call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %51)
br label %62
54: ; preds = %9
%55 = load i32, i32* @errno
%56 = load i32, i32* @stderr
%57 = tail call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.4, i64 0, i64 0), i32 %55)
br label %62
58: ; preds = %1
%59 = load i32, i32* @errno
%60 = load i32, i32* @stderr
%61 = tail call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %59)
br label %62
62: ; preds = %54, %42, %48, %50, %58
%63 = phi i32 [ undef, %42 ], [ undef, %48 ], [ %51, %50 ], [ %55, %54 ], [ %59, %58 ]
ret i32 %63
}