#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;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ ev;

/* Variables and functions */
int /*<<< orphan*/ EV_SND;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SND_TONE;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, struct input_event *, int);
void ring_bell(int freq) {
  ssize_t s;
  struct input_event ev;
  int fd = open("/dev/input/beeper", O_WRONLY);
  ev.type = EV_SND;
  ev.code = SND_TONE;
  ev.value = freq;
  s = write(fd, &ev, sizeof(ev));
  if (s == -1) {
    perror("Error writing bell event.");
    return;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<ring_bell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b0 <ring_bell+0x70>
lea    0xe6c(%rip),%rdi
call   1060 <perror@plt>
jmp    11b8 <ring_bell+0x78>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ring_bell>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11a0 <ring_bell+0x60>
mov    %ebx,%edi
add    $0x18,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
lea    0xe6b(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <perror@plt>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@.str = [18 x i8] c"/dev/input/beeper\00"
@O_WRONLY = global i32 0
@EV_SND = global i32 0
@SND_TONE = global i32 0
@.str.1 = [26 x i8] c"Error writing bell event.\00"
define void @ring_bell(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.input_event
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = load i32, i32* @EV_SND
%9 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @SND_TONE
%11 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* %2
%13 = getelementptr inbounds %struct.input_event, %struct.input_event* %4, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %5
%15 = call i32 @write(i32 %14, %struct.input_event* %4, i32 12)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %23
20: ; preds = %1
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %20, %18
ret void
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32 }
@.str = [18 x i8] c"/dev/input/beeper\00"
@O_WRONLY = global i32 0
@EV_SND = global i32 0
@SND_TONE = global i32 0
@.str.1 = [26 x i8] c"Error writing bell event.\00"
define void @ring_bell(i32 %0) {
%2 = alloca %struct.input_event
%3 = bitcast %struct.input_event* %2 to i8*
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @EV_SND
%7 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @SND_TONE
%9 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.input_event, %struct.input_event* %2, i64 0, i32 0
store i32 %0, i32* %10
%11 = call i32 @write(i32 %5, %struct.input_event* %2, i32 12)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %17
15: ; preds = %1
%16 = tail call i32 @close(i32 %5)
br label %17
17: ; preds = %15, %13
ret void
}