#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 */
struct conf {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int begin_watch(struct conf *);
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ config_free(struct conf *);
struct conf *config_load();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ init_name_map();
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsid();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    printf("FAILED to fork()");
    exit(EXIT_FAILURE);
  }
  if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  umask(0);
  openlog("watch_it", 0, 0);
  syslog(LOG_DEBUG, "\nStarting watch_it\n");
  if ((sid = setsid()) < 0) {
    syslog(LOG_ERR, "setsid() FAILED:\n\tcause:%s", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((chdir("/")) < 0) {
    syslog(LOG_ERR, "chdir(\"/\") FAILED:\n\tcause:%s", strerror(errno));
    exit(EXIT_FAILURE);
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  struct conf *config = config_load();
  if (config == NULL) {
    syslog(LOG_ERR, "failed to load config file");
    return -1;
  }
  init_name_map();
  int res = begin_watch(config);
  config_free(config);
  syslog(LOG_WARNING, "exit");
  closelog();
  exit(res ? EXIT_FAILURE : EXIT_SUCCESS);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1110 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1236 <main+0x46>
lea    0xddd(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    124f <main+0x5f>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
xor    %edi,%edi
call   1090 <umask@plt>
lea    0xdb4(%rip),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10d0 <openlog@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    12cd <main+0xdd>
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xd75(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    131e <main+0x12e>
mov    0x2cae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%edx
lea    0xd42(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1040 <config_load@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1380 <main+0x190>
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
lea    0xcfa(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    $0xffffffff,%eax
add    $0x40,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   10e0 <init_name_map@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <begin_watch@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <config_free@plt>
mov    0x2c3d(%rip),%rax
mov    (%rax),%edi
lea    0xcd7(%rip),%rsi
mov    $0x0,%al
call   10c0 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
cmpl   $0x0,-0x2c(%rbp)
je     13d5 <main+0x1e5>
mov    0x2bf5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    13e1 <main+0x1f1>
mov    0x2bdc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
js     12b1 <main+0xc1>
jne    134b <main+0x15b>
xor    %edi,%edi
call   1090 <umask@plt>
lea    0xdfa(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <openlog@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
xor    %eax,%eax
call   10c0 <syslog@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
test   %rax,%rax
js     12c2 <main+0xd2>
lea    0xdff(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
js     12e2 <main+0xf2>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1040 <config_load@plt>
test   %rax,%rax
jne    130d <main+0x11d>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
xor    %eax,%eax
call   10c0 <syslog@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd48(%rip),%rdi
call   1060 <printf@plt>
jmp    1359 <main+0x169>
mov    0x2ccf(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd4e(%rip),%rsi
jmp    1300 <main+0x110>
mov    0x2caf(%rip),%rax
mov    (%rax),%ebx
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd4b(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   10c0 <syslog@plt>
jmp    1359 <main+0x169>
mov    %rax,%rbx
xor    %eax,%eax
call   10e0 <init_name_map@plt>
mov    %rbx,%rdi
call   10a0 <begin_watch@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10b0 <config_free@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
lea    0xd4a(%rip),%rsi
xor    %eax,%eax
call   10c0 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
test   %ebp,%ebp
jne    1359 <main+0x169>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.conf = type { i32 }
@.str = [17 x i8] c"FAILED to fork()\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [9 x i8] c"watch_it\00"
@LOG_DEBUG = global i32 0
@.str.2 = [20 x i8] c"\0AStarting watch_it\0A\00"
@LOG_ERR = global i32 0
@.str.3 = [27 x i8] c"setsid() FAILED:\0A\09cause:%s\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [29 x i8] c"chdir(\22/\22) FAILED:\0A\09cause:%s\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.6 = [27 x i8] c"failed to load config file\00"
@LOG_WARNING = global i32 0
@.str.7 = [5 x i8] c"exit\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.conf*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 (...) @fork()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i64, i64* %6
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @EXIT_SUCCESS
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %17
%24 = call i32 @umask(i32 0)
%25 = call i32 @openlog(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 0)
%26 = load i32, i32* @LOG_DEBUG
%27 = call i32 (i32, i8*, ...) @syslog(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%28 = call i64 (...) @setsid()
store i64 %28, i64* %7
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %23
%31 = load i32, i32* @LOG_ERR
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 (i32, i8*, ...) @syslog(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %23
%38 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i32, i32* @LOG_ERR
%42 = load i32, i32* @errno
%43 = call i32 @strerror(i32 %42)
%44 = call i32 (i32, i8*, ...) @syslog(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %37
%48 = load i32, i32* @STDIN_FILENO
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @STDOUT_FILENO
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @STDERR_FILENO
%53 = call i32 @close(i32 %52)
%54 = call %struct.conf* (...) @config_load()
store %struct.conf* %54, %struct.conf** %8
%55 = load %struct.conf*, %struct.conf** %8
%56 = icmp eq %struct.conf* %55, null
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = load i32, i32* @LOG_ERR
%59 = call i32 (i32, i8*, ...) @syslog(i32 %58, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
ret i32 -1
60: ; preds = %47
%61 = call i32 (...) @init_name_map()
%62 = load %struct.conf*, %struct.conf** %8
%63 = call i32 @begin_watch(%struct.conf* %62)
store i32 %63, i32* %9
%64 = load %struct.conf*, %struct.conf** %8
%65 = call i32 @config_free(%struct.conf* %64)
%66 = load i32, i32* @LOG_WARNING
%67 = call i32 (i32, i8*, ...) @syslog(i32 %66, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%68 = call i32 (...) @closelog()
%69 = load i32, i32* %9
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %60
%72 = load i32, i32* @EXIT_FAILURE
br label %75
73: ; preds = %60
%74 = load i32, i32* @EXIT_SUCCESS
br label %75
75: ; preds = %73, %71
%76 = phi i32 [ %72, %71 ], [ %74, %73 ]
%77 = call i32 @exit(i32 %76)
unreachable
}

LLVM IR (-O2):
%struct.conf = type { i32 }
@.str = [17 x i8] c"FAILED to fork()\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [9 x i8] c"watch_it\00"
@LOG_DEBUG = global i32 0
@.str.2 = [20 x i8] c"\0AStarting watch_it\0A\00"
@LOG_ERR = global i32 0
@.str.3 = [27 x i8] c"setsid() FAILED:\0A\09cause:%s\00"
@errno = global i32 0
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [29 x i8] c"chdir(\22/\22) FAILED:\0A\09cause:%s\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.6 = [27 x i8] c"failed to load config file\00"
@LOG_WARNING = global i32 0
@.str.7 = [5 x i8] c"exit\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = icmp eq i64 %3, 0
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = load i32, i32* @EXIT_SUCCESS
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %9
%15 = tail call i32 @umask(i32 0)
%16 = tail call i32 @openlog(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 0)
%17 = load i32, i32* @LOG_DEBUG
%18 = tail call i32 (i32, i8*, ...) @syslog(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %14
%22 = load i32, i32* @LOG_ERR
%23 = load i32, i32* @errno
%24 = tail call i32 @strerror(i32 %23)
%25 = tail call i32 (i32, i8*, ...) @syslog(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %14
%29 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load i32, i32* @LOG_ERR
%33 = load i32, i32* @errno
%34 = tail call i32 @strerror(i32 %33)
%35 = tail call i32 (i32, i8*, ...) @syslog(i32 %32, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %28
%39 = load i32, i32* @STDIN_FILENO
%40 = tail call i32 @close(i32 %39)
%41 = load i32, i32* @STDOUT_FILENO
%42 = tail call i32 @close(i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = tail call i32 @close(i32 %43)
%45 = tail call %struct.conf* (...) @config_load()
%46 = icmp eq %struct.conf* %45, null
br i1 %46, label %47, label %50
47: ; preds = %38
%48 = load i32, i32* @LOG_ERR
%49 = tail call i32 (i32, i8*, ...) @syslog(i32 %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
ret i32 -1
50: ; preds = %38
%51 = tail call i32 (...) @init_name_map()
%52 = tail call i32 @begin_watch(%struct.conf* %45)
%53 = tail call i32 @config_free(%struct.conf* %45)
%54 = load i32, i32* @LOG_WARNING
%55 = tail call i32 (i32, i8*, ...) @syslog(i32 %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%56 = tail call i32 (...) @closelog()
%57 = icmp eq i32 %52, 0
%58 = load i32, i32* @EXIT_FAILURE
%59 = load i32, i32* @EXIT_SUCCESS
%60 = select i1 %57, i32 %59, i32 %58
%61 = tail call i32 @exit(i32 %60)
unreachable
}