#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int access(char *, int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int remove(char *);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(void) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(EXIT_FAILURE);
  }
  if ((chdir("/etc/lifeline")) < 0) {
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  if (access("/var/lock/lifeline", F_OK) == -1) {
    int i, e;
    char ch[2];
    FILE *lock;
    FILE *log;
    lock = fopen("/var/lock/lifeline", "w");
    if (lock != NULL) {
      fputs("This daemon is currently running.\n", lock);
      fclose(lock);
      while (1) {
        i++;
        if (i > 20) {
          break;
        }
        sprintf(ch, "%d", i);
        log = fopen("/var/log/lifeline.log", "a");
        if (log != NULL) {
          fputs("Iteration: ", log);
          fputs(ch, log);
          fputs("\n", log);
          fclose(log);
        }
        sleep(10);
      }
      e = remove("/var/lock/lifeline");
      if (e > 0) {
        log = fopen("/var/log/lifeline.log", "a");
        fputs("Error: Unable to delete log file\n", log);
        fclose(log);
      }
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1203 <main+0x33>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    121c <main+0x4c>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1247 <main+0x77>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xdb2(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    126b <main+0x9b>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
lea    0xd69(%rip),%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
jne    13d3 <main+0x203>
lea    0xd54(%rip),%rdi
lea    0xd60(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     13ce <main+0x1fe>
mov    -0x30(%rbp),%rsi
lea    0xd43(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x14,-0x1c(%rbp)
jle    1306 <main+0x136>
jmp    1380 <main+0x1b0>
lea    -0x22(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xd32(%rip),%rsi
call   10c0 <sprintf@plt>
lea    0xd29(%rip),%rdi
lea    0xd38(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1371 <main+0x1a1>
mov    -0x38(%rbp),%rsi
lea    0xd1b(%rip),%rdi
call   1060 <fputs@plt>
lea    -0x22(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x38(%rbp),%rsi
lea    0xce1(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
jmp    12ee <main+0x11e>
lea    0xc87(%rip),%rdi
call   1030 <remove@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    13c9 <main+0x1f9>
lea    0xca9(%rip),%rdi
lea    0xcb8(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xcb2(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
jmp    13ce <main+0x1fe>
jmp    13d3 <main+0x203>
mov    0x2bee(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
js     122b <main+0x3b>
jne    1379 <main+0x189>
xor    %edi,%edi
call   10a0 <umask@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
test   %rax,%rax
js     122b <main+0x3b>
lea    0xddf(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jns    1239 <main+0x49>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
lea    0xd9b(%rip),%rdi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
jne    1379 <main+0x189>
lea    0xd86(%rip),%rdi
lea    0xd92(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1379 <main+0x189>
mov    %rax,%rbx
lea    0xd7c(%rip),%rdi
mov    $0x22,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10f0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xd7e(%rip),%rsi
lea    0xe(%rsp),%rdi
call   10d0 <sprintf@plt>
lea    0xd70(%rip),%rdi
lea    0xd7f(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1328 <main+0x138>
mov    %rax,%rbx
lea    0xd6d(%rip),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10f0 <fwrite@plt>
lea    0xe(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1090 <fputc@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xa,%edi
call   1100 <sleep@plt>
lea    0xcd5(%rip),%rdi
call   1030 <remove@plt>
test   %eax,%eax
jle    1379 <main+0x189>
lea    0xd00(%rip),%rdi
lea    0xd0f(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbx
lea    0xd0e(%rip),%rdi
mov    $0x21,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10f0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [14 x i8] c"/etc/lifeline\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [19 x i8] c"/var/lock/lifeline\00"
@F_OK = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"This daemon is currently running.\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [22 x i8] c"/var/log/lifeline.log\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [12 x i8] c"Iteration: \00"
@.str.8 = [2 x i8] c"\0A\00"
@.str.9 = [34 x i8] c"Error: Unable to delete log file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i8]
%7 = alloca i32*
%8 = alloca i32*
store i32 0, i32* %1
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = load i64, i64* %2
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %15
%22 = call i32 @umask(i32 0)
%23 = call i64 (...) @setsid()
store i64 %23, i64* %3
%24 = load i64, i64* %3
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = call i64 @chdir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %29
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @STDERR_FILENO
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @F_OK
%43 = call i32 @access(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %91
45: ; preds = %35
%46 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %46, i32** %7
%47 = load i32*, i32** %7
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %90
49: ; preds = %45
%50 = load i32*, i32** %7
%51 = call i32 @fputs(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32* %50)
%52 = load i32*, i32** %7
%53 = call i32 @fclose(i32* %52)
br label %54
54: ; preds = %49, %77
%55 = load i32, i32* %4
%56 = add nsw i32 %55, 1
store i32 %56, i32* %4
%57 = load i32, i32* %4
%58 = icmp sgt i32 %57, 20
br i1 %58, label %59, label %60
59: ; preds = %54
br label %79
60: ; preds = %54
%61 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%62 = load i32, i32* %4
%63 = call i32 @sprintf(i8* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %64, i32** %8
%65 = load i32*, i32** %8
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %77
67: ; preds = %60
%68 = load i32*, i32** %8
%69 = call i32 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32* %68)
%70 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%71 = load i32*, i32** %8
%72 = call i32 @fputs(i8* %70, i32* %71)
%73 = load i32*, i32** %8
%74 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32* %73)
%75 = load i32*, i32** %8
%76 = call i32 @fclose(i32* %75)
br label %77
77: ; preds = %67, %60
%78 = call i32 @sleep(i32 10)
br label %54
79: ; preds = %59
%80 = call i32 @remove(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 %80, i32* %5
%81 = load i32, i32* %5
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %89
83: ; preds = %79
%84 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %84, i32** %8
%85 = load i32*, i32** %8
%86 = call i32 @fputs(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i32* %85)
%87 = load i32*, i32** %8
%88 = call i32 @fclose(i32* %87)
br label %89
89: ; preds = %83, %79
br label %90
90: ; preds = %89, %45
br label %91
91: ; preds = %90, %35
%92 = load i32, i32* @EXIT_SUCCESS
%93 = call i32 @exit(i32 %92)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str = [14 x i8] c"/etc/lifeline\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [19 x i8] c"/var/lock/lifeline\00"
@F_OK = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"This daemon is currently running.\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [22 x i8] c"/var/log/lifeline.log\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [12 x i8] c"Iteration: \00"
@.str.9 = [34 x i8] c"Error: Unable to delete log file\0A\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = icmp eq i64 %2, 0
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = load i32, i32* @EXIT_SUCCESS
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %7
%13 = tail call i32 @umask(i32 0)
%14 = tail call i64 (...) @setsid()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
%20 = tail call i64 @chdir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %19
%26 = load i32, i32* @STDIN_FILENO
%27 = tail call i32 @close(i32 %26)
%28 = load i32, i32* @STDOUT_FILENO
%29 = tail call i32 @close(i32 %28)
%30 = load i32, i32* @STDERR_FILENO
%31 = tail call i32 @close(i32 %30)
%32 = load i32, i32* @F_OK
%33 = tail call i32 @access(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %59
35: ; preds = %25
%36 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%37 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %58, label %39
39: ; preds = %35
%40 = tail call i64 @fwrite(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i64 34, i64 1, i32* %37)
%41 = tail call i32 @fclose(i32* %37)
%42 = call i32 @sprintf(i8* %36, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 undef)
%43 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%44 = icmp eq i32* %43, null
br i1 %44, label %50, label %45
45: ; preds = %39
%46 = tail call i64 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i64 11, i64 1, i32* %43)
%47 = call i32 @fputs(i8* %36, i32* %43)
%48 = tail call i32 @fputc(i32 10, i32* %43)
%49 = tail call i32 @fclose(i32* %43)
br label %50
50: ; preds = %45, %39
%51 = tail call i32 @sleep(i32 10)
%52 = tail call i32 @remove(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%56 = tail call i64 @fwrite(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i64 33, i64 1, i32* %55)
%57 = tail call i32 @fclose(i32* %55)
br label %58
58: ; preds = %50, %54, %35
br label %59
59: ; preds = %58, %25
%60 = load i32, i32* @EXIT_SUCCESS
%61 = tail call i32 @exit(i32 %60)
unreachable
}