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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SNDCTL_DSP_SETFRAGMENT;
int /*<<< orphan*/ SOUND_PCM_RESET;
int /*<<< orphan*/ SOUND_PCM_SYNC;
int /*<<< orphan*/ SOUND_PCM_WRITE_BITS;
int /*<<< orphan*/ SOUND_PCM_WRITE_CHANNELS;
int /*<<< orphan*/ SOUND_PCM_WRITE_RATE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int init_soundcard(long sample_rate, int sample_size, int channels) {
  char *devname = "/dev/dsp";
  int fd;
  int parm;
  int sr;
  ;
  if ((fd = open(devname, O_RDONLY)) < 0) {
    fprintf(stderr, "Could not open device '%s'; %s\n", devname,
            strerror(errno));
    exit(1);
  }
  parm = 0x0100008;
  if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &parm) >= 0)
    ;
  else
    ;
  ;
  if (ioctl(fd, SOUND_PCM_RESET, 0) >= 0)
    ;
  else
    ;
  ;
  if (ioctl(fd, SOUND_PCM_SYNC, 0) >= 0)
    ;
  else
    ;
  ;
  if (ioctl(fd, SOUND_PCM_WRITE_BITS, &sample_size) >= 0)
    ;
  else
    ;
  ;
  if (ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &channels) >= 0)
    ;
  else
    ;
  if ((sample_size != 16) && (sample_size != 8)) {
    fprintf(stderr, "DSP error: returned a sample size of %d bits.\n",
            sample_size);
    exit(1);
  }
  sr = sample_rate;
  ;
  ioctl(fd, SOUND_PCM_SYNC, 0);
  ;
  ioctl(fd, SOUND_PCM_WRITE_RATE, &sr);
  sample_rate = sr;
  if (sample_rate < 1000) {
    printf("DSP error: returned a sample rate of %ld samples/second.\n",
           sample_rate);
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<init_soundcard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11df <init_soundcard+0x7f>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x100008,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     120a <init_soundcard+0xaa>
jmp    120f <init_soundcard+0xaf>
jmp    120f <init_soundcard+0xaf>
mov    -0x1c(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     1233 <init_soundcard+0xd3>
jmp    1238 <init_soundcard+0xd8>
jmp    1238 <init_soundcard+0xd8>
mov    -0x1c(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     125c <init_soundcard+0xfc>
jmp    1261 <init_soundcard+0x101>
jmp    1261 <init_soundcard+0x101>
mov    -0x1c(%rbp),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     1285 <init_soundcard+0x125>
jmp    128a <init_soundcard+0x12a>
jmp    128a <init_soundcard+0x12a>
mov    -0x1c(%rbp),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     12ae <init_soundcard+0x14e>
jmp    12b3 <init_soundcard+0x153>
jmp    12b3 <init_soundcard+0x153>
cmpl   $0x10,-0xc(%rbp)
je     12eb <init_soundcard+0x18b>
cmpl   $0x8,-0xc(%rbp)
je     12eb <init_soundcard+0x18b>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xd4f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1040 <ioctl@plt>
movslq -0x24(%rbp),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x3e8,-0x8(%rbp)
jge    134c <init_soundcard+0x1ec>
mov    -0x8(%rbp),%rsi
lea    0xd1b(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_soundcard>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    %esi,0x4(%rsp)
mov    %edx,0xc(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     124d <init_soundcard+0xed>
mov    %eax,%ebx
movl   $0x100008,0x8(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    0x2e07(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    0x4(%rsp),%edx
cmp    $0x8,%edx
je     1211 <init_soundcard+0xb1>
cmp    $0x10,%edx
jne    129e <init_soundcard+0x13e>
mov    %r14d,(%rsp)
mov    (%r15),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
movslq (%rsp),%rsi
cmp    $0x3e7,%rsi
jle    1288 <init_soundcard+0x128>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d64(%rip),%rax
mov    (%rax),%ebx
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd9e(%rip),%rsi
lea    0xd8e(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdc9(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/dsp\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Could not open device '%s'; %s\0A\00"
@errno = global i32 0
@SNDCTL_DSP_SETFRAGMENT = global i32 0
@SOUND_PCM_RESET = global i32 0
@SOUND_PCM_SYNC = global i32 0
@SOUND_PCM_WRITE_BITS = global i32 0
@SOUND_PCM_WRITE_CHANNELS = global i32 0
@.str.2 = [47 x i8] c"DSP error: returned a sample size of %d bits.\0A\00"
@SOUND_PCM_WRITE_RATE = global i32 0
@.str.3 = [58 x i8] c"DSP error: returned a sample rate of %ld samples/second.\0A\00"
define i32 @init_soundcard(i64 %0, i32 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i8*, i8** %7
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %7
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
store i32 1048584, i32* %9
%23 = load i32, i32* %8
%24 = load i32, i32* @SNDCTL_DSP_SETFRAGMENT
%25 = call i64 @ioctl(i32 %23, i32 %24, i32* %9)
%26 = icmp sge i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
br label %29
28: ; preds = %22
br label %29
29: ; preds = %28, %27
%30 = load i32, i32* %8
%31 = load i32, i32* @SOUND_PCM_RESET
%32 = call i64 @ioctl(i32 %30, i32 %31, i32* null)
%33 = icmp sge i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
br label %36
35: ; preds = %29
br label %36
36: ; preds = %35, %34
%37 = load i32, i32* %8
%38 = load i32, i32* @SOUND_PCM_SYNC
%39 = call i64 @ioctl(i32 %37, i32 %38, i32* null)
%40 = icmp sge i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
br label %43
42: ; preds = %36
br label %43
43: ; preds = %42, %41
%44 = load i32, i32* %8
%45 = load i32, i32* @SOUND_PCM_WRITE_BITS
%46 = call i64 @ioctl(i32 %44, i32 %45, i32* %5)
%47 = icmp sge i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %50
49: ; preds = %43
br label %50
50: ; preds = %49, %48
%51 = load i32, i32* %8
%52 = load i32, i32* @SOUND_PCM_WRITE_CHANNELS
%53 = call i64 @ioctl(i32 %51, i32 %52, i32* %6)
%54 = icmp sge i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %57
56: ; preds = %50
br label %57
57: ; preds = %56, %55
%58 = load i32, i32* %5
%59 = icmp ne i32 %58, 16
br i1 %59, label %60, label %68
60: ; preds = %57
%61 = load i32, i32* %5
%62 = icmp ne i32 %61, 8
br i1 %62, label %63, label %68
63: ; preds = %60
%64 = load i32, i32* @stderr
%65 = load i32, i32* %5
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %60, %57
%69 = load i64, i64* %4
%70 = trunc i64 %69 to i32
store i32 %70, i32* %10
%71 = load i32, i32* %8
%72 = load i32, i32* @SOUND_PCM_SYNC
%73 = call i64 @ioctl(i32 %71, i32 %72, i32* null)
%74 = load i32, i32* %8
%75 = load i32, i32* @SOUND_PCM_WRITE_RATE
%76 = call i64 @ioctl(i32 %74, i32 %75, i32* %10)
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
store i64 %78, i64* %4
%79 = load i64, i64* %4
%80 = icmp slt i64 %79, 1000
br i1 %80, label %81, label %85
81: ; preds = %68
%82 = load i64, i64* %4
%83 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i64 %82)
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %68
%86 = load i32, i32* %8
ret i32 %86
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/dsp\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Could not open device '%s'; %s\0A\00"
@errno = global i32 0
@SNDCTL_DSP_SETFRAGMENT = global i32 0
@SOUND_PCM_RESET = global i32 0
@SOUND_PCM_SYNC = global i32 0
@SOUND_PCM_WRITE_BITS = global i32 0
@SOUND_PCM_WRITE_CHANNELS = global i32 0
@.str.2 = [47 x i8] c"DSP error: returned a sample size of %d bits.\0A\00"
@SOUND_PCM_WRITE_RATE = global i32 0
@.str.3 = [58 x i8] c"DSP error: returned a sample rate of %ld samples/second.\0A\00"
define i32 @init_soundcard(i64 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %1, i32* %4
store i32 %2, i32* %5
%8 = bitcast i32* %6 to i8*
%9 = bitcast i32* %7 to i8*
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %3
store i32 1048584, i32* %6
%20 = load i32, i32* @SNDCTL_DSP_SETFRAGMENT
%21 = call i64 @ioctl(i32 %11, i32 %20, i32* %6)
%22 = load i32, i32* @SOUND_PCM_RESET
%23 = call i64 @ioctl(i32 %11, i32 %22, i32* null)
%24 = load i32, i32* @SOUND_PCM_SYNC
%25 = call i64 @ioctl(i32 %11, i32 %24, i32* null)
%26 = load i32, i32* @SOUND_PCM_WRITE_BITS
%27 = call i64 @ioctl(i32 %11, i32 %26, i32* %4)
%28 = load i32, i32* @SOUND_PCM_WRITE_CHANNELS
%29 = call i64 @ioctl(i32 %11, i32 %28, i32* %5)
%30 = load i32, i32* %4
switch i32 %30, label %31 [
i32 16, label %35
i32 8, label %35
]
31: ; preds = %19
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %19, %19
%36 = trunc i64 %0 to i32
store i32 %36, i32* %7
%37 = load i32, i32* @SOUND_PCM_SYNC
%38 = call i64 @ioctl(i32 %11, i32 %37, i32* null)
%39 = load i32, i32* @SOUND_PCM_WRITE_RATE
%40 = call i64 @ioctl(i32 %11, i32 %39, i32* %7)
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 1000
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = sext i32 %41 to i64
%45 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i64 %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %35
ret i32 %11
}