#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int WorkProc();
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsid();
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int status;
  int pid;
  pid = fork();
  if (pid == -1) {
    printf("Start Daemon Error: %s\n", strerror(errno));
    return -1;
  } else if (!pid) {
    umask(0);
    printf("Demon Thread Run \n");
    setsid();
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    status = WorkProc();
    return status;
  } else {
    printf("Demon Run \n");
    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   1090 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c5 <main+0x55>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <main+0xcf>
cmpl   $0x0,-0x18(%rbp)
jne    122a <main+0xba>
xor    %edi,%edi
call   1060 <umask@plt>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1070 <WorkProc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123f <main+0xcf>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11b7 <main+0x37>
cmp    $0xffffffff,%eax
jne    1203 <main+0x83>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe5a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
xor    %edi,%edi
call   1070 <umask@plt>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rcx
jmp    1080 <WorkProc@plt>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Start Daemon Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"Demon Thread Run \0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [12 x i8] c"Demon Run \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %7
%9 = load i32, i32* %7
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %3
br label %32
15: ; preds = %2
%16 = load i32, i32* %7
%17 = icmp ne i32 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %15
%19 = call i32 @umask(i32 0)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 (...) @setsid()
%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 = call i32 (...) @WorkProc()
store i32 %28, i32* %6
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %32
30: ; preds = %15
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %32
32: ; preds = %30, %18, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@.str = [24 x i8] c"Start Daemon Error: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@str = [18 x i8] c"Demon Thread Run \00"
@str.3 = [11 x i8] c"Demon Run \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %19 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %2
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %21
8: ; preds = %2
%9 = tail call i32 @umask(i32 0)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 (...) @setsid()
%12 = load i32, i32* @STDIN_FILENO
%13 = tail call i32 @close(i32 %12)
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @close(i32 %14)
%16 = load i32, i32* @STDERR_FILENO
%17 = tail call i32 @close(i32 %16)
%18 = tail call i32 (...) @WorkProc()
br label %21
19: ; preds = %2
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %8, %4
%22 = phi i32 [ -1, %4 ], [ 0, %19 ], [ %18, %8 ]
ret i32 %22
}