#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  int i, res, desc_size = 0;
  char buf[256];
  fd = open(argv[1], O_RDWR | O_NONBLOCK);
  if (fd < 0) {
    perror("Unable to open device");
    return 1;
  }
  int prior = 21;
  while (1) {
    memset(buf, 0x0, sizeof(buf));
    buf[0] = 0x08;
    buf[7] = 0x02;
    res = write(fd, buf, 8);
    if (res < 0) {
      perror("write");
      exit(1);
    }
    memset(buf, 0x0, sizeof(buf));
    res = read(fd, buf, 8);
    if (res >= 0) {
      if (prior == 21 && buf[0] == 23) {
        printf("Ready to fire!\n");
        fflush(stdout);
      } else if (prior != 22 && buf[0] == 22) {
        printf("Fire!\n");
        fflush(stdout);
      } else if (prior != 21 && buf[0] == 21) {
        printf("Stand down!\n");
        fflush(stdout);
      }
      prior = buf[0];
    }
    usleep(200000);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f6 <main+0x66>
lea    0xe1d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
add    $0x130,%rsp
pop    %rbp
ret
movl   $0x15,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
movb   $0x8,-0x120(%rbp)
movb   $0x2,-0x119(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1258 <main+0xc8>
lea    0xdcd(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1352 <main+0x1c2>
cmpl   $0x15,-0x124(%rbp)
jne    12c8 <main+0x138>
movsbl -0x120(%rbp),%eax
cmp    $0x17,%eax
jne    12c8 <main+0x138>
lea    0xd6c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1345 <main+0x1b5>
cmpl   $0x16,-0x124(%rbp)
je     1304 <main+0x174>
movsbl -0x120(%rbp),%eax
cmp    $0x16,%eax
jne    1304 <main+0x174>
lea    0xd40(%rip),%rdi
call   1040 <printf@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1340 <main+0x1b0>
cmpl   $0x15,-0x124(%rbp)
je     133b <main+0x1ab>
movsbl -0x120(%rbp),%eax
cmp    $0x15,%eax
jne    133b <main+0x1ab>
lea    0xd0b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1340 <main+0x1b0>
jmp    1345 <main+0x1b5>
movsbl -0x120(%rbp),%eax
mov    %eax,-0x124(%rbp)
mov    $0x30d40,%edi
call   10b0 <usleep@plt>
jmp    1200 <main+0x70>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e3c(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jns    11e3 <main+0x53>
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
movb   $0x8,(%rsp)
movb   $0x2,0x7(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    1229 <main+0x99>
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x15,%r13d
mov    %rsp,%rbx
lea    0xdf5(%rip),%r14
mov    0x2d90(%rip),%r15
lea    0xde1(%rip),%r12
jmp    129b <main+0x10b>
nopl   0x0(%rax)
call   1030 <puts@plt>
mov    (%r15),%edi
call   1070 <fflush@plt>
movsbl (%rsp),%r13d
mov    $0x30d40,%edi
call   10b0 <usleep@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
movb   $0x8,(%rsp)
movb   $0x2,0x7(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1213 <main+0x83>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1262 <main+0xd2>
movzbl (%rsp),%eax
cmp    $0x15,%r13d
jne    12ce <main+0x13e>
mov    %r14,%rdi
cmp    $0x17,%al
je     1250 <main+0xc0>
cmp    $0x16,%r13d
je     12df <main+0x14f>
mov    %r12,%rdi
cmp    $0x16,%al
je     1250 <main+0xc0>
cmp    $0x15,%r13d
je     125d <main+0xcd>
lea    0xd2c(%rip),%rdi
cmp    $0x15,%al
je     1250 <main+0xc0>
jmp    125d <main+0xcd>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str = [22 x i8] c"Unable to open device\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [16 x i8] c"Ready to fire!\0A\00"
@stdout = global i32 0
@.str.3 = [7 x i8] c"Fire!\0A\00"
@.str.4 = [13 x i8] c"Stand down!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
ret i32 1
23: ; preds = %2
store i32 21, i32* %11
br label %24
24: ; preds = %23, %87
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 256)
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
store i8 8, i8* %27
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 7
store i8 2, i8* %28
%29 = load i32, i32* %6
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%31 = call i32 @write(i32 %29, i8* %30, i32 8)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %24
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%39 = call i32 @memset(i8* %38, i32 0, i32 256)
%40 = load i32, i32* %6
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 8)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %87
45: ; preds = %37
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, 21
br i1 %47, label %48, label %57
48: ; preds = %45
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 23
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
br label %83
57: ; preds = %48, %45
%58 = load i32, i32* %11
%59 = icmp ne i32 %58, 22
br i1 %59, label %60, label %69
60: ; preds = %57
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 22
br i1 %64, label %65, label %69
65: ; preds = %60
%66 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%67 = load i32, i32* @stdout
%68 = call i32 @fflush(i32 %67)
br label %82
69: ; preds = %60, %57
%70 = load i32, i32* %11
%71 = icmp ne i32 %70, 21
br i1 %71, label %72, label %81
72: ; preds = %69
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp eq i32 %75, 21
br i1 %76, label %77, label %81
77: ; preds = %72
%78 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%79 = load i32, i32* @stdout
%80 = call i32 @fflush(i32 %79)
br label %81
81: ; preds = %77, %72, %69
br label %82
82: ; preds = %81, %65
br label %83
83: ; preds = %82, %53
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
store i32 %86, i32* %11
br label %87
87: ; preds = %83, %37
%88 = call i32 @usleep(i32 200000)
br label %24
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str = [22 x i8] c"Unable to open device\00"
@.str.1 = [6 x i8] c"write\00"
@stdout = global i32 0
@str = [12 x i8] c"Stand down!\00"
@str.5 = [6 x i8] c"Fire!\00"
@str.6 = [15 x i8] c"Ready to fire!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %6, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = call i32 @memset(i8* %4, i32 0, i32 256)
store i8 8, i8* %4
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 7
store i8 2, i8* %14
%15 = call i32 @write(i32 %10, i8* %4, i32 8)
%16 = icmp slt i32 %15, 0
br i1 %16, label %19, label %22
17: ; preds = %2
%18 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
ret i32 1
19: ; preds = %48, %12
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12, %48
%23 = phi i32 [ %49, %48 ], [ 21, %12 ]
%24 = call i32 @memset(i8* %4, i32 0, i32 256)
%25 = call i32 @read(i32 %10, i8* %4, i32 8)
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %48
27: ; preds = %22
%28 = icmp eq i32 %23, 21
%29 = load i8, i8* %4
%30 = icmp eq i8 %29, 23
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %40, label %32
32: ; preds = %27
%33 = icmp ne i32 %23, 22
%34 = icmp eq i8 %29, 22
%35 = select i1 %33, i1 %34, i1 false
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = icmp ne i32 %23, 21
%38 = icmp eq i8 %29, 21
%39 = select i1 %37, i1 %38, i1 false
br i1 %39, label %40, label %45
40: ; preds = %36, %32, %27
%41 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0), %27 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @str.5, i64 0, i64 0), %32 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), %36 ]
%42 = call i32 @puts(i8* dereferenceable(1) %41)
%43 = load i32, i32* @stdout
%44 = call i32 @fflush(i32 %43)
br label %45
45: ; preds = %40, %36
%46 = load i8, i8* %4
%47 = sext i8 %46 to i32
br label %48
48: ; preds = %45, %22
%49 = phi i32 [ %47, %45 ], [ %23, %22 ]
%50 = call i32 @usleep(i32 200000)
%51 = call i32 @memset(i8* %4, i32 0, i32 256)
store i8 8, i8* %4
store i8 2, i8* %14
%52 = call i32 @write(i32 %10, i8* %4, i32 8)
%53 = icmp slt i32 %52, 0
br i1 %53, label %19, label %22
}