#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ WNOHANG;
scalar_t__ execl(char *, char *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ ioctl(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ usleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main() {
  pid_t pid;
  int fd;
  fd = open("/dev/watchdog_dev", O_RDWR);
  if (fd < 0) {
    printf("open watchdog_dev failed\n");
    system("reboot");
    exit(1);
  }
  ioctl(fd, 1);
  pid = fork();
  if (pid < 0) {
    printf("fork failed\n");
    exit(1);
  } else if (pid == 0) {
    if (execl("/etc/run_start.sh", "run_start.sh", (char *)0) < 0) {
      printf("execl failed\n");
      exit(1);
    }
  }
  while (waitpid(pid, NULL, WNOHANG) == 0) {
    ioctl(fd, 3);
    usleep(100000);
    ioctl(fd, 2);
    usleep(100000);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e3 <main+0x53>
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe58(%rip),%rdi
call   1030 <system@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <ioctl@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    121c <main+0x8c>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1263 <main+0xd3>
lea    0xe12(%rip),%rdi
lea    0xe10(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
cmp    $0x0,%rax
jge    125e <main+0xce>
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1263 <main+0xd3>
jmp    1268 <main+0xd8>
jmp    126d <main+0xdd>
mov    -0x10(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jne    12c0 <main+0x130>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   1050 <ioctl@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1050 <ioctl@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
jmp    126d <main+0xdd>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    11d0 <main+0x40>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe51(%rip),%rdi
call   1040 <system@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1050 <ioctl@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11f3 <main+0x63>
lea    0xe47(%rip),%rdi
jmp    1219 <main+0x89>
mov    %rax,%r14
jne    1228 <main+0x98>
lea    0xe1a(%rip),%rdi
lea    0xe18(%rip),%rsi
xor    %edx,%edx
call   1090 <execl@plt>
test   %rax,%rax
jns    1228 <main+0x98>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2db1(%rip),%r15
nop
mov    (%r15),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <waitpid@plt>
test   %rax,%rax
jne    1270 <main+0xe0>
mov    %ebx,%edi
mov    $0x3,%esi
call   1050 <ioctl@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <ioctl@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
jmp    1230 <main+0xa0>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"/dev/watchdog_dev\00"
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"open watchdog_dev failed\0A\00"
@.str.2 = [7 x i8] c"reboot\00"
@.str.3 = [13 x i8] c"fork failed\0A\00"
@.str.4 = [18 x i8] c"/etc/run_start.sh\00"
@.str.5 = [13 x i8] c"run_start.sh\00"
@.str.6 = [14 x i8] c"execl failed\0A\00"
@WNOHANG = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @system(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %3
%14 = call i32 @ioctl(i32 %13, i32 1)
%15 = call i64 (...) @fork()
store i64 %15, i64* %2
%16 = load i64, i64* %2
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load i64, i64* %2
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = call i64 @execl(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* null)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24
br label %31
31: ; preds = %30, %21
br label %32
32: ; preds = %31
br label %33
33: ; preds = %38, %32
%34 = load i64, i64* %2
%35 = load i32, i32* @WNOHANG
%36 = call i64 @waitpid(i64 %34, i32* null, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %33
%39 = load i32, i32* %3
%40 = call i32 @ioctl(i32 %39, i32 3)
%41 = call i32 @usleep(i32 100000)
%42 = load i32, i32* %3
%43 = call i32 @ioctl(i32 %42, i32 2)
%44 = call i32 @usleep(i32 100000)
br label %33
45: ; preds = %33
%46 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [18 x i8] c"/dev/watchdog_dev\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"reboot\00"
@.str.4 = [18 x i8] c"/etc/run_start.sh\00"
@.str.5 = [13 x i8] c"run_start.sh\00"
@WNOHANG = global i32 0
@str = [13 x i8] c"execl failed\00"
@str.7 = [12 x i8] c"fork failed\00"
@str.8 = [25 x i8] c"open watchdog_dev failed\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
%6 = tail call i32 @system(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 @ioctl(i32 %2, i32 1)
%10 = tail call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = icmp eq i64 %10, 0
br i1 %16, label %17, label %23
17: ; preds = %15
%18 = tail call i64 @execl(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* null)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15, %17
%24 = load i32, i32* @WNOHANG
%25 = tail call i64 @waitpid(i64 %10, i32* null, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %23, %27
%28 = tail call i32 @ioctl(i32 %2, i32 3)
%29 = tail call i32 @usleep(i32 100000)
%30 = tail call i32 @ioctl(i32 %2, i32 2)
%31 = tail call i32 @usleep(i32 100000)
%32 = load i32, i32* @WNOHANG
%33 = tail call i64 @waitpid(i64 %10, i32* null, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %27, label %35
35: ; preds = %27, %23
%36 = tail call i32 @exit(i32 0)
unreachable
}