#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int play_file(int fd_audio, FILE *file) {
  int err = 0;
  int n, nleft, nwritten;
  char buf[4096], *p;
  fseek(file, 44, SEEK_SET);
  while ((n = fread(buf, 1, sizeof(buf), file)) > 0) {
    nleft = n;
    p = buf;
    while (nleft) {
      if ((nwritten = write(fd_audio, p, nleft)) < 0) {
        perror("audio driver write error");
        err++;
      }
      nleft -= nwritten;
      p += nwritten;
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<play_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
mov    $0x2c,%esi
call   1050 <fseek@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1213 <play_file+0xd3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     120e <play_file+0xce>
mov    -0x4(%rbp),%edi
mov    -0x1028(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11e7 <play_file+0xa7>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1028(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1028(%rbp)
jmp    11aa <play_file+0x6a>
jmp    1170 <play_file+0x30>
mov    -0x14(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<play_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
mov    %rsi,%rdi
mov    $0x2c,%esi
call   1050 <fseek@plt>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11f1 <play_file+0xb1>
mov    %eax,%ebx
movl   $0x0,0xc(%rsp)
mov    %r12,%r13
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11de <play_file+0x9e>
movslq %r15d,%rax
add    %rax,%r13
sub    %r15d,%ebx
jne    11a0 <play_file+0x60>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %eax,%ebx
mov    %r12,%r13
test   %eax,%eax
jg     11a0 <play_file+0x60>
jmp    11f9 <play_file+0xb9>
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
addl   $0x1,0xc(%rsp)
jmp    11b3 <play_file+0x73>
movl   $0x0,0xc(%rsp)
mov    0xc(%rsp),%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [25 x i8] c"audio driver write error\00"
define i32 @play_file(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i8*
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %5
%11 = load i32*, i32** %4
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @fseek(i32* %11, i32 44, i32 %12)
br label %14
14: ; preds = %43, %2
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%16 = load i32*, i32** %4
%17 = call i32 @fread(i8* %15, i32 1, i32 4096, i32* %16)
store i32 %17, i32* %6
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %44
19: ; preds = %14
%20 = load i32, i32* %6
store i32 %20, i32* %7
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
store i8* %21, i8** %10
br label %22
22: ; preds = %35, %19
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %43
25: ; preds = %22
%26 = load i32, i32* %3
%27 = load i8*, i8** %10
%28 = load i32, i32* %7
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %35
35: ; preds = %31, %25
%36 = load i32, i32* %8
%37 = load i32, i32* %7
%38 = sub nsw i32 %37, %36
store i32 %38, i32* %7
%39 = load i32, i32* %8
%40 = load i8*, i8** %10
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %10
br label %22
43: ; preds = %22
br label %14
44: ; preds = %14
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [25 x i8] c"audio driver write error\00"
define i32 @play_file(i32 %0, i32* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @fseek(i32* %1, i32 44, i32 %5)
%7 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %1)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %12, label %30
9: ; preds = %21
%10 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %1)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %27, label %30
12: ; preds = %2, %27
%13 = phi i8* [ %28, %27 ], [ %4, %2 ]
%14 = phi i32 [ %22, %27 ], [ 0, %2 ]
%15 = phi i32 [ %29, %27 ], [ %7, %2 ]
%16 = call i32 @write(i32 %0, i8* %13, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = add nsw i32 %14, 1
br label %21
21: ; preds = %18, %12
%22 = phi i32 [ %20, %18 ], [ %14, %12 ]
%23 = sub nsw i32 %15, %16
%24 = sext i32 %16 to i64
%25 = getelementptr inbounds i8, i8* %13, i64 %24
%26 = icmp eq i32 %23, 0
br i1 %26, label %9, label %27
27: ; preds = %21, %9
%28 = phi i8* [ %25, %21 ], [ %4, %9 ]
%29 = phi i32 [ %23, %21 ], [ %10, %9 ]
br label %12
30: ; preds = %9, %2
%31 = phi i32 [ 0, %2 ], [ %22, %9 ]
ret i32 %31
}