#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 time_t;
struct stat {
  int st_mtime;
};

/* Variables and functions */
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stat(char *, struct stat *);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ time(int *);
int main(int argc, char **argv) {
  int i = fork();
  struct stat ft;
  time_t ct;
  if (i < 0) {
    printf("fork error");
    return 1;
  }
  if (i > 0)
    return 0;
  int j;
  for (j = 0; j < sysconf(_SC_OPEN_MAX); j++)
    close(j);
  setsid();
  while (1) {
    sleep(120);
    stat("/sdcard/host_heartbeat", &ft);
    time(&ct);
    if (ct - ft.st_mtime > 120) {
      system("su -c reboot");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d2 <main+0x42>
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1270 <main+0xe0>
cmpl   $0x0,-0x14(%rbp)
jle    11e8 <main+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1270 <main+0xe0>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    1226 <main+0x96>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ef <main+0x5f>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    $0x78,%edi
call   10a0 <sleep@plt>
lea    0xdcd(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <time@plt>
mov    -0x1c(%rbp),%eax
sub    -0x18(%rbp),%eax
cmp    $0x78,%eax
jle    126b <main+0xdb>
lea    0xdbc(%rip),%rdi
call   1030 <system@plt>
jmp    122d <main+0x9d>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1243 <main+0xb3>
jne    1254 <main+0xc4>
mov    0x2e2a(%rip),%r14
mov    (%r14),%edi
call   1090 <sysconf@plt>
test   %eax,%eax
jle    11e6 <main+0x56>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
mov    (%r14),%edi
call   1090 <sysconf@plt>
cmp    %eax,%ebx
jl     11d0 <main+0x40>
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe17(%rip),%r14
mov    %rsp,%r15
lea    0x4(%rsp),%rbx
lea    0xe1f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x78,%edi
call   10a0 <sleep@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <stat@plt>
mov    %rbx,%rdi
call   1080 <time@plt>
mov    0x4(%rsp),%eax
sub    (%rsp),%eax
cmp    $0x79,%eax
jl     1210 <main+0x80>
mov    %r12,%rdi
call   1030 <system@plt>
jmp    1210 <main+0x80>
lea    0xdb6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"fork error\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [23 x i8] c"/sdcard/host_heartbeat\00"
@.str.2 = [13 x i8] c"su -c reboot\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @fork()
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %45
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 0, i32* %3
br label %45
19: ; preds = %15
store i32 0, i32* %9
br label %20
20: ; preds = %28, %19
%21 = load i32, i32* %9
%22 = load i32, i32* @_SC_OPEN_MAX
%23 = call i32 @sysconf(i32 %22)
%24 = icmp slt i32 %21, %23
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* %9
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25
%29 = load i32, i32* %9
%30 = add nsw i32 %29, 1
store i32 %30, i32* %9
br label %20
31: ; preds = %20
%32 = call i32 (...) @setsid()
br label %33
33: ; preds = %31, %44
%34 = call i32 @sleep(i32 120)
%35 = call i32 @stat(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %7)
%36 = call i32 @time(i32* %8)
%37 = load i32, i32* %8
%38 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%39 = load i32, i32* %38
%40 = sub nsw i32 %37, %39
%41 = icmp sgt i32 %40, 120
br i1 %41, label %42, label %44
42: ; preds = %33
%43 = call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %33
br label %33
45: ; preds = %18, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"fork error\00"
@_SC_OPEN_MAX = global i32 0
@.str.1 = [23 x i8] c"/sdcard/host_heartbeat\00"
@.str.2 = [13 x i8] c"su -c reboot\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = tail call i32 (...) @fork()
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = icmp slt i32 %5, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %38
11: ; preds = %2
%12 = icmp eq i32 %5, 0
br i1 %12, label %13, label %38
13: ; preds = %11
%14 = load i32, i32* @_SC_OPEN_MAX
%15 = tail call i32 @sysconf(i32 %14)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %13, %17
%18 = phi i32 [ %20, %17 ], [ 0, %13 ]
%19 = tail call i32 @close(i32 %18)
%20 = add nuw nsw i32 %18, 1
%21 = load i32, i32* @_SC_OPEN_MAX
%22 = tail call i32 @sysconf(i32 %21)
%23 = icmp slt i32 %20, %22
br i1 %23, label %17, label %24
24: ; preds = %17, %13
%25 = tail call i32 (...) @setsid()
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %27
27: ; preds = %37, %24
%28 = call i32 @sleep(i32 120)
%29 = call i32 @stat(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%30 = call i32 @time(i32* %4)
%31 = load i32, i32* %4
%32 = load i32, i32* %26
%33 = sub nsw i32 %31, %32
%34 = icmp sgt i32 %33, 120
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %27
br label %27
38: ; preds = %11, %9
%39 = phi i32 [ 1, %9 ], [ 0, %11 ]
ret i32 %39
}