#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 unsigned int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int, char *, unsigned int);
int play(int dsp, char *audiodata, unsigned int bufsize) {
  ssize_t read = 0;
  while (read < bufsize) {
    int add;
    add = write(dsp, audiodata + read, bufsize - read);
    if (add > 0)
      read += add;
    else if (errno != EAGAIN) {
      fprintf(stderr, "could not write PCM data: %s\n", strerror(errno));
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<play>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11e5 <play+0xb5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1188 <play+0x58>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11e0 <play+0xb0>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
je     11db <play+0xab>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe36(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <play+0xbc>
jmp    11e0 <play+0xb0>
jmp    1149 <play+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<play>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     11ab <play+0x7b>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2e88(%rip),%r12
mov    0x2e71(%rip),%r13
jmp    1166 <play+0x36>
nopl   0x0(%rax)
add    %eax,%ebp
cmp    %ebx,%ebp
jae    11ab <play+0x7b>
mov    %ebp,%esi
add    %r14,%rsi
mov    %ebx,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jg     1160 <play+0x30>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
je     1162 <play+0x32>
mov    0x2e54(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe66(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11ad <play+0x7d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"could not write PCM data: %s\0A\00"
define i32 @play(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %40, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %41
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub i32 %20, %21
%23 = call i32 @write(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = add i32 %28, %27
store i32 %29, i32* %8
br label %40
30: ; preds = %14
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i64, i64* @errno
%37 = call i8* @strerror(i64 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %37)
store i32 -1, i32* %4
br label %42
39: ; preds = %30
br label %40
40: ; preds = %39, %26
br label %10
41: ; preds = %10
store i32 0, i32* %4
br label %42
42: ; preds = %41, %34
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"could not write PCM data: %s\0A\00"
define i32 @play(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %25, label %5
5: ; preds = %3, %22
%6 = phi i32 [ %23, %22 ], [ 0, %3 ]
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub i32 %2, %6
%10 = tail call i32 @write(i32 %0, i8* %8, i32 %9)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = add i32 %10, %6
br label %22
14: ; preds = %5
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i8* @strerror(i64 %15)
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %25
22: ; preds = %12, %14
%23 = phi i32 [ %13, %12 ], [ %6, %14 ]
%24 = icmp ult i32 %23, %2
br i1 %24, label %5, label %25
25: ; preds = %22, %3, %18
%26 = phi i32 [ -1, %18 ], [ 0, %3 ], [ 0, %22 ]
ret i32 %26
}