#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 */
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int scanf(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int main() {
  int state = 1;
  int input_state = 0;
  char *input = malloc(15);
  printf("Door: CLOSED\n");
  while (scanf("%s", input) == 1) {
    if (!strcmp(input, "button_clicked")) {
      input_state = 0;
    } else if (!strcmp(input, "cycle_complete")) {
      input_state = 1;
    } else {
      continue;
    }
    if ((state == 1) && (!input_state)) {
      state = 2;
      printf("Door: OPENING\n");
      continue;
    }
    if ((state == 2) && (!input_state)) {
      state = 6;
      printf("Door: STOPPED_WHILE_OPENING\n");
      continue;
    }
    if ((state == 3) && (!input_state)) {
      state = 4;
      printf("Door: CLOSING\n");
      continue;
    }
    if ((state == 4) && (!input_state)) {
      state = 5;
      printf("Door: STOPPED_WHILE_CLOSING\n");
      continue;
    }
    if ((state == 5) && (!input_state)) {
      state = 2;
      printf("Door: OPENING\n");
      continue;
    }
    if ((state == 6) && (!input_state)) {
      state = 4;
      printf("Door: CLOSING\n");
      continue;
    }
    if ((state == 2) && (input_state)) {
      state = 3;
      printf("Door: OPEN\n");
      continue;
    }
    if ((state == 4) && (input_state)) {
      state = 1;
      printf("Door: CLOSED\n");
      continue;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0xf,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe8c(%rip),%rdi
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    1349 <main+0x209>
mov    -0x18(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11b5 <main+0x75>
movl   $0x0,-0xc(%rbp)
jmp    11e4 <main+0xa4>
mov    -0x18(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11da <main+0x9a>
movl   $0x1,-0xc(%rbp)
jmp    11df <main+0x9f>
jmp    1177 <main+0x37>
jmp    11e4 <main+0xa4>
cmpl   $0x1,-0x8(%rbp)
jne    1210 <main+0xd0>
cmpl   $0x0,-0xc(%rbp)
jne    1210 <main+0xd0>
movl   $0x2,-0x8(%rbp)
lea    0xe29(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
cmpl   $0x2,-0x8(%rbp)
jne    123c <main+0xfc>
cmpl   $0x0,-0xc(%rbp)
jne    123c <main+0xfc>
movl   $0x6,-0x8(%rbp)
lea    0xe0c(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
cmpl   $0x3,-0x8(%rbp)
jne    1268 <main+0x128>
cmpl   $0x0,-0xc(%rbp)
jne    1268 <main+0x128>
movl   $0x4,-0x8(%rbp)
lea    0xdfd(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
cmpl   $0x4,-0x8(%rbp)
jne    1294 <main+0x154>
cmpl   $0x0,-0xc(%rbp)
jne    1294 <main+0x154>
movl   $0x5,-0x8(%rbp)
lea    0xde0(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
cmpl   $0x5,-0x8(%rbp)
jne    12c0 <main+0x180>
cmpl   $0x0,-0xc(%rbp)
jne    12c0 <main+0x180>
movl   $0x2,-0x8(%rbp)
lea    0xd79(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
cmpl   $0x6,-0x8(%rbp)
jne    12ec <main+0x1ac>
cmpl   $0x0,-0xc(%rbp)
jne    12ec <main+0x1ac>
movl   $0x4,-0x8(%rbp)
lea    0xd79(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
cmpl   $0x2,-0x8(%rbp)
jne    1318 <main+0x1d8>
cmpl   $0x0,-0xc(%rbp)
je     1318 <main+0x1d8>
movl   $0x3,-0x8(%rbp)
lea    0xd79(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
cmpl   $0x4,-0x8(%rbp)
jne    1344 <main+0x204>
cmpl   $0x0,-0xc(%rbp)
je     1344 <main+0x204>
movl   $0x1,-0x8(%rbp)
lea    0xcc6(%rip),%rdi
call   1030 <printf@plt>
jmp    1177 <main+0x37>
jmp    1177 <main+0x37>
xor    %eax,%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
mov    $0xf,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xf16(%rip),%rdi
call   1030 <puts@plt>
lea    0xe95(%rip),%rdi
mov    %rbx,%rsi
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    12d0 <main+0x190>
mov    $0x1,%r13d
lea    0xe7a(%rip),%r15
lea    0xe70(%rip),%r12
jmp    11b5 <main+0x75>
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%r13d
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    12d0 <main+0x190>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcmp@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1290 <main+0x150>
mov    %rbx,%rdi
lea    0xe3e(%rip),%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
jne    11a1 <main+0x61>
test   %ebp,%ebp
setne  %al
mov    $0x4,%r14d
cmp    $0x3,%r13d
jne    11f2 <main+0xb2>
test   %al,%al
je     1192 <main+0x52>
cmp    $0x4,%r13d
setne  %cl
or     %al,%cl
mov    $0x5,%r14d
lea    0xe35(%rip),%rdi
cmp    $0x1,%cl
jne    1199 <main+0x59>
cmp    $0x5,%r13d
setne  %cl
or     %al,%cl
mov    $0x2,%r14d
lea    0xe36(%rip),%rdi
cmp    $0x1,%cl
jne    1199 <main+0x59>
cmp    $0x6,%r13d
setne  %cl
or     %al,%cl
lea    0xe2b(%rip),%rdi
mov    $0x4,%r14d
cmp    $0x1,%cl
jne    1199 <main+0x59>
cmp    $0x2,%r13d
sete   %cl
mov    $0x3,%r14d
lea    0xe23(%rip),%rdi
test   %al,%cl
jne    1199 <main+0x59>
cmp    $0x4,%r13d
sete   %cl
mov    $0x1,%r14d
lea    0xdfa(%rip),%rdi
test   %al,%cl
jne    1199 <main+0x59>
jmp    11a1 <main+0x61>
nopl   0x0(%rax,%rax,1)
mov    $0x2,%r14d
lea    0xdbc(%rip),%rdi
cmp    $0x1,%r13d
je     1199 <main+0x59>
test   %ebp,%ebp
setne  %al
cmp    $0x2,%r13d
jne    11e2 <main+0xa2>
mov    $0x6,%r14d
lea    0xd5e(%rip),%rdi
test   %al,%al
je     1199 <main+0x59>
jmp    11e2 <main+0xa2>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Door: CLOSED\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [15 x i8] c"button_clicked\00"
@.str.3 = [15 x i8] c"cycle_complete\00"
@.str.4 = [15 x i8] c"Door: OPENING\0A\00"
@.str.5 = [29 x i8] c"Door: STOPPED_WHILE_OPENING\0A\00"
@.str.6 = [15 x i8] c"Door: CLOSING\0A\00"
@.str.7 = [29 x i8] c"Door: STOPPED_WHILE_CLOSING\0A\00"
@.str.8 = [12 x i8] c"Door: OPEN\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
store i32 1, i32* %2
store i32 0, i32* %3
%5 = call i8* @malloc(i32 15)
store i8* %5, i8** %4
%6 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %87, %85, %77, %69, %61, %53, %45, %37, %29, %21, %0
%8 = load i8*, i8** %4
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %88
11: ; preds = %7
%12 = load i8*, i8** %4
%13 = call i32 @strcmp(i8* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11
store i32 0, i32* %3
br label %23
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %16
store i32 1, i32* %3
br label %22
21: ; preds = %16
br label %7
22: ; preds = %20
br label %23
23: ; preds = %22, %15
%24 = load i32, i32* %2
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32, i32* %3
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
store i32 2, i32* %2
%30 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %7
31: ; preds = %26, %23
%32 = load i32, i32* %2
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32, i32* %3
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
store i32 6, i32* %2
%38 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %7
39: ; preds = %34, %31
%40 = load i32, i32* %2
%41 = icmp eq i32 %40, 3
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i32, i32* %3
%44 = icmp ne i32 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
store i32 4, i32* %2
%46 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %7
47: ; preds = %42, %39
%48 = load i32, i32* %2
%49 = icmp eq i32 %48, 4
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i32, i32* %3
%52 = icmp ne i32 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %50
store i32 5, i32* %2
%54 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %7
55: ; preds = %50, %47
%56 = load i32, i32* %2
%57 = icmp eq i32 %56, 5
br i1 %57, label %58, label %63
58: ; preds = %55
%59 = load i32, i32* %3
%60 = icmp ne i32 %59, 0
br i1 %60, label %63, label %61
61: ; preds = %58
store i32 2, i32* %2
%62 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %7
63: ; preds = %58, %55
%64 = load i32, i32* %2
%65 = icmp eq i32 %64, 6
br i1 %65, label %66, label %71
66: ; preds = %63
%67 = load i32, i32* %3
%68 = icmp ne i32 %67, 0
br i1 %68, label %71, label %69
69: ; preds = %66
store i32 4, i32* %2
%70 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %7
71: ; preds = %66, %63
%72 = load i32, i32* %2
%73 = icmp eq i32 %72, 2
br i1 %73, label %74, label %79
74: ; preds = %71
%75 = load i32, i32* %3
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %74
store i32 3, i32* %2
%78 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %7
79: ; preds = %74, %71
%80 = load i32, i32* %2
%81 = icmp eq i32 %80, 4
br i1 %81, label %82, label %87
82: ; preds = %79
%83 = load i32, i32* %3
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %82
store i32 1, i32* %2
%86 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %7
87: ; preds = %82, %79
br label %7
88: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [15 x i8] c"button_clicked\00"
@.str.3 = [15 x i8] c"cycle_complete\00"
@str.10 = [28 x i8] c"Door: STOPPED_WHILE_OPENING\00"
@str.12 = [28 x i8] c"Door: STOPPED_WHILE_CLOSING\00"
@str.13 = [14 x i8] c"Door: OPENING\00"
@str.14 = [14 x i8] c"Door: CLOSING\00"
@str.15 = [13 x i8] c"Door: CLOSED\00"
@str.16 = [11 x i8] c"Door: OPEN\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(15) i8* @malloc(i32 15)
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.15, i64 0, i64 0))
%3 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %47
5: ; preds = %0, %20
%6 = phi i32 [ %21, %20 ], [ 1, %0 ]
%7 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(15) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(15) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = xor i1 %8, true
br label %28
14: ; preds = %5
%15 = icmp eq i32 %6, 1
br i1 %15, label %16, label %24
16: ; preds = %14, %44, %41, %38, %35, %32, %28, %24
%17 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.10, i64 0, i64 0), %24 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.14, i64 0, i64 0), %28 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.12, i64 0, i64 0), %32 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0), %35 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.14, i64 0, i64 0), %38 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.16, i64 0, i64 0), %41 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.15, i64 0, i64 0), %44 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0), %14 ]
%18 = phi i32 [ 6, %24 ], [ 4, %28 ], [ 5, %32 ], [ 2, %35 ], [ 4, %38 ], [ 3, %41 ], [ 1, %44 ], [ 2, %14 ]
%19 = tail call i32 @puts(i8* dereferenceable(1) %17)
br label %20
20: ; preds = %16, %9, %44
%21 = phi i32 [ %6, %9 ], [ %6, %44 ], [ %18, %16 ]
%22 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %5, label %47
24: ; preds = %14
%25 = xor i1 %8, true
%26 = icmp ne i32 %6, 2
%27 = select i1 %26, i1 true, i1 %25
br i1 %27, label %28, label %16
28: ; preds = %12, %24
%29 = phi i1 [ %13, %12 ], [ %25, %24 ]
%30 = icmp ne i32 %6, 3
%31 = select i1 %30, i1 true, i1 %29
br i1 %31, label %32, label %16
32: ; preds = %28
%33 = icmp ne i32 %6, 4
%34 = select i1 %33, i1 true, i1 %29
br i1 %34, label %35, label %16
35: ; preds = %32
%36 = icmp ne i32 %6, 5
%37 = select i1 %36, i1 true, i1 %29
br i1 %37, label %38, label %16
38: ; preds = %35
%39 = icmp ne i32 %6, 6
%40 = select i1 %39, i1 true, i1 %29
br i1 %40, label %41, label %16
41: ; preds = %38
%42 = icmp eq i32 %6, 2
%43 = select i1 %42, i1 %29, i1 false
br i1 %43, label %16, label %44
44: ; preds = %41
%45 = icmp eq i32 %6, 4
%46 = select i1 %45, i1 %29, i1 false
br i1 %46, label %16, label %20
47: ; preds = %20, %0
ret i32 0
}