#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*/ errno;
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 *, ...);
int /*<<< orphan*/ *freopen(char const *, char *, int /*<<< orphan*/ *);
scalar_t__ setsid();
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int daemonize(const char *pidfile, const char *logfile) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "fork: %s\n", strerror(errno));
    return -1;
  }
  if (pid > 0) {
    FILE *fp = fopen(pidfile, "w");
    if (fp == NULL) {
      fprintf(stderr, "Invalid pid file\n");
    } else {
      fprintf(fp, "%d", pid);
      fclose(fp);
    }
    exit(EXIT_SUCCESS);
  }
  umask(0);
  if (setsid() < 0) {
    fprintf(stderr, "setsid: %s\n", strerror(errno));
    return -1;
  }
  fclose(stdin);
  FILE *fp;
  fp = freopen(logfile, "w", stdout);
  if (fp == NULL) {
    fprintf(stderr, "freopen: %s\n", strerror(errno));
    return -1;
  }
  fp = freopen(logfile, "w", stderr);
  if (fp == NULL) {
    fprintf(stderr, "freopen: %s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11f3 <daemonize+0x63>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rdx
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <daemonize+0x20e>
cmpq   $0x0,-0x20(%rbp)
jle    1267 <daemonize+0xd7>
mov    -0x10(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123a <daemonize+0xaa>
mov    0x2db4(%rip),%rax
mov    (%rax),%rdi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1259 <daemonize+0xc9>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
cmp    $0x0,%rax
jge    12bc <daemonize+0x12c>
mov    0x2d52(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rdx
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <daemonize+0x20e>
mov    0x2d25(%rip),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    0x2cf2(%rip),%rax
mov    (%rax),%rdx
lea    0xd2a(%rip),%rsi
call   1040 <freopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1331 <daemonize+0x1a1>
mov    0x2cdd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <daemonize+0x20e>
mov    -0x18(%rbp),%rdi
mov    0x2c9c(%rip),%rax
mov    (%rax),%rdx
lea    0xcc4(%rip),%rsi
call   1040 <freopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1397 <daemonize+0x207>
mov    0x2c77(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rdx
lea    0xca9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <daemonize+0x20e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemonize>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1245 <daemonize+0xa5>
jne    129d <daemonize+0xfd>
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   1050 <setsid@plt>
test   %rax,%rax
js     1266 <daemonize+0xc6>
mov    0x2e09(%rip),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%rdx
lea    0xe12(%rip),%rsi
mov    %r14,%rdi
call   1040 <freopen@plt>
mov    0x2dd1(%rip),%r15
mov    (%r15),%rbx
test   %rax,%rax
je     122e <daemonize+0x8e>
lea    0xdf4(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <freopen@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1297 <daemonize+0xf7>
mov    (%r15),%rbx
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdea(%rip),%rsi
jmp    1285 <daemonize+0xe5>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rbx
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd9c(%rip),%rsi
jmp    1285 <daemonize+0xe5>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rbx
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd9c(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rax,%rbx
lea    0xd63(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
jne    12d6 <daemonize+0x136>
mov    0x2d1d(%rip),%rax
mov    (%rax),%rcx
lea    0xd47(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
jmp    12f5 <daemonize+0x155>
mov    %rax,%r14
lea    0xd3e(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32* null
@.str = [10 x i8] c"fork: %s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [18 x i8] c"Invalid pid file\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [12 x i8] c"setsid: %s\0A\00"
@stdin = global i32* null
@stdout = global i32* null
@.str.5 = [13 x i8] c"freopen: %s\0A\00"
define i32 @daemonize(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32*, i32** @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %71
17: ; preds = %2
%18 = load i64, i64* %6
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %7
%23 = load i32*, i32** %7
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32*, i32** @stderr
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %34
28: ; preds = %20
%29 = load i32*, i32** %7
%30 = load i64, i64* %6
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 %30)
%32 = load i32*, i32** %7
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %28, %25
%35 = load i32, i32* @EXIT_SUCCESS
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %17
%38 = call i32 @umask(i32 0)
%39 = call i64 (...) @setsid()
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32*, i32** @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %44)
store i32 -1, i32* %3
br label %71
46: ; preds = %37
%47 = load i32*, i32** @stdin
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %5
%50 = load i32*, i32** @stdout
%51 = call i32* @freopen(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %50)
store i32* %51, i32** %8
%52 = load i32*, i32** %8
%53 = icmp eq i32* %52, null
br i1 %53, label %54, label %59
54: ; preds = %46
%55 = load i32*, i32** @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %57)
store i32 -1, i32* %3
br label %71
59: ; preds = %46
%60 = load i8*, i8** %5
%61 = load i32*, i32** @stderr
%62 = call i32* @freopen(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %61)
store i32* %62, i32** %8
%63 = load i32*, i32** %8
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %70
65: ; preds = %59
%66 = load i32*, i32** @stderr
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 (i32*, i8*, ...) @fprintf(i32* %66, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %68)
store i32 -1, i32* %3
br label %71
70: ; preds = %59
store i32 0, i32* %3
br label %71
71: ; preds = %70, %65, %54, %41, %12
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [10 x i8] c"fork: %s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [18 x i8] c"Invalid pid file\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [12 x i8] c"setsid: %s\0A\00"
@stdin = global i32* null
@stdout = global i32* null
@.str.5 = [13 x i8] c"freopen: %s\0A\00"
define i32 @daemonize(i8* nocapture %0, i8* %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32*, i32** @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %52
10: ; preds = %2
%11 = icmp eq i64 %3, 0
br i1 %11, label %24, label %12
12: ; preds = %10
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32*, i32** @stderr
%17 = tail call i64 @fwrite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 17, i64 1, i32* %16)
br label %21
18: ; preds = %12
%19 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 %3)
%20 = tail call i32 @fclose(i32* %13)
br label %21
21: ; preds = %18, %15
%22 = load i32, i32* @EXIT_SUCCESS
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %10
%25 = tail call i32 @umask(i32 0)
%26 = tail call i64 (...) @setsid()
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32*, i32** @stderr
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %31)
br label %52
33: ; preds = %24
%34 = load i32*, i32** @stdin
%35 = tail call i32 @fclose(i32* %34)
%36 = load i32*, i32** @stdout
%37 = tail call i32* @freopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %36)
%38 = icmp eq i32* %37, null
%39 = load i32*, i32** @stderr
br i1 %38, label %40, label %44
40: ; preds = %33
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %42)
br label %52
44: ; preds = %33
%45 = tail call i32* @freopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %39)
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i32*, i32** @stderr
%49 = load i32, i32* @errno
%50 = tail call i8* @strerror(i32 %49)
%51 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %40, %47, %44, %28, %5
%53 = phi i32 [ -1, %5 ], [ -1, %28 ], [ -1, %40 ], [ -1, %47 ], [ 0, %44 ]
ret i32 %53
}