#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 timespec {
  int member_1;
  int tv_sec;
  int tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
char EOF;
int /*<<< orphan*/ KDMKTONE;
int /*<<< orphan*/ O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int floor(int);
char getchar();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ isspace(char);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  struct timespec delay = {0, 50000000};
  bool sound = false;
  int console;
  char character;
  if (argc >= 3 && !strcmp(argv[2], "sound")) {
    sound = true;
  }
  if (argc >= 2) {
    if (!strcmp(argv[1], "sound")) {
      sound = true;
    } else {
      int temp_delay = atoi(argv[1]);
      delay.tv_sec = floor(temp_delay / 1000);
      temp_delay -= delay.tv_sec * 1000;
      delay.tv_nsec = temp_delay * 1000000;
    }
  }
  if (sound) {
    console = open("/dev/tty0", O_WRONLY);
  }
  while (character = getchar(), character != EOF) {
    putchar(character);
    fflush(stdout);
    if (!isspace(character) && sound) {
      ioctl(console, KDMKTONE, 339612);
    }
    nanosleep(&delay, NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe43(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe40(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     1201 <main+0x61>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe24(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1201 <main+0x61>
movl   $0x1,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1277 <main+0xd7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf6(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1234 <main+0x94>
movl   $0x1,-0x24(%rbp)
jmp    1272 <main+0xd2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   10c0 <floor@plt>
mov    %eax,-0x1c(%rbp)
imul   $0x3e8,-0x1c(%rbp),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
imul   $0xf4240,-0x30(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1277 <main+0xd7>
cmpl   $0x0,-0x24(%rbp)
je     1299 <main+0xf9>
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
lea    0xd85(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
jmp    129e <main+0xfe>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
mov    0x2d35(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1319 <main+0x179>
movsbl -0x29(%rbp),%edi
call   1030 <putchar@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
movsbl -0x29(%rbp),%edi
call   1060 <isspace@plt>
cmp    $0x0,%eax
jne    1307 <main+0x167>
cmpl   $0x0,-0x24(%rbp)
je     1307 <main+0x167>
mov    -0x28(%rbp),%edi
mov    0x2cdd(%rip),%rax
mov    (%rax),%esi
mov    $0x52e9c,%edx
call   1050 <ioctl@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
jmp    129e <main+0xfe>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
movups 0xe48(%rip),%xmm0
movaps %xmm0,(%rsp)
cmp    $0x3,%edi
jl     11da <main+0x3a>
mov    0x10(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1070 <strcmp@plt>
xor    %ebx,%ebx
test   %eax,%eax
sete   %bl
jmp    11e1 <main+0x41>
xor    %ebx,%ebx
cmp    $0x2,%edi
jne    1254 <main+0xb4>
mov    0x8(%rbp),%rbp
lea    0xe24(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1236 <main+0x96>
mov    %rbp,%rdi
call   10b0 <atoi@plt>
movslq %eax,%rbp
imul   $0x10624dd3,%rbp,%rdi
mov    %rdi,%rax
shr    $0x3f,%rax
sar    $0x26,%rdi
add    %eax,%edi
call   10c0 <floor@plt>
mov    %eax,0x4(%rsp)
imul   $0xfffffc18,%eax,%eax
add    %eax,%ebp
imul   $0xf4240,%ebp,%eax
mov    %eax,0x8(%rsp)
test   %ebx,%ebx
je     1252 <main+0xb2>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
lea    0xdd0(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%r14d
mov    $0x1,%bl
jmp    1254 <main+0xb4>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <getchar@plt>
mov    0x2d86(%rip),%r12
cmp    (%r12),%al
jne    1279 <main+0xd9>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d50(%rip),%r13
xor    $0x1,%bl
mov    %rsp,%r15
jmp    12a7 <main+0x107>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
cmp    (%r12),%al
je     1268 <main+0xc8>
movsbl %al,%ebp
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    0x0(%r13),%edi
call   1090 <fflush@plt>
mov    %ebp,%edi
call   1060 <isspace@plt>
test   %eax,%eax
setne  %al
or     %bl,%al
jne    1290 <main+0xf0>
mov    0x2d07(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    $0x52e9c,%edx
call   1050 <ioctl@plt>
jmp    1290 <main+0xf0>

LLVM IR (-O0):
%struct.timespec = type { i32, i32, i32, i32 }
@__const.main.delay = %struct.timespec { i32 0, i32 50000000, i32 0, i32 0 }
@.str = [6 x i8] c"sound\00"
@.str.1 = [10 x i8] c"/dev/tty0\00"
@O_WRONLY = global i32 0
@EOF = global i8 0
@stdout = global i32 0
@KDMKTONE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast %struct.timespec* %6 to i8*
store i32 0, i32* %7
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 3
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %14
store i32 1, i32* %7
br label %21
21: ; preds = %20, %14, %2
%22 = load i32, i32* %4
%23 = icmp sge i32 %22, 2
br i1 %23, label %24, label %49
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @strcmp(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %24
store i32 1, i32* %7
br label %48
31: ; preds = %24
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = sdiv i32 %36, 1000
%38 = call i32 @floor(i32 %37)
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%41 = load i32, i32* %40
%42 = mul nsw i32 %41, 1000
%43 = load i32, i32* %10
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = mul nsw i32 %45, 1000000
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 2
store i32 %46, i32* %47
br label %48
48: ; preds = %31, %30
br label %49
49: ; preds = %48, %21
%50 = load i32, i32* %7
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32, i32* @O_WRONLY
%54 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %53)
store i32 %54, i32* %8
br label %55
55: ; preds = %52, %49
br label %56
56: ; preds = %78, %55
%57 = call signext i8 (...) @getchar()
store i8 %57, i8* %9
%58 = load i8, i8* %9
%59 = sext i8 %58 to i32
%60 = load i8, i8* @EOF
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %59, %61
br i1 %62, label %63, label %80
63: ; preds = %56
%64 = load i8, i8* %9
%65 = call i32 @putchar(i8 signext %64)
%66 = load i32, i32* @stdout
%67 = call i32 @fflush(i32 %66)
%68 = load i8, i8* %9
%69 = call i32 @isspace(i8 signext %68)
%70 = icmp ne i32 %69, 0
br i1 %70, label %78, label %71
71: ; preds = %63
%72 = load i32, i32* %7
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %71
%75 = load i32, i32* %8
%76 = load i32, i32* @KDMKTONE
%77 = call i32 @ioctl(i32 %75, i32 %76, i32 339612)
br label %78
78: ; preds = %74, %71, %63
%79 = call i32 @nanosleep(%struct.timespec* %6, i32* null)
br label %56
80: ; preds = %56
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32, i32, i32 }
@__const.main.delay = %struct.timespec { i32 0, i32 50000000, i32 0, i32 0 }
@.str = [6 x i8] c"sound\00"
@.str.1 = [10 x i8] c"/dev/tty0\00"
@O_WRONLY = global i32 0
@EOF = global i8 0
@stdout = global i32 0
@KDMKTONE = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = icmp sgt i32 %0, 2
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
%11 = zext i1 %10 to i32
br label %14
12: ; preds = %2
%13 = icmp eq i32 %0, 2
br i1 %13, label %14, label %33
14: ; preds = %6, %12
%15 = phi i32 [ %11, %6 ], [ 0, %12 ]
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @strcmp(i8* dereferenceable(1) %17, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32 %18, 0
br i1 %19, label %30, label %20
20: ; preds = %14
%21 = tail call i32 @atoi(i8* %17)
%22 = sdiv i32 %21, 1000
%23 = tail call i32 @floor(i32 %22)
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = mul i32 %23, -1000
%26 = add i32 %25, %21
%27 = mul nsw i32 %26, 1000000
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = icmp eq i32 %15, 0
br i1 %29, label %33, label %30
30: ; preds = %14, %20
%31 = load i32, i32* @O_WRONLY
%32 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %12, %30, %20
%34 = phi i1 [ true, %30 ], [ false, %20 ], [ false, %12 ]
%35 = phi i32 [ %32, %30 ], [ undef, %20 ], [ undef, %12 ]
%36 = call signext i8 (...) @getchar()
%37 = load i8, i8* @EOF
%38 = icmp eq i8 %36, %37
br i1 %38, label %55, label %39
39: ; preds = %33, %50
%40 = phi i8 [ %52, %50 ], [ %36, %33 ]
%41 = call i32 @putchar(i8 signext %40)
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
%44 = call i32 @isspace(i8 signext %40)
%45 = icmp eq i32 %44, 0
%46 = and i1 %34, %45
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = load i32, i32* @KDMKTONE
%49 = call i32 @ioctl(i32 %35, i32 %48, i32 339612)
br label %50
50: ; preds = %47, %39
%51 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%52 = call signext i8 (...) @getchar()
%53 = load i8, i8* @EOF
%54 = icmp eq i8 %52, %53
br i1 %54, label %55, label %39
55: ; preds = %50, %33
ret i32 0
}