#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 pollfd {
  int fd;
  int events;
  int revents;
};
typedef int /*<<< orphan*/ fdset;

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ beep_fd_close(int);
int beep_fd_open();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, char **, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ system(char *);
int main(int argc, char **argv, char **envp) {
  struct pollfd fdset[1];
  int nfds = 2;
  int beep_fd, timeout, rc;
  char *buf[64];
  int len;
  if (argc < 2) {
    printf("Usage: beepcmd \"<command>\"\n\n");
    printf(
        "Runs a command when there is a console beep by reading /dev/beep\n");
    exit(-1);
  }
  beep_fd = beep_fd_open();
  timeout = (3 * 1000);
  while (1) {
    memset((void *)fdset, 0, sizeof(fdset));
    fdset[0].fd = beep_fd;
    fdset[0].events = POLLIN;
    lseek(fdset[0].fd, 0, SEEK_SET);
    rc = poll(fdset, nfds, timeout);
    if (rc < 0) {
      printf("\npoll() failed!\n");
      return -1;
    }
    if (fdset[0].revents & POLLIN) {
      len = read(fdset[0].fd, buf, 64);
      system(argv[1]);
    }
    fflush(stdout);
  }
  beep_fd_close(beep_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x2,-0x2c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e0 <main+0x50>
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1080 <beep_fd_open@plt>
mov    %eax,-0x30(%rbp)
movl   $0xbb8,-0x34(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
lea    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x34(%rbp),%edx
call   10a0 <poll@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    125c <main+0xcc>
lea    0xe16(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x250,%rsp
pop    %rbp
ret
mov    -0x20(%rbp),%eax
mov    0x2d82(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1298 <main+0x108>
mov    -0x28(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,-0x244(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <system@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    11f1 <main+0x61>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
cmp    $0x1,%edi
jle    125f <main+0xcf>
mov    %rsi,%r14
xor    %eax,%eax
call   1080 <beep_fd_open@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    0x2e28(%rip),%r12
mov    0x2e11(%rip),%r13
mov    0x2e02(%rip),%r15
jmp    11d8 <main+0x48>
mov    (%r15),%edi
call   1090 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %ebp,(%rsp)
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    0x0(%r13),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0xbb8,%edx
call   10a0 <poll@plt>
test   %eax,%eax
js     123c <main+0xac>
mov    (%r12),%eax
test   %eax,0x8(%rsp)
je     11d0 <main+0x40>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    0x8(%r14),%rdi
call   1040 <system@plt>
jmp    11d0 <main+0x40>
lea    0xdbd(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdaa(%rip),%rdi
call   1030 <puts@plt>
lea    0xdba(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [29 x i8] c"Usage: beepcmd \22<command>\22\0A\0A\00"
@.str.1 = [66 x i8] c"Runs a command when there is a console beep by reading /dev/beep\0A\00"
@POLLIN = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [17 x i8] c"\0Apoll() failed!\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [1 x %struct.pollfd]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [64 x i8*]
%14 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 2, i32* %9
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %3
%22 = call i32 (...) @beep_fd_open()
store i32 %22, i32* %10
store i32 3000, i32* %11
br label %23
23: ; preds = %21, %63
%24 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%25 = bitcast %struct.pollfd* %24 to i8*
%26 = call i32 @memset(i8* %25, i32 0, i32 12)
%27 = load i32, i32* %10
%28 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* @POLLIN
%31 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @lseek(i32 %35, i32 0, i32 %36)
%38 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%39 = load i32, i32* %9
%40 = load i32, i32* %11
%41 = call i32 @poll(%struct.pollfd* %38, i32 %39, i32 %40)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %23
%45 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
ret i32 -1
46: ; preds = %23
%47 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%48 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = load i32, i32* @POLLIN
%51 = and i32 %49, %50
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %63
53: ; preds = %46
%54 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%55 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = getelementptr inbounds [64 x i8*], [64 x i8*]* %13, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8** %57, i32 64)
store i32 %58, i32* %14
%59 = load i8**, i8*** %6
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = call i32 @system(i8* %61)
br label %63
63: ; preds = %53, %46
%64 = load i32, i32* @stdout
%65 = call i32 @fflush(i32 %64)
br label %23
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@SEEK_SET = global i32 0
@stdout = global i32 0
@str = [16 x i8] c"\0Apoll() failed!\00"
@str.3 = [28 x i8] c"Usage: beepcmd \22<command>\22\0A\00"
@str.4 = [65 x i8] c"Runs a command when there is a console beep by reading /dev/beep\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = alloca [1 x %struct.pollfd]
%5 = alloca [64 x i8*]
%6 = bitcast [1 x %struct.pollfd]* %4 to i8*
%7 = bitcast [64 x i8*]* %5 to i8*
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.3, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str.4, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %3
%14 = tail call i32 (...) @beep_fd_open()
%15 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%16 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
%19 = getelementptr inbounds [64 x i8*], [64 x i8*]* %5, i64 0, i64 0
%20 = getelementptr inbounds i8*, i8** %1, i64 1
br label %21
21: ; preds = %40, %13
%22 = call i32 @memset(i8* %6, i32 0, i32 12)
store i32 %14, i32* %16
%23 = load i32, i32* @POLLIN
store i32 %23, i32* %17
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %14, i32 0, i32 %24)
%26 = call i32 @poll(%struct.pollfd* %15, i32 2, i32 3000)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret i32 -1
30: ; preds = %21
%31 = load i32, i32* %18
%32 = load i32, i32* @POLLIN
%33 = and i32 %32, %31
%34 = icmp eq i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %30
%36 = load i32, i32* %16
%37 = call i32 @read(i32 %36, i8** %19, i32 64)
%38 = load i8*, i8** %20
%39 = call i32 @system(i8* %38)
br label %40
40: ; preds = %35, %30
%41 = load i32, i32* @stdout
%42 = call i32 @fflush(i32 %41)
br label %21
}