#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_WRONLY;
int /*<<< orphan*/ SNDCTL_DSP_CHANNELS;
int /*<<< orphan*/ SNDCTL_DSP_RESET;
int /*<<< orphan*/ SNDCTL_DSP_SAMPLESIZE;
int /*<<< orphan*/ SNDCTL_DSP_SPEED;
int /*<<< orphan*/ SNDCTL_DSP_SYNC;
int /*<<< orphan*/ close(int);
char *getenv(char *);
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int aud_writeinit(int sample_rate, int sample_size, int channels) {
  int audio_fd, error;
  char *devicename;
  printf("requested chans=%d, sample rate=%d sample size=%d\n", channels,
         sample_rate, sample_size);
  if (NULL == (devicename = getenv("AUDIODEV")))
    devicename = "/dev/dsp";
  if ((audio_fd = open(devicename, O_WRONLY, 0)) < 0) {
    perror("setparams : open ");
    return -1;
  }
  if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) != 0) {
    perror("setparams : reset ");
    close(audio_fd);
    return -1;
  }
  if ((error = ioctl(audio_fd, SNDCTL_DSP_SAMPLESIZE, &sample_size)) != 0) {
    perror("setparams : bitwidth ");
    close(audio_fd);
    return -1;
  }
  if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels) != 0) {
    perror("setparams : channels ");
    close(audio_fd);
    return -1;
  }
  if ((error = ioctl(audio_fd, SNDCTL_DSP_SPEED, &sample_rate)) != 0) {
    perror("setparams : sample rate ");
    close(audio_fd);
    return -1;
  }
  if ((error = ioctl(audio_fd, SNDCTL_DSP_SYNC, 0)) != 0) {
    perror("setparams : sync ");
    close(audio_fd);
    return -1;
  }
  printf("set chans=%d, sample rate=%d sample size=%d\n", channels, sample_rate,
         sample_size);
  return audio_fd;
}


x86 assembly (clang, -O0):
<aud_writeinit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11af <aud_writeinit+0x4f>
lea    0xe91(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11e7 <aud_writeinit+0x87>
lea    0xe6f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <aud_writeinit+0x1e1>
mov    -0x14(%rbp),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%eax
je     1225 <aud_writeinit+0xc5>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <aud_writeinit+0x1e1>
mov    -0x14(%rbp),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1266 <aud_writeinit+0x106>
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <aud_writeinit+0x1e1>
mov    -0x14(%rbp),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%eax
je     12a4 <aud_writeinit+0x144>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <aud_writeinit+0x1e1>
mov    -0x14(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     12e5 <aud_writeinit+0x185>
lea    0xdca(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <aud_writeinit+0x1e1>
mov    -0x14(%rbp),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1326 <aud_writeinit+0x1c6>
lea    0xda2(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <aud_writeinit+0x1e1>
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xd8b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aud_writeinit>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ecx
mov    %edi,%eax
mov    %edi,0xc(%rsp)
mov    %esi,0x8(%rsp)
mov    %edx,0x4(%rsp)
lea    0xe84(%rip),%rdi
mov    %edx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
lea    0xea7(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xea1(%rip),%rdi
cmovne %rax,%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
test   %eax,%eax
js     124e <aud_writeinit+0xee>
mov    %eax,%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %eax,%eax
jne    125c <aud_writeinit+0xfc>
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
jne    1265 <aud_writeinit+0x105>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
jne    126e <aud_writeinit+0x10e>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
jne    1277 <aud_writeinit+0x117>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %eax,%eax
jne    1280 <aud_writeinit+0x120>
mov    0x4(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
jmp    1298 <aud_writeinit+0x138>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
jmp    1293 <aud_writeinit+0x133>
lea    0xdf4(%rip),%rdi
jmp    1287 <aud_writeinit+0x127>
lea    0xdfe(%rip),%rdi
jmp    1287 <aud_writeinit+0x127>
lea    0xe0b(%rip),%rdi
jmp    1287 <aud_writeinit+0x127>
lea    0xe18(%rip),%rdi
jmp    1287 <aud_writeinit+0x127>
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [51 x i8] c"requested chans=%d, sample rate=%d sample size=%d\0A\00"
@.str.1 = [9 x i8] c"AUDIODEV\00"
@.str.2 = [9 x i8] c"/dev/dsp\00"
@O_WRONLY = global i32 0
@.str.3 = [18 x i8] c"setparams : open \00"
@SNDCTL_DSP_RESET = global i32 0
@.str.4 = [19 x i8] c"setparams : reset \00"
@SNDCTL_DSP_SAMPLESIZE = global i32 0
@.str.5 = [22 x i8] c"setparams : bitwidth \00"
@SNDCTL_DSP_CHANNELS = global i32 0
@.str.6 = [22 x i8] c"setparams : channels \00"
@SNDCTL_DSP_SPEED = global i32 0
@.str.7 = [25 x i8] c"setparams : sample rate \00"
@SNDCTL_DSP_SYNC = global i32 0
@.str.8 = [18 x i8] c"setparams : sync \00"
@.str.9 = [45 x i8] c"set chans=%d, sample rate=%d sample size=%d\0A\00"
define i32 @aud_writeinit(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12, i32 %13)
%15 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i8* %15, i8** %10
%16 = icmp eq i8* null, %15
br i1 %16, label %17, label %18
17: ; preds = %3
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %10
br label %18
18: ; preds = %17, %3
%19 = load i8*, i8** %10
%20 = load i32, i32* @O_WRONLY
%21 = call i32 @open(i8* %19, i32 %20, i32 0)
store i32 %21, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %76
25: ; preds = %18
%26 = load i32, i32* %8
%27 = load i32, i32* @SNDCTL_DSP_RESET
%28 = call i32 @ioctl(i32 %26, i32 %27, i32* null)
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %4
br label %76
34: ; preds = %25
%35 = load i32, i32* %8
%36 = load i32, i32* @SNDCTL_DSP_SAMPLESIZE
%37 = call i32 @ioctl(i32 %35, i32 %36, i32* %6)
store i32 %37, i32* %9
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %4
br label %76
43: ; preds = %34
%44 = load i32, i32* %8
%45 = load i32, i32* @SNDCTL_DSP_CHANNELS
%46 = call i32 @ioctl(i32 %44, i32 %45, i32* %7)
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %4
br label %76
52: ; preds = %43
%53 = load i32, i32* %8
%54 = load i32, i32* @SNDCTL_DSP_SPEED
%55 = call i32 @ioctl(i32 %53, i32 %54, i32* %5)
store i32 %55, i32* %9
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %4
br label %76
61: ; preds = %52
%62 = load i32, i32* %8
%63 = load i32, i32* @SNDCTL_DSP_SYNC
%64 = call i32 @ioctl(i32 %62, i32 %63, i32* null)
store i32 %64, i32* %9
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %4
br label %76
70: ; preds = %61
%71 = load i32, i32* %7
%72 = load i32, i32* %5
%73 = load i32, i32* %6
%74 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.9, i64 0, i64 0), i32 %71, i32 %72, i32 %73)
%75 = load i32, i32* %8
store i32 %75, i32* %4
br label %76
76: ; preds = %70, %66, %57, %48, %39, %30, %23
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
@.str = [51 x i8] c"requested chans=%d, sample rate=%d sample size=%d\0A\00"
@.str.1 = [9 x i8] c"AUDIODEV\00"
@.str.2 = [9 x i8] c"/dev/dsp\00"
@O_WRONLY = global i32 0
@.str.3 = [18 x i8] c"setparams : open \00"
@SNDCTL_DSP_RESET = global i32 0
@.str.4 = [19 x i8] c"setparams : reset \00"
@SNDCTL_DSP_SAMPLESIZE = global i32 0
@.str.5 = [22 x i8] c"setparams : bitwidth \00"
@SNDCTL_DSP_CHANNELS = global i32 0
@.str.6 = [22 x i8] c"setparams : channels \00"
@SNDCTL_DSP_SPEED = global i32 0
@.str.7 = [25 x i8] c"setparams : sample rate \00"
@SNDCTL_DSP_SYNC = global i32 0
@.str.8 = [18 x i8] c"setparams : sync \00"
@.str.9 = [45 x i8] c"set chans=%d, sample rate=%d sample size=%d\0A\00"
define i32 @aud_writeinit(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %0, i32 %1)
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %8, null
%10 = select i1 %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %8
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %56
16: ; preds = %3
%17 = load i32, i32* @SNDCTL_DSP_RESET
%18 = tail call i32 @ioctl(i32 %12, i32 %17, i32* null)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @close(i32 %12)
br label %56
23: ; preds = %16
%24 = load i32, i32* @SNDCTL_DSP_SAMPLESIZE
%25 = call i32 @ioctl(i32 %12, i32 %24, i32* %5)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32 @close(i32 %12)
br label %56
30: ; preds = %23
%31 = load i32, i32* @SNDCTL_DSP_CHANNELS
%32 = call i32 @ioctl(i32 %12, i32 %31, i32* %6)
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32 @close(i32 %12)
br label %56
37: ; preds = %30
%38 = load i32, i32* @SNDCTL_DSP_SPEED
%39 = call i32 @ioctl(i32 %12, i32 %38, i32* %4)
%40 = icmp eq i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%43 = call i32 @close(i32 %12)
br label %56
44: ; preds = %37
%45 = load i32, i32* @SNDCTL_DSP_SYNC
%46 = call i32 @ioctl(i32 %12, i32 %45, i32* null)
%47 = icmp eq i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%50 = call i32 @close(i32 %12)
br label %56
51: ; preds = %44
%52 = load i32, i32* %6
%53 = load i32, i32* %4
%54 = load i32, i32* %5
%55 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.9, i64 0, i64 0), i32 %52, i32 %53, i32 %54)
br label %56
56: ; preds = %51, %48, %41, %34, %27, %20, %14
%57 = phi i32 [ -1, %14 ], [ -1, %20 ], [ -1, %27 ], [ -1, %34 ], [ -1, %41 ], [ -1, %48 ], [ %12, %51 ]
ret i32 %57
}