#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 /*<<< orphan*/ time_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int LOG_INFO;
int LOG_USER;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ syslog(int, char *, ...);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main() {
  pid_t pid;
  time_t now;
  int i;
  char *buf = "This is a example process\n";
  pid = fork();
  if (pid < 0) {
    printf("create child process fail\n");
    exit(1);
  }
  if (pid > 0) {
    exit(0);
  }
  setsid();
  for (i = 0; i < 1024; i++) {
    close(i);
  }
  chdir("/tmp");
  umask(0);
  signal(SIGCHLD, SIG_IGN);
  syslog(LOG_USER | LOG_INFO, "Daemon test!\n");
  while (1) {
    time(&now);
    syslog(LOG_USER | LOG_INFO, "now is : \t%d\t\t\n", ctime(&now));
    sleep(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe2a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1206 <main+0x46>
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    1218 <main+0x58>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x400,-0x18(%rbp)
jge    1249 <main+0x89>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1226 <main+0x66>
lea    0xde6(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    0x2d45(%rip),%rax
or     (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
lea    -0x14(%rbp),%rdi
call   10a0 <time@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    0x2d1c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <ctime@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
jmp    1293 <main+0xd3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     1291 <main+0xd1>
jne    12a7 <main+0xe7>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <setsid@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    $0x400,%ebx
jne    11f0 <main+0x30>
lea    0xdf7(%rip),%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    0x2db5(%rip),%r15
mov    0x2d8e(%rip),%r12
mov    (%r12),%edi
or     (%r15),%edi
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   10b0 <syslog@plt>
lea    0xc(%rsp),%rbx
lea    0xdb8(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <time@plt>
mov    (%r12),%ebp
or     (%r15),%ebp
mov    %rbx,%rdi
call   1040 <ctime@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <syslog@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
jmp    1260 <main+0xa0>
lea    0xd8b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"This is a example process\0A\00"
@.str.1 = [27 x i8] c"create child process fail\0A\00"
@.str.2 = [5 x i8] c"/tmp\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.3 = [14 x i8] c"Daemon test!\0A\00"
@.str.4 = [16 x i8] c"now is : \09%d\09\09\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i64, i64* %2
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %12
%18 = call i32 (...) @setsid()
store i32 0, i32* %4
br label %19
19: ; preds = %25, %17
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 1024
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %19
28: ; preds = %19
%29 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @umask(i32 0)
%31 = load i32, i32* @SIGCHLD
%32 = load i32, i32* @SIG_IGN
%33 = call i32 @signal(i32 %31, i32 %32)
%34 = load i32, i32* @LOG_USER
%35 = load i32, i32* @LOG_INFO
%36 = or i32 %34, %35
%37 = call i32 (i32, i8*, ...) @syslog(i32 %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %28, %38
%39 = call i32 @time(i32* %3)
%40 = load i32, i32* @LOG_USER
%41 = load i32, i32* @LOG_INFO
%42 = or i32 %40, %41
%43 = call i32 @ctime(i32* %3)
%44 = call i32 (i32, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = call i32 @sleep(i32 3)
br label %38
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c"/tmp\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.3 = [14 x i8] c"Daemon test!\0A\00"
@.str.4 = [16 x i8] c"now is : \09%d\09\09\0A\00"
@str = [26 x i8] c"create child process fail\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %8
%13 = tail call i32 (...) @setsid()
br label %14
14: ; preds = %12, %14
%15 = phi i32 [ 0, %12 ], [ %17, %14 ]
%16 = tail call i32 @close(i32 %15)
%17 = add nuw nsw i32 %15, 1
%18 = icmp eq i32 %17, 1024
br i1 %18, label %19, label %14
19: ; preds = %14
%20 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @umask(i32 0)
%22 = load i32, i32* @SIGCHLD
%23 = load i32, i32* @SIG_IGN
%24 = tail call i32 @signal(i32 %22, i32 %23)
%25 = load i32, i32* @LOG_USER
%26 = load i32, i32* @LOG_INFO
%27 = or i32 %26, %25
%28 = tail call i32 (i32, i8*, ...) @syslog(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %29, %19
%30 = call i32 @time(i32* %1)
%31 = load i32, i32* @LOG_USER
%32 = load i32, i32* @LOG_INFO
%33 = or i32 %32, %31
%34 = call i32 @ctime(i32* %1)
%35 = call i32 (i32, i8*, ...) @syslog(i32 %33, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 @sleep(i32 3)
br label %29
}