#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 pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ daemon(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  pid_t fres;
  FILE *fp;
  printf("execing %s \n", argv[1]);
  fres = fork();
  if (fres == -1) {
    perror("fork");
  } else if (fres != 0) {
    return 0;
  }
  setuid(geteuid());
  setsid();
  signal(SIGHUP, SIG_IGN);
  daemon(0, 0);
  while (1) {
    fp = fopen("/tmp/ap_reboot", "r");
    if (fp) {
      system(argv[1]);
      fclose(fp);
      unlink("/tmp/ap_reboot");
      sleep(60);
    } else {
      sleep(30);
    }
  }
}


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    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe0b(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    121f <main+0x4f>
lea    0xdf8(%rip),%rdi
call   10c0 <perror@plt>
jmp    1236 <main+0x66>
cmpl   $0x0,-0x14(%rbp)
je     1231 <main+0x61>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    1236 <main+0x66>
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,%edi
call   10d0 <setuid@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
xor    %esi,%esi
mov    %esi,%edi
call   10a0 <daemon@plt>
lea    0xda0(%rip),%rdi
lea    0xda8(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12be <main+0xee>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <system@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd68(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x3c,%edi
call   10e0 <sleep@plt>
jmp    12c8 <main+0xf8>
mov    $0x1e,%edi
call   10e0 <sleep@plt>
jmp    126b <main+0x9b>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x8(%rsi),%rsi
lea    0xe1a(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1215 <main+0x45>
cmp    $0xffffffff,%eax
je     1209 <main+0x39>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdfd(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%edi
call   10d0 <setuid@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
xor    %edi,%edi
xor    %esi,%esi
call   10a0 <daemon@plt>
lea    0xdc1(%rip),%r12
lea    0xdc9(%rip),%r15
jmp    126a <main+0x9a>
nopw   0x0(%rax,%rax,1)
mov    $0x1e,%edi
call   10e0 <sleep@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1260 <main+0x90>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
call   1050 <system@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
mov    $0x3c,%edi
call   10e0 <sleep@plt>
jmp    126a <main+0x9a>

LLVM IR (-O0):
@.str = [13 x i8] c"execing %s \0A\00"
@.str.1 = [5 x i8] c"fork\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [15 x i8] c"/tmp/ap_reboot\00"
@.str.3 = [2 x i8] c"r\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 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
17: ; preds = %2
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
ret i32 0
21: ; preds = %17
br label %22
22: ; preds = %21, %15
%23 = call i32 (...) @geteuid()
%24 = call i32 @setuid(i32 %23)
%25 = call i32 (...) @setsid()
%26 = load i32, i32* @SIGHUP
%27 = load i32, i32* @SIG_IGN
%28 = call i32 @signal(i32 %26, i32 %27)
%29 = call i32 @daemon(i32 0, i32 0)
br label %30
30: ; preds = %22, %45
%31 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %31, i32** %7
%32 = load i32*, i32** %7
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %43
34: ; preds = %30
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @system(i8* %37)
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
%41 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @sleep(i32 60)
br label %45
43: ; preds = %30
%44 = call i32 @sleep(i32 30)
br label %45
45: ; preds = %43, %34
br label %30
}

LLVM IR (-O2):
@.str = [13 x i8] c"execing %s \0A\00"
@.str.1 = [5 x i8] c"fork\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [15 x i8] c"/tmp/ap_reboot\00"
@.str.3 = [2 x i8] c"r\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %9 [
i32 -1, label %7
i32 0, label %10
]
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %10
9: ; preds = %2
ret i32 0
10: ; preds = %2, %7
%11 = tail call i32 (...) @geteuid()
%12 = tail call i32 @setuid(i32 %11)
%13 = tail call i32 (...) @setsid()
%14 = load i32, i32* @SIGHUP
%15 = load i32, i32* @SIG_IGN
%16 = tail call i32 @signal(i32 %14, i32 %15)
%17 = tail call i32 @daemon(i32 0, i32 0)
br label %18
18: ; preds = %26, %10
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = load i8*, i8** %3
%23 = tail call i32 @system(i8* %22)
%24 = tail call i32 @fclose(i32* %19)
%25 = tail call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %18, %21
%27 = phi i32 [ 60, %21 ], [ 30, %18 ]
%28 = tail call i32 @sleep(i32 %27)
br label %18
}