#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 */

/* Variables and functions */
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid = fork();
  if (pid < 0) {
    printf("%m\n");
    exit(-1);
  } else if (pid > 0) {
    exit(0);
  } else {
    setsid();
    umask(0);
    chdir("/");
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    syslog(LOG_INFO, "这是一条日志");
    while (1) {
      sleep(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d0 <main+0x40>
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jle    11e1 <main+0x51>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe0e(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    123a <main+0xaa>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     120c <main+0x7c>
jne    1222 <main+0x92>
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe4d(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1080 <syslog@plt>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1200 <main+0x70>
lea    0xded(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%m\0A\00"
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@LOG_INFO = global i32 0
@.str.2 = [19 x i8] c"\E8\BF\99\E6\98\AF\E4\B8\80\E6\9D\A1\E6\97\A5\E5\BF\97\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %13
%19 = call i32 (...) @setsid()
%20 = call i32 @umask(i32 0)
%21 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @STDERR_FILENO
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @LOG_INFO
%29 = call i32 @syslog(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %18, %30
%31 = call i32 @sleep(i32 1)
br label %30
}

LLVM IR (-O2):
@.str = [4 x i8] c"%m\0A\00"
@.str.1 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@LOG_INFO = global i32 0
@.str.2 = [19 x i8] c"\E8\BF\99\E6\98\AF\E4\B8\80\E6\9D\A1\E6\97\A5\E5\BF\97\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = icmp eq i32 %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()
%14 = tail call i32 @umask(i32 0)
%15 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @STDIN_FILENO
%17 = tail call i32 @close(i32 %16)
%18 = load i32, i32* @STDOUT_FILENO
%19 = tail call i32 @close(i32 %18)
%20 = load i32, i32* @STDERR_FILENO
%21 = tail call i32 @close(i32 %20)
%22 = load i32, i32* @LOG_INFO
%23 = tail call i32 @syslog(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %24, %12
%25 = tail call i32 @sleep(i32 1)
br label %24
}