#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int getdtablesize();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int main(int argc, char **argv) {
  int i = fork();
  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) {
    system("touch /sdcard/host_heartbeat");
    sleep(120);
    if (fopen("/sdcard/host_heartbeat", "r") != NULL) {
      system("stop adbd");
      system("sleep 2");
      system("start adbd");
    }
  }
  return 0;
}


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    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c2 <main+0x42>
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1272 <main+0xf2>
cmpl   $0x0,-0x14(%rbp)
jle    11d8 <main+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1272 <main+0xf2>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10a0 <getdtablesize@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    120f <main+0x8f>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11df <main+0x5f>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xdee(%rip),%rdi
call   1030 <system@plt>
mov    $0x78,%edi
call   1080 <sleep@plt>
lea    0xdde(%rip),%rdi
lea    0xdcf(%rip),%rsi
call   1070 <fopen@plt>
cmp    $0x0,%rax
je     126d <main+0xed>
lea    0xdd8(%rip),%rdi
call   1030 <system@plt>
lea    0xdd6(%rip),%rdi
call   1030 <system@plt>
lea    0xdd2(%rip),%rdi
call   1030 <system@plt>
jmp    1216 <main+0x96>
mov    -0x4(%rbp),%eax
add    $0x20,%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   1090 <fork@plt>
test   %eax,%eax
js     123c <main+0xbc>
jne    124d <main+0xcd>
xor    %eax,%eax
call   10a0 <getdtablesize@plt>
test   %eax,%eax
jle    11c5 <main+0x45>
xor    %ebx,%ebx
nop
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   10a0 <getdtablesize@plt>
cmp    %eax,%ebx
jl     11b0 <main+0x30>
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe38(%rip),%r14
lea    0xe37(%rip),%r15
lea    0xe28(%rip),%rbx
lea    0xe40(%rip),%r12
lea    0xe43(%rip),%r13
lea    0xe44(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <system@plt>
mov    $0x78,%edi
call   1080 <sleep@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1200 <main+0x80>
mov    %r12,%rdi
call   1030 <system@plt>
mov    %r13,%rdi
call   1030 <system@plt>
mov    %rbp,%rdi
call   1030 <system@plt>
jmp    1200 <main+0x80>
lea    0xdbd(%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):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [29 x i8] c"touch /sdcard/host_heartbeat\00"
@.str.2 = [23 x i8] c"/sdcard/host_heartbeat\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [10 x i8] c"stop adbd\00"
@.str.5 = [8 x i8] c"sleep 2\00"
@.str.6 = [11 x i8] 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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @fork()
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %40
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 0, i32* %3
br label %40
17: ; preds = %13
store i32 0, i32* %7
br label %18
18: ; preds = %25, %17
%19 = load i32, i32* %7
%20 = call i32 (...) @getdtablesize()
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %18
28: ; preds = %18
%29 = call i32 (...) @setsid()
br label %30
30: ; preds = %28, %39
%31 = call i32 @system(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @sleep(i32 120)
%33 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = call i32 @system(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @system(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %35, %30
br label %30
40: ; preds = %16, %11
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [29 x i8] c"touch /sdcard/host_heartbeat\00"
@.str.2 = [23 x i8] c"/sdcard/host_heartbeat\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [10 x i8] c"stop adbd\00"
@.str.5 = [8 x i8] c"sleep 2\00"
@.str.6 = [11 x i8] c"start adbd\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %30
7: ; preds = %2
%8 = icmp eq i32 %3, 0
br i1 %8, label %9, label %30
9: ; preds = %7
%10 = tail call i32 (...) @getdtablesize()
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9, %12
%13 = phi i32 [ %15, %12 ], [ 0, %9 ]
%14 = tail call i32 @close(i32 %13)
%15 = add nuw nsw i32 %13, 1
%16 = tail call i32 (...) @getdtablesize()
%17 = icmp slt i32 %15, %16
br i1 %17, label %12, label %18
18: ; preds = %12, %9
%19 = tail call i32 (...) @setsid()
br label %20
20: ; preds = %29, %18
%21 = tail call i32 @system(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @sleep(i32 120)
%23 = tail call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = tail call i32 @system(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%28 = tail call i32 @system(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %25, %20
br label %20
30: ; preds = %7, %5
%31 = phi i32 [ 1, %5 ], [ 0, %7 ]
ret i32 %31
}