#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 AFMT_S16_NE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SNDCTL_DSP_CHANNELS;
int /*<<< orphan*/ SNDCTL_DSP_SETFMT;
int /*<<< orphan*/ SNDCTL_DSP_SPEED;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int dsp_open(char *dspname, int samplerate) {
  int dev;
  int arg;
  dev = open(dspname, O_RDWR);
  if (dev < 0) {
    perror("dsp_open: fopen");
    return -1;
  }
  arg = AFMT_S16_NE;
  if (ioctl(dev, SNDCTL_DSP_SETFMT, &arg) == -1) {
    perror("dsp_open: ioctl: SNDCTL_DSP_SETFMT");
    return -1;
  }
  if (arg != AFMT_S16_NE) {
    fputs("dsp_open: Cannot set sample format\n", stderr);
    return -1;
  }
  arg = 2;
  if (ioctl(dev, SNDCTL_DSP_CHANNELS, &arg) == -1) {
    perror("dsp_open: ioctl: SNDCTL_DSP_CHANNELS");
    return -1;
  }
  if (arg != 2) {
    fputs("dsp_open: Cannot set number of channels\n", stderr);
    return -1;
  }
  arg = samplerate;
  if (ioctl(dev, SNDCTL_DSP_SPEED, &arg) == -1) {
    perror("dsp_open: ioctl: SNDCTL_DSP_SPEED");
    return -1;
  }
  if (arg != samplerate) {
    fputs("dsp_open: Cannot set sample rate\n", stderr);
    return -1;
  }
  return dev;
}


x86 assembly (clang, -O0):
<dsp_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1186 <dsp_open+0x46>
lea    0xe8b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <dsp_open+0x192>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11c8 <dsp_open+0x88>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <dsp_open+0x192>
mov    -0x1c(%rbp),%eax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%eax
je     11fb <dsp_open+0xbb>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1030 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <dsp_open+0x192>
movl   $0x2,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1238 <dsp_open+0xf8>
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <dsp_open+0x192>
cmpl   $0x2,-0x1c(%rbp)
je     1263 <dsp_open+0x123>
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1030 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <dsp_open+0x192>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    129f <dsp_open+0x15f>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <dsp_open+0x192>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12cc <dsp_open+0x18c>
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
lea    0xe0c(%rip),%rdi
call   1030 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <dsp_open+0x192>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dsp_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1234 <dsp_open+0xf4>
mov    %eax,%ebx
mov    0x2e76(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1242 <dsp_open+0x102>
mov    0xc(%rsp),%eax
cmp    0x0(%rbp),%eax
jne    11fd <dsp_open+0xbd>
movl   $0x2,0xc(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1250 <dsp_open+0x110>
cmpl   $0x2,0xc(%rsp)
jne    120f <dsp_open+0xcf>
mov    %r14d,0xc(%rsp)
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     125e <dsp_open+0x11e>
cmp    %r14d,0xc(%rsp)
je     1229 <dsp_open+0xe9>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xecc(%rip),%rdi
jmp    121f <dsp_open+0xdf>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
jmp    121f <dsp_open+0xdf>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1030 <fputs@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc5(%rip),%rdi
call   1060 <perror@plt>
jmp    1224 <dsp_open+0xe4>
lea    0xdc7(%rip),%rdi
call   1060 <perror@plt>
jmp    1224 <dsp_open+0xe4>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
jmp    1224 <dsp_open+0xe4>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
jmp    1224 <dsp_open+0xe4>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [16 x i8] c"dsp_open: fopen\00"
@AFMT_S16_NE = global i32 0
@SNDCTL_DSP_SETFMT = global i32 0
@.str.1 = [35 x i8] c"dsp_open: ioctl: SNDCTL_DSP_SETFMT\00"
@.str.2 = [36 x i8] c"dsp_open: Cannot set sample format\0A\00"
@stderr = global i32 0
@SNDCTL_DSP_CHANNELS = global i32 0
@.str.3 = [37 x i8] c"dsp_open: ioctl: SNDCTL_DSP_CHANNELS\00"
@.str.4 = [41 x i8] c"dsp_open: Cannot set number of channels\0A\00"
@SNDCTL_DSP_SPEED = global i32 0
@.str.5 = [34 x i8] c"dsp_open: ioctl: SNDCTL_DSP_SPEED\00"
@.str.6 = [34 x i8] c"dsp_open: Cannot set sample rate\0A\00"
define i32 @dsp_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
15: ; preds = %2
%16 = load i32, i32* @AFMT_S16_NE
store i32 %16, i32* %7
%17 = load i32, i32* %6
%18 = load i32, i32* @SNDCTL_DSP_SETFMT
%19 = call i32 @ioctl(i32 %17, i32 %18, i32* %7)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
23: ; preds = %15
%24 = load i32, i32* %7
%25 = load i32, i32* @AFMT_S16_NE
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fputs(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 -1, i32* %3
br label %60
30: ; preds = %23
store i32 2, i32* %7
%31 = load i32, i32* %6
%32 = load i32, i32* @SNDCTL_DSP_CHANNELS
%33 = call i32 @ioctl(i32 %31, i32 %32, i32* %7)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
37: ; preds = %30
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 2
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %41)
store i32 -1, i32* %3
br label %60
43: ; preds = %37
%44 = load i32, i32* %5
store i32 %44, i32* %7
%45 = load i32, i32* %6
%46 = load i32, i32* @SNDCTL_DSP_SPEED
%47 = call i32 @ioctl(i32 %45, i32 %46, i32* %7)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
51: ; preds = %43
%52 = load i32, i32* %7
%53 = load i32, i32* %5
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = call i32 @fputs(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %56)
store i32 -1, i32* %3
br label %60
58: ; preds = %51
%59 = load i32, i32* %6
store i32 %59, i32* %3
br label %60
60: ; preds = %58, %55, %49, %40, %35, %27, %21, %13
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [16 x i8] c"dsp_open: fopen\00"
@AFMT_S16_NE = global i32 0
@SNDCTL_DSP_SETFMT = global i32 0
@.str.1 = [35 x i8] c"dsp_open: ioctl: SNDCTL_DSP_SETFMT\00"
@.str.2 = [36 x i8] c"dsp_open: Cannot set sample format\0A\00"
@stderr = global i32 0
@SNDCTL_DSP_CHANNELS = global i32 0
@.str.3 = [37 x i8] c"dsp_open: ioctl: SNDCTL_DSP_CHANNELS\00"
@.str.4 = [41 x i8] c"dsp_open: Cannot set number of channels\0A\00"
@SNDCTL_DSP_SPEED = global i32 0
@.str.5 = [34 x i8] c"dsp_open: ioctl: SNDCTL_DSP_SPEED\00"
@.str.6 = [34 x i8] c"dsp_open: Cannot set sample rate\0A\00"
define i32 @dsp_open(i8* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %48
10: ; preds = %2
%11 = load i32, i32* @AFMT_S16_NE
store i32 %11, i32* %3
%12 = load i32, i32* @SNDCTL_DSP_SETFMT
%13 = call i32 @ioctl(i32 %6, i32 %12, i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %48
17: ; preds = %10
%18 = load i32, i32* %3
%19 = load i32, i32* @AFMT_S16_NE
%20 = icmp eq i32 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 @fputs(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %48
24: ; preds = %17
store i32 2, i32* %3
%25 = load i32, i32* @SNDCTL_DSP_CHANNELS
%26 = call i32 @ioctl(i32 %6, i32 %25, i32* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %48
30: ; preds = %24
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, 2
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %48
36: ; preds = %30
store i32 %1, i32* %3
%37 = load i32, i32* @SNDCTL_DSP_SPEED
%38 = call i32 @ioctl(i32 %6, i32 %37, i32* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
br label %48
42: ; preds = %36
%43 = load i32, i32* %3
%44 = icmp eq i32 %43, %1
br i1 %44, label %48, label %45
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 @fputs(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %42, %45, %40, %33, %28, %21, %15, %8
%49 = phi i32 [ -1, %8 ], [ -1, %15 ], [ -1, %21 ], [ -1, %28 ], [ -1, %33 ], [ -1, %40 ], [ -1, %45 ], [ %6, %42 ]
ret i32 %49
}