#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*/ close(int);
int fork();
int getdtablesize();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stat(char *, struct stat *);
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 < getdtablesize(); j++)
    close(j);
  setsid();
  while (1) {
    sleep(120);
    stat("/sdcard/host_heartbeat", &ft);
    time(&ct);
    if (ct - ft.st_mtime > 120) {
      system("su -c stop adbd");
      system("sleep 2");
      system("su -c start adbd");
    }
  }
  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   10a0 <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    1281 <main+0xf1>
cmpl   $0x0,-0x14(%rbp)
jle    11e8 <main+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1281 <main+0xf1>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   10b0 <getdtablesize@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    121f <main+0x8f>
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   1090 <sleep@plt>
lea    0xdd4(%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    127c <main+0xec>
lea    0xdc3(%rip),%rdi
call   1030 <system@plt>
lea    0xdc7(%rip),%rdi
call   1030 <system@plt>
lea    0xdc3(%rip),%rdi
call   1030 <system@plt>
jmp    1226 <main+0x96>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     1243 <main+0xb3>
jne    1254 <main+0xc4>
xor    %eax,%eax
call   10b0 <getdtablesize@plt>
test   %eax,%eax
jle    11d5 <main+0x45>
xor    %ebx,%ebx
nop
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   10b0 <getdtablesize@plt>
cmp    %eax,%ebx
jl     11c0 <main+0x30>
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe28(%rip),%r14
mov    %rsp,%r15
lea    0x4(%rsp),%rbx
lea    0xe30(%rip),%r12
lea    0xe39(%rip),%r13
lea    0xe3a(%rip),%rbp
mov    $0x78,%edi
call   1090 <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     1200 <main+0x70>
mov    %r12,%rdi
call   1030 <system@plt>
mov    %r13,%rdi
call   1030 <system@plt>
mov    %rbp,%rdi
call   1030 <system@plt>
jmp    1200 <main+0x70>
lea    0xdb6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"fork error\00"
@.str.1 = [23 x i8] c"/sdcard/host_heartbeat\00"
@.str.2 = [16 x i8] c"su -c stop adbd\00"
@.str.3 = [8 x i8] c"sleep 2\00"
@.str.4 = [17 x i8] c"su -c start adbd\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 = tail call i32 (...) @getdtablesize()
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13, %16
%17 = phi i32 [ %19, %16 ], [ 0, %13 ]
%18 = tail call i32 @close(i32 %17)
%19 = add nuw nsw i32 %17, 1
%20 = tail call i32 (...) @getdtablesize()
%21 = icmp slt i32 %19, %20
br i1 %21, label %16, label %22
22: ; preds = %16, %13
%23 = tail call i32 (...) @setsid()
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %25
25: ; preds = %37, %22
%26 = call i32 @sleep(i32 120)
%27 = call i32 @stat(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%28 = call i32 @time(i32* %4)
%29 = load i32, i32* %4
%30 = load i32, i32* %24
%31 = sub nsw i32 %29, %30
%32 = icmp sgt i32 %31, 120
br i1 %32, label %33, label %37
33: ; preds = %25
%34 = call i32 @system(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %33, %25
br label %25
38: ; preds = %11, %9
%39 = phi i32 [ 1, %9 ], [ 0, %11 ]
ret i32 %39
}