#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 O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int errno;
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
scalar_t__ getpid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void daemonize(char *exe, const char *log) {
  char logfile[1024] = "";
  strcat(logfile, log);
  pid_t pid = fork();
  if (pid == -1) {
    fprintf(stderr, "failed to fork while daemonising (errno=%d)\n", errno);
  } else if (pid != 0) {
    _exit(0);
  }
  if (setsid() == -1) {
    fprintf(stderr,
            "failed to become a session leader while "
            "daemonising(errno=%d)\n",
            errno);
  }
  signal(SIGHUP, SIG_IGN);
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "failed to fork while daemonising (errno=%d)\n", errno);
  } else if (pid != 0) {
    _exit(0);
  }
  if (chdir("/") == -1) {
    fprintf(stderr,
            "failed to change working directory while "
            "daemonising (errno=%d)\n",
            errno);
  }
  printf("pid: %d\n", (int)getpid());
  umask(0);
  int logfile_fileno =
      open(logfile, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP);
  if (logfile_fileno == -1) {
    fprintf(stderr, "failed to open logfile (errno=%d), %s\n", errno, logfile);
  }
  dup2(logfile_fileno, STDOUT_FILENO);
  dup2(logfile_fileno, STDERR_FILENO);
  close(STDIN_FILENO);
  freopen("/dev/null", "w+", stdin);
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1100 <strcat@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    1265 <daemonize+0x75>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
jmp    127e <daemonize+0x8e>
cmpl   $0x0,-0x414(%rbp)
je     1279 <daemonize+0x89>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    127e <daemonize+0x8e>
mov    $0x0,%al
call   10b0 <setsid@plt>
cmp    $0xffffffff,%eax
jne    12ae <daemonize+0xbe>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%edx
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
call   10c0 <signal@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    1304 <daemonize+0x114>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
mov    0x2cb1(%rip),%rax
mov    (%rax),%edx
lea    0xd08(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
jmp    131d <daemonize+0x12d>
cmpl   $0x0,-0x414(%rbp)
je     1318 <daemonize+0x128>
xor    %edi,%edi
call   1030 <_exit@plt>
jmp    131d <daemonize+0x12d>
lea    0xd48(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1352 <daemonize+0x162>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
mov    0x2c5e(%rip),%rax
mov    (%rax),%edx
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd4d(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
call   10e0 <umask@plt>
lea    -0x410(%rbp),%rdi
mov    0x2c54(%rip),%rax
mov    (%rax),%esi
mov    0x2c73(%rip),%rax
or     (%rax),%esi
mov    0x2be2(%rip),%rax
or     (%rax),%esi
mov    0x2bf1(%rip),%rax
mov    (%rax),%edx
mov    0x2c20(%rip),%rax
or     (%rax),%edx
mov    0x2be7(%rip),%rax
or     (%rax),%edx
call   10f0 <open@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x418(%rbp)
jne    13ea <daemonize+0x1fa>
mov    0x2bde(%rip),%rax
mov    (%rax),%edi
mov    0x2bcd(%rip),%rax
mov    (%rax),%edx
lea    -0x410(%rbp),%rcx
lea    0xcd5(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    -0x418(%rbp),%edi
mov    0x2be9(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    -0x418(%rbp),%edi
mov    0x2b8d(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    0x2b67(%rip),%rax
mov    (%rax),%edi
call   10a0 <close@plt>
mov    0x2ba1(%rip),%rax
mov    (%rax),%edx
lea    0xcaf(%rip),%rdi
lea    0xcb2(%rip),%rsi
call   1090 <freopen@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
mov    %rsp,%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1100 <strcat@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     124e <daemonize+0x5e>
cmp    $0xffffffff,%eax
jne    13cf <daemonize+0x1df>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
lea    0xdb9(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
xor    %eax,%eax
call   10b0 <setsid@plt>
cmp    $0xffffffff,%eax
jne    127a <daemonize+0x8a>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
lea    0xdba(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
call   10c0 <signal@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     12c5 <daemonize+0xd5>
cmp    $0xffffffff,%eax
jne    13cf <daemonize+0x1df>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%edx
lea    0xd42(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
lea    0xda0(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    12f6 <daemonize+0x106>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
mov    0x2cba(%rip),%rax
mov    (%rax),%edx
lea    0xd7f(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdab(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
xor    %edi,%edi
call   10e0 <umask@plt>
mov    0x2cb7(%rip),%rax
mov    0x2cd8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2c45(%rip),%rax
or     (%rax),%esi
mov    0x2c54(%rip),%rax
mov    0x2c85(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2c4a(%rip),%rax
or     (%rax),%edx
mov    %rsp,%rdi
call   10f0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    137a <daemonize+0x18a>
mov    0x2c4a(%rip),%rax
mov    (%rax),%edi
mov    0x2c39(%rip),%rax
mov    (%rax),%edx
lea    0xd48(%rip),%rsi
mov    %rsp,%rcx
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    0x2c5f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
mov    0x2c07(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
mov    0x2bdf(%rip),%rax
mov    (%rax),%edi
call   10a0 <close@plt>
mov    0x2c19(%rip),%rax
mov    (%rax),%edx
lea    0xd27(%rip),%rdi
lea    0xd2a(%rip),%rsi
call   1090 <freopen@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"failed to fork while daemonising (errno=%d)\0A\00"
@errno = global i32 0
@.str.1 = [63 x i8] c"failed to become a session leader while daemonising(errno=%d)\0A\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [65 x i8] c"failed to change working directory while daemonising (errno=%d)\0A\00"
@.str.4 = [9 x i8] c"pid: %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.5 = [39 x i8] c"failed to open logfile (errno=%d), %s\0A\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.6 = [10 x i8] c"/dev/null\00"
@.str.7 = [3 x i8] c"w+\00"
@stdin = global i32 0
define void @daemonize(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = bitcast [1024 x i8]* %5 to i8*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = call i32 @strcat(i8* %9, i8* %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %25
19: ; preds = %2
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @_exit(i32 0)
unreachable
24: ; preds = %19
br label %25
25: ; preds = %24, %15
%26 = call i32 (...) @setsid()
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28, %25
%33 = load i32, i32* @SIGHUP
%34 = load i32, i32* @SIG_IGN
%35 = call i32 @signal(i32 %33, i32 %34)
%36 = call i32 (...) @fork()
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %41)
br label %49
43: ; preds = %32
%44 = load i32, i32* %6
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @_exit(i32 0)
unreachable
48: ; preds = %43
br label %49
49: ; preds = %48, %39
%50 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %52, %49
%57 = call i64 (...) @getpid()
%58 = trunc i64 %57 to i32
%59 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = call i32 @umask(i32 0)
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%62 = load i32, i32* @O_RDWR
%63 = load i32, i32* @O_CREAT
%64 = or i32 %62, %63
%65 = load i32, i32* @O_APPEND
%66 = or i32 %64, %65
%67 = load i32, i32* @S_IRUSR
%68 = load i32, i32* @S_IWUSR
%69 = or i32 %67, %68
%70 = load i32, i32* @S_IRGRP
%71 = or i32 %69, %70
%72 = call i32 @open(i8* %61, i32 %66, i32 %71)
store i32 %72, i32* %7
%73 = load i32, i32* %7
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %80
75: ; preds = %56
%76 = load i32, i32* @stderr
%77 = load i32, i32* @errno
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%79 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %77, i8* %78)
br label %80
80: ; preds = %75, %56
%81 = load i32, i32* %7
%82 = load i32, i32* @STDOUT_FILENO
%83 = call i32 @dup2(i32 %81, i32 %82)
%84 = load i32, i32* %7
%85 = load i32, i32* @STDERR_FILENO
%86 = call i32 @dup2(i32 %84, i32 %85)
%87 = load i32, i32* @STDIN_FILENO
%88 = call i32 @close(i32 %87)
%89 = load i32, i32* @stdin
%90 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 %89)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"failed to fork while daemonising (errno=%d)\0A\00"
@errno = global i32 0
@.str.1 = [63 x i8] c"failed to become a session leader while daemonising(errno=%d)\0A\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [65 x i8] c"failed to change working directory while daemonising (errno=%d)\0A\00"
@.str.4 = [9 x i8] c"pid: %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.5 = [39 x i8] c"failed to open logfile (errno=%d), %s\0A\00"
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.6 = [10 x i8] c"/dev/null\00"
@.str.7 = [3 x i8] c"w+\00"
@stdin = global i32 0
define void @daemonize(i8* nocapture readnone %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcat(i8* %4, i8* %1)
%6 = call i32 (...) @fork()
switch i32 %6, label %11 [
i32 -1, label %7
i32 0, label %13
]
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %13
11: ; preds = %2
%12 = call i32 @_exit(i32 0)
unreachable
13: ; preds = %2, %7
%14 = call i32 (...) @setsid()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %16, %13
%21 = load i32, i32* @SIGHUP
%22 = load i32, i32* @SIG_IGN
%23 = call i32 @signal(i32 %21, i32 %22)
%24 = call i32 (...) @fork()
switch i32 %24, label %29 [
i32 -1, label %25
i32 0, label %31
]
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %31
29: ; preds = %20
%30 = call i32 @_exit(i32 0)
unreachable
31: ; preds = %20, %25
%32 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %31
%39 = call i64 (...) @getpid()
%40 = trunc i64 %39 to i32
%41 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = call i32 @umask(i32 0)
%43 = load i32, i32* @O_RDWR
%44 = load i32, i32* @O_CREAT
%45 = or i32 %44, %43
%46 = load i32, i32* @O_APPEND
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IRUSR
%49 = load i32, i32* @S_IWUSR
%50 = or i32 %49, %48
%51 = load i32, i32* @S_IRGRP
%52 = or i32 %50, %51
%53 = call i32 @open(i8* %4, i32 %47, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %38
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %57, i8* %4)
br label %59
59: ; preds = %55, %38
%60 = load i32, i32* @STDOUT_FILENO
%61 = call i32 @dup2(i32 %53, i32 %60)
%62 = load i32, i32* @STDERR_FILENO
%63 = call i32 @dup2(i32 %53, i32 %62)
%64 = load i32, i32* @STDIN_FILENO
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* @stdin
%67 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 %66)
ret void
}