#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char);
int fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main() {
  pid_t pid;
  if ((pid = fork()) < -1) {
    return -1;
  } else if (pid != 0) {
    return 0;
  }
  setsid();
  chdir("/");
  umask(0);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  FILE *fp;
  int fd;
  fp = fopen("/Users/czz/mydaemon.log", 'a');
  while (1) {
    fputs("hei i'am chenzongzhi\n", stderr);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jge    11ae <main+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <main+0xc6>
cmpl   $0x0,-0x8(%rbp)
je     11c4 <main+0x44>
movl   $0x0,-0x4(%rbp)
jmp    1246 <main+0xc6>
jmp    11c9 <main+0x49>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xe29(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    0xdee(%rip),%rdi
mov    $0x61,%esi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1222 <main+0xa2>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
jl     11a0 <main+0x20>
mov    $0x0,%eax
test   %ecx,%ecx
je     11a8 <main+0x28>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe4a(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    0xe0f(%rip),%rdi
mov    $0x61,%esi
call   1080 <fopen@plt>
mov    0x2dd4(%rip),%r14
lea    0xe0f(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1210 <main+0x90>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [24 x i8] c"/Users/czz/mydaemon.log\00"
@.str.2 = [22 x i8] c"hei i'am chenzongzhi\0A\00"
@stderr = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = icmp slt i32 %5, -1
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %28
8: ; preds = %0
%9 = load i32, i32* %2
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %8
store i32 0, i32* %1
br label %28
12: ; preds = %8
br label %13
13: ; preds = %12
%14 = call i32 (...) @setsid()
%15 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @umask(i32 0)
%17 = load i32, i32* @STDIN_FILENO
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* @STDERR_FILENO
%22 = call i32 @close(i32 %21)
%23 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8 signext 97)
store i32* %23, i32** %3
br label %24
24: ; preds = %13, %24
%25 = load i32, i32* @stderr
%26 = call i32 @fputs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @sleep(i32 1)
br label %24
28: ; preds = %11, %7
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [24 x i8] c"/Users/czz/mydaemon.log\00"
@.str.2 = [22 x i8] c"hei i'am chenzongzhi\0A\00"
@stderr = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp slt i32 %1, -1
br i1 %2, label %20, label %3
3: ; preds = %0
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %20
5: ; preds = %3
%6 = tail call i32 (...) @setsid()
%7 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @umask(i32 0)
%9 = load i32, i32* @STDIN_FILENO
%10 = tail call i32 @close(i32 %9)
%11 = load i32, i32* @STDOUT_FILENO
%12 = tail call i32 @close(i32 %11)
%13 = load i32, i32* @STDERR_FILENO
%14 = tail call i32 @close(i32 %13)
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8 signext 97)
br label %16
16: ; preds = %16, %5
%17 = load i32, i32* @stderr
%18 = tail call i32 @fputs(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = tail call i32 @sleep(i32 1)
br label %16
20: ; preds = %3, %0
%21 = phi i32 [ -1, %0 ], [ 0, %3 ]
ret i32 %21
}