#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 input_event {
  int value;
  int /*<<< orphan*/ code;
  int /*<<< orphan*/ type;
};

/* Variables and functions */
int /*<<< orphan*/ EV_SND;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SND_TONE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write_event(int, struct input_event);
int main(int argc, char **argv) {
  int fd = -1;
  struct input_event ev;
  char def[] = "/dev/event1";
  char *fname;
  if (argc > 1)
    fname = argv[1];
  else
    fname = def;
  ev.type = EV_SND;
  ev.code = SND_TONE;
  fd = open(fname, O_WRONLY);
  if (fd == -1) {
    perror("open()");
    exit(1);
  }
  ev.value = 750;
  write_event(fd, ev);
  usleep(10000);
  ev.value = 0;
  write_event(fd, ev);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0xe7c(%rip),%rax
mov    %rax,-0x2c(%rbp)
mov    0xe7a(%rip),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ac <main+0x4c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11b4 <main+0x54>
lea    -0x2c(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1201 <main+0xa1>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x2ee,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   1040 <write_event@plt>
mov    $0x2710,%edi
call   1080 <usleep@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%edx
call   1040 <write_event@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x6576652f7665642f,%rax
65 76 65
mov    %rax,(%rsp)
movl   $0x31746e,0x8(%rsp)
cmp    $0x2,%edi
jl     1189 <main+0x29>
mov    0x8(%rsi),%rdi
jmp    118c <main+0x2c>
mov    %rsp,%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%r14d
mov    0x2e43(%rip),%rax
mov    (%rax),%ebx
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11f2 <main+0x92>
mov    %eax,%ebp
shl    $0x20,%rbx
lea    0x2ee(%rbx),%rsi
mov    %eax,%edi
mov    %r14d,%edx
call   1040 <write_event@plt>
mov    $0x2710,%edi
call   1080 <usleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <write_event@plt>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@__const.main.def = [12 x i8] c"/dev/event1\00"
@EV_SND = global i32 0
@SND_TONE = global i32 0
@O_WRONLY = global i32 0
@.str = [7 x i8] c"open()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.input_event
%8 = alloca [12 x i8]
%9 = alloca i8*
%10 = alloca { i64, i32 }
%11 = alloca { i64, i32 }
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%12 = bitcast [12 x i8]* %8 to i8*
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %9
br label %21
19: ; preds = %2
%20 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
store i8* %20, i8** %9
br label %21
21: ; preds = %19, %15
%22 = load i32, i32* @EV_SND
%23 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @SND_TONE
%25 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i8*, i8** %9
%27 = load i32, i32* @O_WRONLY
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
store i32 750, i32* %35
%36 = load i32, i32* %6
%37 = bitcast { i64, i32 }* %10 to i8*
%38 = bitcast %struct.input_event* %7 to i8*
%39 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %10, i32 0, i32 0
%40 = load i64, i64* %39
%41 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %10, i32 0, i32 1
%42 = load i32, i32* %41
%43 = call i32 @write_event(i32 %36, i64 %40, i32 %42)
%44 = call i32 @usleep(i32 10000)
%45 = getelementptr inbounds %struct.input_event, %struct.input_event* %7, i32 0, i32 0
store i32 0, i32* %45
%46 = load i32, i32* %6
%47 = bitcast { i64, i32 }* %11 to i8*
%48 = bitcast %struct.input_event* %7 to i8*
%49 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %11, i32 0, i32 0
%50 = load i64, i64* %49
%51 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %11, i32 0, i32 1
%52 = load i32, i32* %51
%53 = call i32 @write_event(i32 %46, i64 %50, i32 %52)
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
ret i32 0
}

LLVM IR (-O2):
@__const.main.def = [12 x i8] c"/dev/event1\00"
@EV_SND = global i32 0
@SND_TONE = global i32 0
@O_WRONLY = global i32 0
@.str = [7 x i8] c"open()\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [12 x i8]
%4 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ %4, %2 ]
%11 = load i32, i32* @EV_SND
%12 = load i32, i32* @SND_TONE
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = zext i32 %12 to i64
%21 = shl nuw i64 %20, 32
%22 = or i64 %21, 750
%23 = tail call i32 @write_event(i32 %14, i64 %22, i32 %11)
%24 = tail call i32 @usleep(i32 10000)
%25 = tail call i32 @write_event(i32 %14, i64 %21, i32 %11)
%26 = tail call i32 @close(i32 %14)
ret i32 0
}