int main() {
  char cmds[2][15] = {"button_clicked", "cycle_complete"};
  char states[6][22] = {"CLOSED",
                        "OPENING",
                        "OPEN",
                        "CLOSING",
                        "STOPPED_WHILE_CLOSING",
                        "STOPPED_WHILE_OPENING"};
  char command[15];
  int state = 0;
  printf("Door: %s\n", states[state]);
  while (scanf("%15s", command) == 1) {
    if (strcmp(command, cmds[0]) == 0) {
      switch (state) {
      case 0: {
        state = 1;
        break;
      }
      case 1: {
        state = 5;
        break;
      }
      case 2: {
        state = 3;
        break;
      }
      case 3: {
        state = 4;
        break;
      }
      case 4: {
        state = 1;
        break;
      }
      case 5: {
        state = 3;
        break;
      }
      }
    } else if (strcmp(command, cmds[1]) == 0) {
      switch (state) {
      case 1: {
        state = 2;
        break;
      }
      case 3: {
        state = 0;
        break;
      }
      }
    }
    printf("Door: %s\n", states[state]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xec7(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xec4(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xec1(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xebf(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xeb9(%rip),%ax
mov    %ax,-0x14(%rbp)
lea    -0xc0(%rbp),%rdi
lea    0xeab(%rip),%rsi
mov    $0x84,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0xd4(%rbp)
movslq -0xd4(%rbp),%rax
lea    -0xc0(%rbp),%rsi
imul   $0x16,%rax,%rax
add    %rax,%rsi
lea    0xeff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xcf(%rbp),%rsi
lea    0xef4(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    1321 <main+0x1e1>
lea    -0xcf(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    128b <main+0x14b>
mov    -0xd4(%rbp),%eax
mov    %rax,-0xe0(%rbp)
sub    $0x5,%rax
ja     1286 <main+0x146>
mov    -0xe0(%rbp),%rax
lea    0xdd8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0xd4(%rbp)
jmp    1286 <main+0x146>
movl   $0x5,-0xd4(%rbp)
jmp    1286 <main+0x146>
movl   $0x3,-0xd4(%rbp)
jmp    1286 <main+0x146>
movl   $0x4,-0xd4(%rbp)
jmp    1286 <main+0x146>
movl   $0x1,-0xd4(%rbp)
jmp    1286 <main+0x146>
movl   $0x3,-0xd4(%rbp)
jmp    12f9 <main+0x1b9>
lea    -0xcf(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0xf,%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    12f4 <main+0x1b4>
mov    -0xd4(%rbp),%eax
mov    %eax,-0xe4(%rbp)
sub    $0x1,%eax
je     12d6 <main+0x196>
jmp    12c2 <main+0x182>
mov    -0xe4(%rbp),%eax
sub    $0x3,%eax
je     12e5 <main+0x1a5>
jmp    12ef <main+0x1af>
movl   $0x2,-0xd4(%rbp)
jmp    12ef <main+0x1af>
movl   $0x0,-0xd4(%rbp)
jmp    12f4 <main+0x1b4>
jmp    12f9 <main+0x1b9>
movslq -0xd4(%rbp),%rax
lea    -0xc0(%rbp),%rsi
imul   $0x16,%rax,%rax
add    %rax,%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11cc <main+0x8c>
xor    %eax,%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xec8(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    $0x84,%edx
mov    %rbx,%rdi
call   1050 <memcpy@plt>
lea    0xf4b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf44(%rip),%rdi
lea    0x1(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    1247 <main+0x107>
lea    0xe62(%rip),%r14
lea    0x1(%rsp),%rbx
lea    0xf12(%rip),%r15
lea    0xf15(%rip),%r12
lea    0xe57(%rip),%r13
xor    %ebp,%ebp
jmp    11f2 <main+0xb2>
xor    %ebp,%ebp
xchg   %ax,%ax
movslq %ebp,%rax
lea    (%rax,%rax,4),%rcx
lea    (%rax,%rcx,4),%rcx
add    %rax,%rcx
lea    (%rsp,%rcx,1),%rsi
add    $0x10,%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmp    $0x1,%eax
jne    1247 <main+0x107>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     1230 <main+0xf0>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
jne    11c0 <main+0x80>
cmp    $0x3,%ebp
je     11bc <main+0x7c>
cmp    $0x1,%ebp
jne    11c0 <main+0x80>
mov    $0x2,%ebp
jmp    11c0 <main+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x6,%ebp
jae    11c0 <main+0x80>
movslq %ebp,%rax
lea    0xe65(%rip),%rcx
mov    (%rcx,%rax,4),%ebp
jmp    11c0 <main+0x80>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.cmds = [2 x [15 x i8]] [[15 x i8] c"button_clicked\00", [15 x i8] c"cycle_complete\00"]
@__const.main.states = [6 x [22 x i8]] [[22 x i8] c"CLOSED\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"OPENING\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"OPEN\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"CLOSING\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"STOPPED_WHILE_CLOSING\00", [22 x i8] c"STOPPED_WHILE_OPENING\00"]
@.str = [10 x i8] c"Door: %s\0A\00"
@.str.1 = [5 x i8] c"%15s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x [15 x i8]]
%3 = alloca [6 x [22 x i8]]
%4 = alloca [15 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [2 x [15 x i8]]* %2 to i8*
%7 = bitcast [6 x [22 x i8]]* %3 to i8*
store i32 0, i32* %5
%8 = load i32, i32* %5
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [6 x [22 x i8]], [6 x [22 x i8]]* %3, i64 0, i64 %9
%11 = getelementptr inbounds [22 x i8], [22 x i8]* %10, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %44, %0
%14 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%15 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %50
17: ; preds = %13
%18 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%19 = getelementptr inbounds [2 x [15 x i8]], [2 x [15 x i8]]* %2, i64 0, i64 0
%20 = getelementptr inbounds [15 x i8], [15 x i8]* %19, i64 0, i64 0
%21 = call i32 @strcmp(i8* %18, i8* %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %17
%24 = load i32, i32* %5
switch i32 %24, label %31 [
i32 0, label %25
i32 1, label %26
i32 2, label %27
i32 3, label %28
i32 4, label %29
i32 5, label %30
]
25: ; preds = %23
store i32 1, i32* %5
br label %31
26: ; preds = %23
store i32 5, i32* %5
br label %31
27: ; preds = %23
store i32 3, i32* %5
br label %31
28: ; preds = %23
store i32 4, i32* %5
br label %31
29: ; preds = %23
store i32 1, i32* %5
br label %31
30: ; preds = %23
store i32 3, i32* %5
br label %31
31: ; preds = %23, %30, %29, %28, %27, %26, %25
br label %44
32: ; preds = %17
%33 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%34 = getelementptr inbounds [2 x [15 x i8]], [2 x [15 x i8]]* %2, i64 0, i64 1
%35 = getelementptr inbounds [15 x i8], [15 x i8]* %34, i64 0, i64 0
%36 = call i32 @strcmp(i8* %33, i8* %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %32
%39 = load i32, i32* %5
switch i32 %39, label %42 [
i32 1, label %40
i32 3, label %41
]
40: ; preds = %38
store i32 2, i32* %5
br label %42
41: ; preds = %38
store i32 0, i32* %5
br label %42
42: ; preds = %38, %41, %40
br label %43
43: ; preds = %42, %32
br label %44
44: ; preds = %43, %31
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [6 x [22 x i8]], [6 x [22 x i8]]* %3, i64 0, i64 %46
%48 = getelementptr inbounds [22 x i8], [22 x i8]* %47, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %48)
br label %13
50: ; preds = %13
ret i32 0
}

LLVM IR (-O2):
@__const.main.cmds = [2 x [15 x i8]] [[15 x i8] c"button_clicked\00", [15 x i8] c"cycle_complete\00"]
@__const.main.states = [6 x [22 x i8]] [[22 x i8] c"CLOSED\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"OPENING\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"OPEN\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"CLOSING\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", [22 x i8] c"STOPPED_WHILE_CLOSING\00", [22 x i8] c"STOPPED_WHILE_OPENING\00"]
@.str = [10 x i8] c"Door: %s\0A\00"
@.str.1 = [5 x i8] c"%15s\00"
@switch.table.main = [6 x i32] [i32 1, i32 5, i32 3, i32 4, i32 1, i32 3]
define i32 @main() {
%1 = alloca [6 x [22 x i8]]
%2 = alloca [15 x i8]
%3 = getelementptr inbounds [6 x [22 x i8]], [6 x [22 x i8]]* %1, i64 0, i64 0, i64 0
%4 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %3)
%6 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %31
8: ; preds = %0, %24
%9 = phi i32 [ %25, %24 ], [ 0, %0 ]
%10 = call i32 @strcmp(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([2 x [15 x i8]], [2 x [15 x i8]]* @__const.main.cmds, i64 0, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = icmp ult i32 %9, 6
br i1 %13, label %20, label %24
14: ; preds = %8
%15 = call i32 @strcmp(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([2 x [15 x i8]], [2 x [15 x i8]]* @__const.main.cmds, i64 0, i64 1, i64 0))
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
switch i32 %9, label %24 [
i32 1, label %18
i32 3, label %19
]
18: ; preds = %17
br label %24
19: ; preds = %17
br label %24
20: ; preds = %12
%21 = sext i32 %9 to i64
%22 = getelementptr inbounds [6 x i32], [6 x i32]* @switch.table.main, i64 0, i64 %21
%23 = load i32, i32* %22
br label %24
24: ; preds = %20, %12, %14, %17, %19, %18
%25 = phi i32 [ %9, %12 ], [ %9, %17 ], [ 0, %19 ], [ 2, %18 ], [ %9, %14 ], [ %23, %20 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [6 x [22 x i8]], [6 x [22 x i8]]* %1, i64 0, i64 %26, i64 0
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%30 = icmp eq i32 %29, 1
br i1 %30, label %8, label %31
31: ; preds = %24, %0
ret i32 0
}