#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 LoadConfig(char *);
int MonitorProc();
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
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;
  if (argc != 2) {
    printf("Usage: ./my_daemon filename.cfg\n");
    return -1;
  }
  status = LoadConfig(argv[1]);
  if (!status) {
    printf("Error: Load config failed\n");
    return -1;
  }
  pid = fork();
  if (pid == -1) {
    printf("Start Daemon Error: %s\n", strerror(errno));
    return -1;
  } else if (!pid) {
    umask(0);
    setsid();
    chdir("/");
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    status = MonitorProc();
    return status;
  } else {
    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)
cmpl   $0x2,-0x8(%rbp)
je     11ca <main+0x3a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <main+0x117>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <LoadConfig@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11fe <main+0x6e>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <main+0x117>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    123d <main+0xad>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <main+0x117>
cmpl   $0x0,-0x18(%rbp)
jne    12a0 <main+0x110>
xor    %edi,%edi
call   1080 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xdf8(%rip),%rdi
call   1030 <chdir@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1070 <MonitorProc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a7 <main+0x117>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    11e6 <main+0x46>
mov    0x8(%rsi),%rdi
call   10a0 <LoadConfig@plt>
test   %eax,%eax
je     11ef <main+0x4f>
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1204 <main+0x64>
cmp    $0xffffffff,%eax
jne    1200 <main+0x60>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe26(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11fb <main+0x5b>
lea    0xe47(%rip),%rdi
jmp    11f6 <main+0x56>
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xdff(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
jmp    1080 <MonitorProc@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Usage: ./my_daemon filename.cfg\0A\00"
@.str.1 = [27 x i8] c"Error: Load config failed\0A\00"
@.str.2 = [24 x i8] c"Start Daemon Error: %s\0A\00"
@errno = global i32 0
@.str.3 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
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 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @LoadConfig(i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
21: ; preds = %12
%22 = call i32 (...) @fork()
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %45
29: ; preds = %21
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %29
%33 = call i32 @umask(i32 0)
%34 = call i32 (...) @setsid()
%35 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @STDERR_FILENO
%41 = call i32 @close(i32 %40)
%42 = call i32 (...) @MonitorProc()
store i32 %42, i32* %6
%43 = load i32, i32* %6
store i32 %43, i32* %3
br label %45
44: ; preds = %29
store i32 0, i32* %3
br label %45
45: ; preds = %44, %32, %25, %19, %10
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str.2 = [24 x i8] c"Start Daemon Error: %s\0A\00"
@errno = global i32 0
@.str.3 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@str = [26 x i8] c"Error: Load config failed\00"
@str.4 = [32 x i8] c"Usage: ./my_daemon filename.cfg\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.4, i64 0, i64 0))
br label %30
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @LoadConfig(i8* %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %30
13: ; preds = %6
%14 = tail call i32 (...) @fork()
switch i32 %14, label %30 [
i32 -1, label %15
i32 0, label %19
]
15: ; preds = %13
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %30
19: ; preds = %13
%20 = tail call i32 @umask(i32 0)
%21 = tail call i32 (...) @setsid()
%22 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, 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 (...) @MonitorProc()
br label %30
30: ; preds = %13, %19, %15, %11, %4
%31 = phi i32 [ -1, %4 ], [ -1, %15 ], [ %29, %19 ], [ -1, %11 ], [ 0, %13 ]
ret i32 %31
}