#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 */
struct inotify_event {
  int mask;
  scalar_t__ len;
  int /*<<< orphan*/ name;
};

/* Variables and functions */
int IN_ISDIR;
int /*<<< orphan*/ RB_AUTOBOOT;
int /*<<< orphan*/ handle_config();
int /*<<< orphan*/ handle_login();
int /*<<< orphan*/ handle_network_settings();
int /*<<< orphan*/ reboot(int /*<<< orphan*/);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ restart_app();
int /*<<< orphan*/ sleep(int);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
void handle_event(int length, char buffer[]) {
  int i = 0;
  struct inotify_event *event;
  while (i < length) {
    event = (struct inotify_event *)&buffer[i];
    if (event->len) {
      if (!(event->mask & IN_ISDIR)) {
        if (strcmp(event->name, "reboot") == 0) {
          sleep(1);
          reboot(RB_AUTOBOOT);
        }
        if (strcmp(event->name, "restart_app") == 0) {
          restart_app();
          remove("/tmp/restart_app");
        }
        if (strcmp(event->name, "activate_config") == 0) {
          handle_config();
          handle_login();
          handle_network_settings();
          remove("/tmp/activate_config");
        }
      }
    }
    i += (sizeof(struct inotify_event)) + event->len;
  }
}


x86 assembly (clang, -O0):
<handle_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12a0 <handle_event+0x120>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1285 <handle_event+0x105>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    0x2e1b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1280 <handle_event+0x100>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
lea    0xe1a(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    120d <handle_event+0x8d>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1090 <reboot@plt>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
lea    0xdf1(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    123d <handle_event+0xbd>
mov    $0x0,%al
call   1070 <restart_app@plt>
lea    0xdcf(%rip),%rdi
call   1030 <remove@plt>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
lea    0xdd2(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    127b <handle_event+0xfb>
mov    $0x0,%al
call   1080 <handle_config@plt>
mov    $0x0,%al
call   1060 <handle_login@plt>
mov    $0x0,%al
call   1040 <handle_network_settings@plt>
lea    0xda2(%rip),%rdi
call   1030 <remove@plt>
jmp    1280 <handle_event+0x100>
jmp    1285 <handle_event+0x105>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x18,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
jmp    1196 <handle_event+0x16>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_event>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
jle    1277 <handle_event+0xf7>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e49(%rip),%r12
lea    0xe66(%rip),%r15
lea    0xe70(%rip),%r13
xor    %eax,%eax
jmp    11d3 <handle_event+0x53>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rbx,%r14,1),%eax
add    %r14d,%eax
add    $0x18,%eax
cmp    %ebp,%eax
jge    1277 <handle_event+0xf7>
movslq %eax,%r14
cmpq   $0x0,0x8(%rbx,%r14,1)
je     11c0 <handle_event+0x40>
mov    (%r12),%eax
test   %eax,(%rbx,%r14,1)
jne    11c0 <handle_event+0x40>
mov    0x10(%rbx,%r14,1),%edi
lea    0xe0c(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1216 <handle_event+0x96>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1090 <reboot@plt>
mov    0x10(%rbx,%r14,1),%edi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    123b <handle_event+0xbb>
xor    %eax,%eax
call   1070 <restart_app@plt>
lea    0xdd1(%rip),%rdi
call   1030 <remove@plt>
mov    0x10(%rbx,%r14,1),%edi
mov    %r13,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11c0 <handle_event+0x40>
xor    %eax,%eax
call   1080 <handle_config@plt>
xor    %eax,%eax
call   1060 <handle_login@plt>
xor    %eax,%eax
call   1040 <handle_network_settings@plt>
lea    0xdab(%rip),%rdi
call   1030 <remove@plt>
jmp    11c0 <handle_event+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i32, i64, i32 }
@IN_ISDIR = global i32 0
@.str = [7 x i8] c"reboot\00"
@RB_AUTOBOOT = global i32 0
@.str.1 = [12 x i8] c"restart_app\00"
@.str.2 = [17 x i8] c"/tmp/restart_app\00"
@.str.3 = [16 x i8] c"activate_config\00"
@.str.4 = [21 x i8] c"/tmp/activate_config\00"
define void @handle_event(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.inotify_event*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %7
7: ; preds = %60, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %3
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %69
11: ; preds = %7
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = bitcast i8* %15 to %struct.inotify_event*
store %struct.inotify_event* %16, %struct.inotify_event** %6
%17 = load %struct.inotify_event*, %struct.inotify_event** %6
%18 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %17, i32 0, i32 1
%19 = load i64, i64* %18
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %60
21: ; preds = %11
%22 = load %struct.inotify_event*, %struct.inotify_event** %6
%23 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @IN_ISDIR
%26 = and i32 %24, %25
%27 = icmp ne i32 %26, 0
br i1 %27, label %59, label %28
28: ; preds = %21
%29 = load %struct.inotify_event*, %struct.inotify_event** %6
%30 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %29, i32 0, i32 2
%31 = load i32, i32* %30
%32 = call i64 @strcmp(i32 %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = call i32 @sleep(i32 1)
%36 = load i32, i32* @RB_AUTOBOOT
%37 = call i32 @reboot(i32 %36)
br label %38
38: ; preds = %34, %28
%39 = load %struct.inotify_event*, %struct.inotify_event** %6
%40 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %39, i32 0, i32 2
%41 = load i32, i32* %40
%42 = call i64 @strcmp(i32 %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = call i32 (...) @restart_app()
%46 = call i32 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %44, %38
%48 = load %struct.inotify_event*, %struct.inotify_event** %6
%49 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %48, i32 0, i32 2
%50 = load i32, i32* %49
%51 = call i64 @strcmp(i32 %50, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %47
%54 = call i32 (...) @handle_config()
%55 = call i32 (...) @handle_login()
%56 = call i32 (...) @handle_network_settings()
%57 = call i32 @remove(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %53, %47
br label %59
59: ; preds = %58, %21
br label %60
60: ; preds = %59, %11
%61 = load %struct.inotify_event*, %struct.inotify_event** %6
%62 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %61, i32 0, i32 1
%63 = load i64, i64* %62
%64 = add i64 24, %63
%65 = load i32, i32* %5
%66 = sext i32 %65 to i64
%67 = add i64 %66, %64
%68 = trunc i64 %67 to i32
store i32 %68, i32* %5
br label %7
69: ; preds = %7
ret void
}

LLVM IR (-O2):
@IN_ISDIR = global i32 0
@.str = [7 x i8] c"reboot\00"
@RB_AUTOBOOT = global i32 0
@.str.1 = [12 x i8] c"restart_app\00"
@.str.2 = [17 x i8] c"/tmp/restart_app\00"
@.str.3 = [16 x i8] c"activate_config\00"
@.str.4 = [21 x i8] c"/tmp/activate_config\00"
define void @handle_event(i32 %0, i8* nocapture %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %50
4: ; preds = %2, %44
%5 = phi i32 [ %48, %44 ], [ 0, %2 ]
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 8
%9 = bitcast i8* %8 to i64*
%10 = load i64, i64* %9
%11 = icmp eq i64 %10, 0
br i1 %11, label %44, label %12
12: ; preds = %4
%13 = bitcast i8* %7 to i32*
%14 = load i32, i32* %13
%15 = load i32, i32* @IN_ISDIR
%16 = and i32 %15, %14
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %44
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %7, i64 16
%20 = bitcast i8* %19 to i32*
%21 = load i32, i32* %20
%22 = tail call i64 @strcmp(i32 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = tail call i32 @sleep(i32 1)
%26 = load i32, i32* @RB_AUTOBOOT
%27 = tail call i32 @reboot(i32 %26)
br label %28
28: ; preds = %24, %18
%29 = load i32, i32* %20
%30 = tail call i64 @strcmp(i32 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = tail call i32 (...) @restart_app()
%34 = tail call i32 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %28
%36 = load i32, i32* %20
%37 = tail call i64 @strcmp(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = tail call i32 (...) @handle_config()
%41 = tail call i32 (...) @handle_login()
%42 = tail call i32 (...) @handle_network_settings()
%43 = tail call i32 @remove(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %12, %39, %35, %4
%45 = load i64, i64* %9
%46 = trunc i64 %45 to i32
%47 = add i32 %5, 24
%48 = add i32 %47, %46
%49 = icmp slt i32 %48, %0
br i1 %49, label %4, label %50
50: ; preds = %44, %2
ret void
}