#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 /*<<< orphan*/ DIR;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execle(char *, char *, int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ make_invincible();
int /*<<< orphan*/ make_vulnerable();
int open(char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char *env[2];
  int status;
  DIR *d;
  int pid;
  int lfd;
  int foreground = 0;
  if ((argc > 1) && (strncmp(argv[1], "-f", 3) == 0)) {
    foreground = 1;
  }
  if (!foreground) {
    pid = fork();
    if (pid != 0) {
      exit(0);
    }
    close(0);
    close(1);
    close(2);
  }
  make_invincible();
  env[0] = "PYTHONPATH="
           "/usr/local/tashi/"
           "/src/";
  env[1] = NULL;
  while (1) {
    pid = fork();
    if (pid == 0) {
      make_vulnerable();
      if (!foreground) {
        lfd = open("/var/log/nodemanager.log", O_WRONLY | O_APPEND | O_CREAT);
        if (lfd < 0) {
          lfd = open("/dev/null", O_WRONLY);
        }
        dup2(lfd, 2);
        dup2(lfd, 1);
        close(0);
      }
      chdir("/usr/local/tashi/");
      execle("./bin/nodemanager.py", "./bin/nodemanager.py", NULL, env);
      exit(-1);
    }
    sleep(10);
    waitpid(pid, &status, 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)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1211 <main+0x51>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0a(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1211 <main+0x51>
movl   $0x1,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1251 <main+0x91>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1236 <main+0x76>
xor    %edi,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1070 <make_invincible@plt>
lea    0xda4(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    132a <main+0x16a>
mov    $0x0,%al
call   1080 <make_vulnerable@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    12fd <main+0x13d>
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
mov    0x2d28(%rip),%rax
or     (%rax),%esi
mov    0x2d4f(%rip),%rax
or     (%rax),%esi
lea    0xd73(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12dc <main+0x11c>
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    0xd6a(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    -0x38(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
call   1060 <close@plt>
lea    0xd44(%rip),%rdi
call   1040 <chdir@plt>
lea    -0x20(%rbp),%rcx
lea    0xd46(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10c0 <execle@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    -0x34(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
jmp    126b <main+0xab>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jl     11ec <main+0x2c>
mov    0x8(%rsi),%rdi
lea    0xe28(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11ec <main+0x2c>
xor    %r14d,%r14d
jmp    1219 <main+0x59>
xor    %eax,%eax
call   10e0 <fork@plt>
xor    %edi,%edi
test   %eax,%eax
jne    1304 <main+0x144>
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x1,%r14b
xor    %eax,%eax
call   1070 <make_invincible@plt>
lea    0xddc(%rip),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1273 <main+0xb3>
mov    %eax,%ebp
lea    0xc(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1250 <main+0x90>
xor    %eax,%eax
call   1080 <make_vulnerable@plt>
test   %r14b,%r14b
je     12dd <main+0x11d>
mov    0x2d4a(%rip),%rbp
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
mov    0x2d5f(%rip),%rax
or     (%rax),%esi
lea    0xd83(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12be <main+0xfe>
mov    0x0(%rbp),%esi
lea    0xd87(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
xor    %edi,%edi
call   1060 <close@plt>
lea    0xd64(%rip),%rdi
call   1040 <chdir@plt>
lea    0xd6a(%rip),%rdi
lea    0x10(%rsp),%rcx
mov    %rdi,%rsi
xor    %edx,%edx
call   10c0 <execle@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"-f\00"
@.str.1 = [34 x i8] c"PYTHONPATH=/usr/local/tashi//src/\00"
@.str.2 = [25 x i8] c"/var/log/nodemanager.log\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@.str.3 = [10 x i8] c"/dev/null\00"
@.str.4 = [18 x i8] c"/usr/local/tashi/\00"
@.str.5 = [21 x i8] c"./bin/nodemanager.py\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i8*]
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 1, i32* %11
br label %21
21: ; preds = %20, %14, %2
%22 = load i32, i32* %11
%23 = icmp ne i32 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %21
%25 = call i32 (...) @fork()
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %24
%31 = call i32 @close(i32 0)
%32 = call i32 @close(i32 1)
%33 = call i32 @close(i32 2)
br label %34
34: ; preds = %30, %21
%35 = call i32 (...) @make_invincible()
%36 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8** %36
%37 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 1
store i8* null, i8** %37
br label %38
38: ; preds = %34, %69
%39 = call i32 (...) @fork()
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %69
42: ; preds = %38
%43 = call i32 (...) @make_vulnerable()
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 0
br i1 %45, label %64, label %46
46: ; preds = %42
%47 = load i32, i32* @O_WRONLY
%48 = load i32, i32* @O_APPEND
%49 = or i32 %47, %48
%50 = load i32, i32* @O_CREAT
%51 = or i32 %49, %50
%52 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %51)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %46
%56 = load i32, i32* @O_WRONLY
%57 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %56)
store i32 %57, i32* %10
br label %58
58: ; preds = %55, %46
%59 = load i32, i32* %10
%60 = call i32 @dup2(i32 %59, i32 2)
%61 = load i32, i32* %10
%62 = call i32 @dup2(i32 %61, i32 1)
%63 = call i32 @close(i32 0)
br label %64
64: ; preds = %58, %42
%65 = call i32 @chdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%66 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%67 = call i32 @execle(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32* null, i8** %66)
%68 = call i32 @exit(i32 -1)
unreachable
69: ; preds = %38
%70 = call i32 @sleep(i32 10)
%71 = load i32, i32* %9
%72 = call i32 @waitpid(i32 %71, i32* %7, i32 0)
br label %38
}

LLVM IR (-O2):
@.str = [3 x i8] c"-f\00"
@.str.1 = [34 x i8] c"PYTHONPATH=/usr/local/tashi//src/\00"
@.str.2 = [25 x i8] c"/var/log/nodemanager.log\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@.str.3 = [10 x i8] c"/dev/null\00"
@.str.4 = [18 x i8] c"/usr/local/tashi/\00"
@.str.5 = [21 x i8] c"./bin/nodemanager.py\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i8*]
%4 = alloca i32
%5 = bitcast [2 x i8*]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strncmp(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %2, %8
%14 = tail call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %13
%19 = tail call i32 @close(i32 0)
%20 = tail call i32 @close(i32 1)
%21 = tail call i32 @close(i32 2)
br label %22
22: ; preds = %18, %8
%23 = phi i1 [ true, %18 ], [ false, %8 ]
%24 = tail call i32 (...) @make_invincible()
%25 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8** %25
%26 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 1
store i8* null, i8** %26
%27 = call i32 (...) @fork()
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %51
29: ; preds = %51, %22
%30 = call i32 (...) @make_vulnerable()
br i1 %23, label %31, label %47
31: ; preds = %29
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_APPEND
%34 = or i32 %33, %32
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = load i32, i32* @O_WRONLY
%41 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %39, %31
%43 = phi i32 [ %41, %39 ], [ %37, %31 ]
%44 = call i32 @dup2(i32 %43, i32 2)
%45 = call i32 @dup2(i32 %43, i32 1)
%46 = call i32 @close(i32 0)
br label %47
47: ; preds = %42, %29
%48 = call i32 @chdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @execle(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32* null, i8** %25)
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %22, %51
%52 = phi i32 [ %55, %51 ], [ %27, %22 ]
%53 = call i32 @sleep(i32 10)
%54 = call i32 @waitpid(i32 %52, i32* %4, i32 0)
%55 = call i32 (...) @fork()
%56 = icmp eq i32 %55, 0
br i1 %56, label %29, label %51
}