#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 /*<<< orphan*/ printf(char *, int, int);
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("/dev/sound/dsp0 write error");
        err++;
      } else
        printf("%d/%d written.\n", nwritten, nleft);
      nleft -= nwritten;
      p += nwritten;
    }
  }
  write(fd_audio, NULL, 0);
  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    0x2e6c(%rip),%rax
mov    (%rax),%edx
mov    $0x2c,%esi
call   1060 <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    123a <play_file+0xea>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1235 <play_file+0xe5>
mov    -0x4(%rbp),%edi
mov    -0x1028(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11fc <play_file+0xac>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    120e <play_file+0xbe>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
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    11ba <play_file+0x6a>
jmp    1180 <play_file+0x30>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <write@plt>
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,%rbp
mov    %edi,%ebx
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    %rsi,%rdi
mov    $0x2c,%esi
call   1060 <fseek@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbp,%r13
mov    %rbp,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1210 <play_file+0xc0>
mov    %eax,%ebp
movl   $0x0,0xc(%rsp)
lea    0xe6e(%rip),%r12
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %eax,%r14d
test   %eax,%eax
js     11fd <play_file+0xad>
mov    %r12,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1050 <printf@plt>
movslq %r14d,%rax
add    %rax,%r15
sub    %r14d,%ebp
jne    11b0 <play_file+0x60>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11b0 <play_file+0x60>
jmp    1218 <play_file+0xc8>
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
addl   $0x1,0xc(%rsp)
jmp    11d0 <play_file+0x80>
movl   $0x0,0xc(%rsp)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <write@plt>
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 = [28 x i8] c"/dev/sound/dsp0 write error\00"
@.str.1 = [16 x i8] c"%d/%d written.\0A\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 = %47, %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 %48
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 = %39, %19
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %47
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 ([28 x i8], [28 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 %39
35: ; preds = %25
%36 = load i32, i32* %8
%37 = load i32, i32* %7
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %35, %31
%40 = load i32, i32* %8
%41 = load i32, i32* %7
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %7
%43 = load i32, i32* %8
%44 = load i8*, i8** %10
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8* %46, i8** %10
br label %22
47: ; preds = %22
br label %14
48: ; preds = %14
%49 = load i32, i32* %3
%50 = call i32 @write(i32 %49, i8* null, i32 0)
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [28 x i8] c"/dev/sound/dsp0 write error\00"
@.str.1 = [16 x i8] c"%d/%d written.\0A\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 %32
9: ; preds = %23
%10 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %1)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %29, label %32
12: ; preds = %2, %29
%13 = phi i8* [ %30, %29 ], [ %4, %2 ]
%14 = phi i32 [ %24, %29 ], [ 0, %2 ]
%15 = phi i32 [ %31, %29 ], [ %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 ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%20 = add nsw i32 %14, 1
br label %23
21: ; preds = %12
%22 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %15)
br label %23
23: ; preds = %21, %18
%24 = phi i32 [ %20, %18 ], [ %14, %21 ]
%25 = sub nsw i32 %15, %16
%26 = sext i32 %16 to i64
%27 = getelementptr inbounds i8, i8* %13, i64 %26
%28 = icmp eq i32 %25, 0
br i1 %28, label %9, label %29
29: ; preds = %23, %9
%30 = phi i8* [ %27, %23 ], [ %4, %9 ]
%31 = phi i32 [ %25, %23 ], [ %10, %9 ]
br label %12
32: ; preds = %9, %2
%33 = phi i32 [ 0, %2 ], [ %24, %9 ]
%34 = tail call i32 @write(i32 %0, i8* null, i32 0)
ret i32 %33
}