#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*/ buffer;

/* Variables and functions */
int AFMT_S16_LE;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SNDCTL_DSP_CHANNELS;
int /*<<< orphan*/ SNDCTL_DSP_SETFMT;
int /*<<< orphan*/ SNDCTL_DSP_SETFRAGMENT;
int /*<<< orphan*/ SNDCTL_DSP_SPEED;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, unsigned char *, int);
void main(void) {
  int fd, i, r;
  unsigned char buffer[256 * 1024];
  memset(buffer, 0x80, sizeof(buffer));
  fd = open("/dev/dsp", O_WRONLY);
  if (fd < 0) {
    perror("open (playback)");
    return;
  }
  i = AFMT_S16_LE;
  if ((r = ioctl(fd, SNDCTL_DSP_SETFMT, &i)) < 0) {
    perror("setfmt");
    return;
  }
  printf("afmt = %i, result = %i (%i)\n", AFMT_S16_LE, i, r);
  i = 1;
  if ((r = ioctl(fd, SNDCTL_DSP_CHANNELS, &i)) < 0) {
    perror("channels");
    return;
  }
  printf("channels = 1, result = %i (%i)\n", i, r);
  i = 22050;
  if ((r = ioctl(fd, SNDCTL_DSP_SPEED, &i)) < 0) {
    perror("rate");
    return;
  }
  printf("rate = 22050, result = %i (%i)\n", i, r);
  i = 0x0008;
  if ((r = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &i)) < 0) {
    perror("setfragment");
    return;
  }
  printf("fragment = 0x20008, result = 0x%x (%i)\n", i, r);
  printf("write - begin\n");
  while (1) {
    write(fd, buffer, 1024);
    usleep(10000);
  }
  printf("write - end\n");
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40010,%rsp
lea    -0x40010(%rbp),%rdi
mov    $0x80,%esi
mov    $0x40000,%edx
call   1050 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11c4 <main+0x54>
lea    0xe4f(%rip),%rdi
call   1080 <perror@plt>
jmp    1337 <main+0x1c7>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1202 <main+0x92>
lea    0xe21(%rip),%rdi
call   1080 <perror@plt>
jmp    1337 <main+0x1c7>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1258 <main+0xe8>
lea    0xdef(%rip),%rdi
call   1080 <perror@plt>
jmp    1337 <main+0x1c7>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5622,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    12a5 <main+0x135>
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
jmp    1337 <main+0x1c7>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x8,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    12f2 <main+0x182>
lea    0xda3(%rip),%rdi
call   1080 <perror@plt>
jmp    1337 <main+0x1c7>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x40010(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    $0x2710,%edi
call   1090 <usleep@plt>
jmp    1314 <main+0x1a4>
add    $0x40010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x40018,%rsp
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    $0x40000,%edx
call   1060 <memset@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12db <main+0x15b>
mov    %eax,%ebp
mov    0x2e1d(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
js     12e4 <main+0x164>
mov    (%rbx),%esi
mov    0xc(%rsp),%edx
lea    0xe2d(%rip),%rdi
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x1,0xc(%rsp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
js     12ed <main+0x16d>
mov    0xc(%rsp),%esi
lea    0xe1a(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x5622,0xc(%rsp)
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
js     12f6 <main+0x176>
mov    0xc(%rsp),%esi
lea    0xe06(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x8,0xc(%rsp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
js     12ff <main+0x17f>
mov    0xc(%rsp),%esi
lea    0xdfd(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    $0x2710,%edi
call   10a0 <usleep@plt>
jmp    12c0 <main+0x140>
lea    0xd27(%rip),%rdi
jmp    1306 <main+0x186>
lea    0xd2e(%rip),%rdi
jmp    1306 <main+0x186>
lea    0xd49(%rip),%rdi
jmp    1306 <main+0x186>
lea    0xd69(%rip),%rdi
jmp    1306 <main+0x186>
lea    0xd85(%rip),%rdi
call   1090 <perror@plt>
add    $0x40018,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/dsp\00"
@O_WRONLY = global i32 0
@.str.1 = [16 x i8] c"open (playback)\00"
@AFMT_S16_LE = global i32 0
@SNDCTL_DSP_SETFMT = global i32 0
@.str.2 = [7 x i8] c"setfmt\00"
@.str.3 = [29 x i8] c"afmt = %i, result = %i (%i)\0A\00"
@SNDCTL_DSP_CHANNELS = global i32 0
@.str.4 = [9 x i8] c"channels\00"
@.str.5 = [32 x i8] c"channels = 1, result = %i (%i)\0A\00"
@SNDCTL_DSP_SPEED = global i32 0
@.str.6 = [5 x i8] c"rate\00"
@.str.7 = [32 x i8] c"rate = 22050, result = %i (%i)\0A\00"
@SNDCTL_DSP_SETFRAGMENT = global i32 0
@.str.8 = [12 x i8] c"setfragment\00"
@.str.9 = [40 x i8] c"fragment = 0x20008, result = 0x%x (%i)\0A\00"
@.str.10 = [15 x i8] c"write - begin\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [262144 x i8]
%5 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 128, i32 262144)
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %62
13: ; preds = %0
%14 = load i32, i32* @AFMT_S16_LE
store i32 %14, i32* %2
%15 = load i32, i32* %1
%16 = load i32, i32* @SNDCTL_DSP_SETFMT
%17 = call i32 @ioctl(i32 %15, i32 %16, i32* %2)
store i32 %17, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %62
21: ; preds = %13
%22 = load i32, i32* @AFMT_S16_LE
%23 = load i32, i32* %2
%24 = load i32, i32* %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %22, i32 %23, i32 %24)
store i32 1, i32* %2
%26 = load i32, i32* %1
%27 = load i32, i32* @SNDCTL_DSP_CHANNELS
%28 = call i32 @ioctl(i32 %26, i32 %27, i32* %2)
store i32 %28, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %62
32: ; preds = %21
%33 = load i32, i32* %2
%34 = load i32, i32* %3
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %33, i32 %34)
store i32 22050, i32* %2
%36 = load i32, i32* %1
%37 = load i32, i32* @SNDCTL_DSP_SPEED
%38 = call i32 @ioctl(i32 %36, i32 %37, i32* %2)
store i32 %38, i32* %3
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %62
42: ; preds = %32
%43 = load i32, i32* %2
%44 = load i32, i32* %3
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %43, i32 %44)
store i32 8, i32* %2
%46 = load i32, i32* %1
%47 = load i32, i32* @SNDCTL_DSP_SETFRAGMENT
%48 = call i32 @ioctl(i32 %46, i32 %47, i32* %2)
store i32 %48, i32* %3
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %62
52: ; preds = %42
%53 = load i32, i32* %2
%54 = load i32, i32* %3
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.9, i64 0, i64 0), i32 %53, i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
br label %57
57: ; preds = %52, %57
%58 = load i32, i32* %1
%59 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%60 = call i32 @write(i32 %58, i8* %59, i32 1024)
%61 = call i32 @usleep(i32 10000)
br label %57
62: ; preds = %50, %40, %30, %19, %11
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/dsp\00"
@O_WRONLY = global i32 0
@.str.1 = [16 x i8] c"open (playback)\00"
@AFMT_S16_LE = global i32 0
@SNDCTL_DSP_SETFMT = global i32 0
@.str.2 = [7 x i8] c"setfmt\00"
@.str.3 = [29 x i8] c"afmt = %i, result = %i (%i)\0A\00"
@SNDCTL_DSP_CHANNELS = global i32 0
@.str.4 = [9 x i8] c"channels\00"
@.str.5 = [32 x i8] c"channels = 1, result = %i (%i)\0A\00"
@SNDCTL_DSP_SPEED = global i32 0
@.str.6 = [5 x i8] c"rate\00"
@.str.7 = [32 x i8] c"rate = 22050, result = %i (%i)\0A\00"
@SNDCTL_DSP_SETFRAGMENT = global i32 0
@.str.8 = [12 x i8] c"setfragment\00"
@.str.9 = [40 x i8] c"fragment = 0x20008, result = 0x%x (%i)\0A\00"
@str = [14 x i8] c"write - begin\00"
define void @main() {
%1 = alloca i32
%2 = alloca [262144 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [262144 x i8], [262144 x i8]* %2, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 128, i32 262144)
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %40, label %9
9: ; preds = %0
%10 = load i32, i32* @AFMT_S16_LE
store i32 %10, i32* %1
%11 = load i32, i32* @SNDCTL_DSP_SETFMT
%12 = call i32 @ioctl(i32 %7, i32 %11, i32* %1)
%13 = icmp slt i32 %12, 0
br i1 %13, label %40, label %14
14: ; preds = %9
%15 = load i32, i32* @AFMT_S16_LE
%16 = load i32, i32* %1
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %16, i32 %12)
store i32 1, i32* %1
%18 = load i32, i32* @SNDCTL_DSP_CHANNELS
%19 = call i32 @ioctl(i32 %7, i32 %18, i32* %1)
%20 = icmp slt i32 %19, 0
br i1 %20, label %40, label %21
21: ; preds = %14
%22 = load i32, i32* %1
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 %19)
store i32 22050, i32* %1
%24 = load i32, i32* @SNDCTL_DSP_SPEED
%25 = call i32 @ioctl(i32 %7, i32 %24, i32* %1)
%26 = icmp slt i32 %25, 0
br i1 %26, label %40, label %27
27: ; preds = %21
%28 = load i32, i32* %1
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %28, i32 %25)
store i32 8, i32* %1
%30 = load i32, i32* @SNDCTL_DSP_SETFRAGMENT
%31 = call i32 @ioctl(i32 %7, i32 %30, i32* %1)
%32 = icmp slt i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %27
%34 = load i32, i32* %1
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.9, i64 0, i64 0), i32 %34, i32 %31)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %37
37: ; preds = %37, %33
%38 = call i32 @write(i32 %7, i8* %4, i32 1024)
%39 = call i32 @usleep(i32 10000)
br label %37
40: ; preds = %27, %21, %14, %9, %0
%41 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), %14 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), %21 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), %27 ]
%42 = call i32 @perror(i8* %41)
ret void
}