#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int checkPamUser(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ delete_all();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getLoggedusers(int /*<<< orphan*/ *);
char *getUserName(int /*<<< orphan*/);
int list_count();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ remove(char *);
scalar_t__ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int make_daemon() {
  FILE *fp = NULL;
  FILE *fp_pid = NULL;
  pid_t process_id = 0;
  pid_t sid = 0;
  process_id = fork();
  if (process_id < 0) {
    printf("fork failed!\n");
    exit(1);
  }
  if (process_id > 0) {
    fp_pid = fopen("/tmp/clepsydrad.pid", "w+");
    fprintf(fp_pid, "%d\n", process_id);
    fflush(fp_pid);
    fclose(fp_pid);
    printf("process_id of daemon process %d \n", process_id);
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(1);
  }
  chdir("/tmp");
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  fp = fopen("/tmp/clepsydrad.log", "w+");
  char *p = NULL;
  int countToDie = 0;
  while (1) {
    sleep(1);
    countToDie++;
    if (countToDie > 15) {
      break;
    } else {
      int loggedC = getLoggedusers(fp);
      fprintf(fp, " pam cnt of logged users... %d\n", loggedC);
      fflush(fp);
    }
  }
  fflush(fp);
  fprintf(fp, " First user in list is .. [%s] \n", getUserName(0));
  fprintf(fp, " pam check status  .. [%d] \n", checkPamUser(getUserName(0)));
  fprintf(fp, "list contains [%d] count of items...\n", list_count());
  delete_all();
  fprintf(fp, "Quit clepsydrad, bye !...\n");
  fclose(fp);
  remove("/tmp/clepsydrad.pid");
  return (0);
}


x86 assembly (clang, -O0):
<make_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1140 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1276 <make_daemon+0x56>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    12d9 <make_daemon+0xb9>
lea    0xd86(%rip),%rdi
lea    0xd93(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <fflush@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rsi
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
xor    %edi,%edi
call   1110 <exit@plt>
xor    %edi,%edi
call   10c0 <umask@plt>
mov    $0x0,%al
call   10a0 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1300 <make_daemon+0xe0>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xd40(%rip),%rdi
call   1070 <chdir@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
call   1090 <close@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1090 <close@plt>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   1090 <close@plt>
lea    0xd0f(%rip),%rdi
lea    0xcde(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x1,%edi
call   1130 <sleep@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0xf,-0x2c(%rbp)
jle    137e <make_daemon+0x15e>
jmp    13ad <make_daemon+0x18d>
mov    -0x8(%rbp),%rdi
call   1050 <getLoggedusers@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xcc8(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   10d0 <fflush@plt>
jmp    135c <make_daemon+0x13c>
mov    -0x8(%rbp),%rdi
call   10d0 <fflush@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
xor    %edi,%edi
call   10e0 <getUserName@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xcad(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %edi,%edi
call   10e0 <getUserName@plt>
mov    %rax,%rdi
call   1120 <checkPamUser@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xca3(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1100 <list_count@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xc9d(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x0,%al
call   1040 <delete_all@plt>
mov    -0x8(%rbp),%rdi
lea    0xcaa(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xbbd(%rip),%rdi
call   1030 <remove@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_daemon>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1160 <fork@plt>
test   %rax,%rax
js     13a8 <make_daemon+0x168>
jne    13be <make_daemon+0x17e>
xor    %edi,%edi
call   10d0 <umask@plt>
xor    %eax,%eax
call   10b0 <setsid@plt>
test   %rax,%rax
js     1413 <make_daemon+0x1d3>
lea    0xdc1(%rip),%rdi
call   1080 <chdir@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10a0 <close@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <close@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10a0 <close@plt>
lea    0xd90(%rip),%rdi
lea    0xd5f(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   1150 <sleep@plt>
mov    $0xf,%ebp
lea    0xd7f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <getLoggedusers@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10c0 <fprintf@plt>
mov    %rbx,%rdi
call   10e0 <fflush@plt>
mov    $0x1,%edi
call   1150 <sleep@plt>
add    $0xffffffff,%ebp
jne    12e0 <make_daemon+0xa0>
mov    %rbx,%rdi
call   10e0 <fflush@plt>
xor    %edi,%edi
call   10f0 <getUserName@plt>
lea    0xd4e(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   10c0 <fprintf@plt>
xor    %edi,%edi
call   10f0 <getUserName@plt>
mov    %rax,%rdi
call   1140 <checkPamUser@plt>
lea    0xd4c(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   10c0 <fprintf@plt>
xor    %eax,%eax
call   1110 <list_count@plt>
lea    0xd4f(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   10c0 <fprintf@plt>
xor    %eax,%eax
call   1040 <delete_all@plt>
lea    0xd5b(%rip),%rdi
mov    $0x1a,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1130 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0xc64(%rip),%rdi
call   1030 <remove@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd42(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    %rax,%rbx
lea    0xc38(%rip),%rdi
lea    0xc45(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,%r14
lea    0xc90(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   10c0 <fprintf@plt>
mov    %r14,%rdi
call   10e0 <fflush@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
lea    0xc15(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
xor    %edi,%edi
call   1120 <exit@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fork failed!\0A\00"
@.str.1 = [20 x i8] c"/tmp/clepsydrad.pid\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [34 x i8] c"process_id of daemon process %d \0A\00"
@.str.5 = [5 x i8] c"/tmp\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.6 = [20 x i8] c"/tmp/clepsydrad.log\00"
@.str.7 = [32 x i8] c" pam cnt of logged users... %d\0A\00"
@.str.8 = [33 x i8] c" First user in list is .. [%s] \0A\00"
@.str.9 = [29 x i8] c" pam check status .. [%d] \0A\00"
@.str.10 = [38 x i8] c"list contains [%d] count of items...\0A\00"
@.str.11 = [27 x i8] c"Quit clepsydrad, bye !...\0A\00"
define i32 @make_daemon() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32* null, i32** %1
store i32* null, i32** %2
store i64 0, i64* %3
store i64 0, i64* %4
%8 = call i64 (...) @fork()
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i64, i64* %3
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %18, i32** %2
%19 = load i32*, i32** %2
%20 = load i64, i64* %3
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %20)
%22 = load i32*, i32** %2
%23 = call i32 @fflush(i32* %22)
%24 = load i32*, i32** %2
%25 = call i32 @fclose(i32* %24)
%26 = load i64, i64* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i64 %26)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %14
%30 = call i32 @umask(i32 0)
%31 = call i64 (...) @setsid()
store i64 %31, i64* %4
%32 = load i64, i64* %4
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* @STDIN_FILENO
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @STDERR_FILENO
%43 = call i32 @close(i32 %42)
%44 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %44, i32** %1
store i8* null, i8** %5
store i32 0, i32* %6
br label %45
45: ; preds = %36, %60
%46 = call i32 @sleep(i32 1)
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp sgt i32 %49, 15
br i1 %50, label %51, label %52
51: ; preds = %45
br label %61
52: ; preds = %45
%53 = load i32*, i32** %1
%54 = call i32 @getLoggedusers(i32* %53)
store i32 %54, i32* %7
%55 = load i32*, i32** %1
%56 = load i32, i32* %7
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %56)
%58 = load i32*, i32** %1
%59 = call i32 @fflush(i32* %58)
br label %60
60: ; preds = %52
br label %45
61: ; preds = %51
%62 = load i32*, i32** %1
%63 = call i32 @fflush(i32* %62)
%64 = load i32*, i32** %1
%65 = call i8* @getUserName(i32 0)
%66 = call i32 (i32*, i8*, ...) @fprintf(i32* %64, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %65)
%67 = load i32*, i32** %1
%68 = call i8* @getUserName(i32 0)
%69 = call i32 @checkPamUser(i8* %68)
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %67, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i32 %69)
%71 = load i32*, i32** %1
%72 = call i32 (...) @list_count()
%73 = call i32 (i32*, i8*, ...) @fprintf(i32* %71, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.10, i64 0, i64 0), i32 %72)
%74 = call i32 (...) @delete_all()
%75 = load i32*, i32** %1
%76 = call i32 (i32*, i8*, ...) @fprintf(i32* %75, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0))
%77 = load i32*, i32** %1
%78 = call i32 @fclose(i32* %77)
%79 = call i32 @remove(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"/tmp/clepsydrad.pid\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [34 x i8] c"process_id of daemon process %d \0A\00"
@.str.5 = [5 x i8] c"/tmp\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.6 = [20 x i8] c"/tmp/clepsydrad.log\00"
@.str.7 = [32 x i8] c" pam cnt of logged users... %d\0A\00"
@.str.8 = [33 x i8] c" First user in list is .. [%s] \0A\00"
@.str.9 = [29 x i8] c" pam check status .. [%d] \0A\00"
@.str.10 = [38 x i8] c"list contains [%d] count of items...\0A\00"
@.str.11 = [27 x i8] c"Quit clepsydrad, bye !...\0A\00"
@str = [13 x i8] c"fork failed!\00"
define i32 @make_daemon() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %15, label %8
8: ; preds = %6
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%11 = tail call i32 @fflush(i32* %9)
%12 = tail call i32 @fclose(i32* %9)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i64 %1)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %6
%16 = tail call i32 @umask(i32 0)
%17 = tail call i64 (...) @setsid()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%23 = load i32, i32* @STDIN_FILENO
%24 = tail call i32 @close(i32 %23)
%25 = load i32, i32* @STDOUT_FILENO
%26 = tail call i32 @close(i32 %25)
%27 = load i32, i32* @STDERR_FILENO
%28 = tail call i32 @close(i32 %27)
%29 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @sleep(i32 1)
br label %31
31: ; preds = %21, %31
%32 = phi i32 [ 0, %21 ], [ %33, %31 ]
%33 = add nuw nsw i32 %32, 1
%34 = tail call i32 @getLoggedusers(i32* %29)
%35 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %34)
%36 = tail call i32 @fflush(i32* %29)
%37 = tail call i32 @sleep(i32 1)
%38 = icmp eq i32 %33, 15
br i1 %38, label %39, label %31
39: ; preds = %31
%40 = tail call i32 @fflush(i32* %29)
%41 = tail call i8* @getUserName(i32 0)
%42 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i8* %41)
%43 = tail call i8* @getUserName(i32 0)
%44 = tail call i32 @checkPamUser(i8* %43)
%45 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0), i32 %44)
%46 = tail call i32 (...) @list_count()
%47 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.10, i64 0, i64 0), i32 %46)
%48 = tail call i32 (...) @delete_all()
%49 = tail call i64 @fwrite(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0), i64 26, i64 1, i32* %29)
%50 = tail call i32 @fclose(i32* %29)
%51 = tail call i32 @remove(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}