#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
void play_file_dvr(int filefd, int dvrfd) {
  char buf[(512 * 188)];
  int count, written, bytes, total = 0;
  while ((count = read(filefd, buf, (512 * 188))) > 0) {
    total += count;
    fprintf(stderr, "read  %d (%d total)\n", count, total);
    written = 0;
    while (written < count) {
      bytes = write(dvrfd, buf + written, count - written);
      fprintf(stderr, "write %d\n", bytes);
      if (bytes < 0) {
        perror("write dvr");
        return;
      } else if (bytes == 0) {
        fprintf(stderr, "write dvr: 0 bytes !");
        return;
      }
      written += bytes;
    }
  }
}


x86 assembly (clang, -O0):
<play_file_dvr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x17820,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x17820(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x17810(%rbp),%rsi
mov    $0x17800,%edx
call   1040 <read@plt>
mov    %eax,-0x17814(%rbp)
cmp    $0x0,%eax
jle    127f <play_file_dvr+0x13f>
mov    -0x17814(%rbp),%eax
add    -0x17820(%rbp),%eax
mov    %eax,-0x17820(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x17814(%rbp),%edx
mov    -0x17820(%rbp),%ecx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x17818(%rbp)
mov    -0x17818(%rbp),%eax
cmp    -0x17814(%rbp),%eax
jge    127a <play_file_dvr+0x13a>
mov    -0x8(%rbp),%edi
lea    -0x17810(%rbp),%rsi
movslq -0x17818(%rbp),%rax
add    %rax,%rsi
mov    -0x17814(%rbp),%edx
sub    -0x17818(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1781c(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x1781c(%rbp),%edx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x1781c(%rbp)
jge    1235 <play_file_dvr+0xf5>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
jmp    127f <play_file_dvr+0x13f>
cmpl   $0x0,-0x1781c(%rbp)
jne    125e <play_file_dvr+0x11e>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    127f <play_file_dvr+0x13f>
jmp    1263 <play_file_dvr+0x123>
mov    -0x1781c(%rbp),%eax
add    -0x17818(%rbp),%eax
mov    %eax,-0x17818(%rbp)
jmp    11bd <play_file_dvr+0x7d>
jmp    115b <play_file_dvr+0x1b>
add    $0x17820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<play_file_dvr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x17818,%rsp
mov    %esi,%ebx
lea    0x10(%rsp),%rsi
mov    %edi,0x8(%rsp)
mov    $0x17800,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11fb <play_file_dvr+0xbb>
mov    %eax,%ebp
mov    0x2e69(%rip),%r14
lea    0xe82(%rip),%rsi
lea    0xe90(%rip),%r12
xor    %ecx,%ecx
add    %ebp,%ecx
mov    (%r14),%edi
mov    %ebp,%edx
mov    %ecx,0xc(%rsp)
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %r13d,%r13d
nopl   0x0(%rax)
movslq %r13d,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %ebx,%edi
call   1030 <write@plt>
mov    %eax,%r15d
mov    (%r14),%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
js     122f <play_file_dvr+0xef>
je     120d <play_file_dvr+0xcd>
add    %r15d,%r13d
cmp    %r13d,%ebp
jg     11a0 <play_file_dvr+0x60>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x17800,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
lea    0xe0b(%rip),%rsi
mov    0xc(%rsp),%ecx
jg     1187 <play_file_dvr+0x47>
add    $0x17818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%edi
lea    0xe12(%rip),%rsi
xor    %eax,%eax
add    $0x17818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>
lea    0xde9(%rip),%rdi
add    $0x17818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <perror@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"read %d (%d total)\0A\00"
@.str.1 = [10 x i8] c"write %d\0A\00"
@.str.2 = [10 x i8] c"write dvr\00"
@.str.3 = [21 x i8] c"write dvr: 0 bytes !\00"
define void @play_file_dvr(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [96256 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %9
br label %10
10: ; preds = %55, %2
%11 = load i32, i32* %3
%12 = getelementptr inbounds [96256 x i8], [96256 x i8]* %5, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 96256)
store i32 %13, i32* %6
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %56
15: ; preds = %10
%16 = load i32, i32* %6
%17 = load i32, i32* %9
%18 = add nsw i32 %17, %16
store i32 %18, i32* %9
%19 = load i32, i32* @stderr
%20 = load i32, i32* %6
%21 = load i32, i32* %9
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21)
store i32 0, i32* %7
br label %23
23: ; preds = %51, %15
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %55
27: ; preds = %23
%28 = load i32, i32* %4
%29 = getelementptr inbounds [96256 x i8], [96256 x i8]* %5, i64 0, i64 0
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = sub nsw i32 %33, %34
%36 = call i32 @write(i32 %28, i8* %32, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* @stderr
%38 = load i32, i32* %8
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %27
%43 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %56
44: ; preds = %27
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %56
50: ; preds = %44
br label %51
51: ; preds = %50
%52 = load i32, i32* %8
%53 = load i32, i32* %7
%54 = add nsw i32 %53, %52
store i32 %54, i32* %7
br label %23
55: ; preds = %23
br label %10
56: ; preds = %42, %47, %10
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"read %d (%d total)\0A\00"
@.str.1 = [10 x i8] c"write %d\0A\00"
@.str.2 = [10 x i8] c"write dvr\00"
@.str.3 = [21 x i8] c"write dvr: 0 bytes !\00"
define void @play_file_dvr(i32 %0, i32 %1) {
%3 = alloca [96256 x i8]
%4 = getelementptr inbounds [96256 x i8], [96256 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 96256)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %10, label %35
7: ; preds = %16
%8 = call i32 @read(i32 %0, i8* %4, i32 96256)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %35
10: ; preds = %2, %7
%11 = phi i32 [ %8, %7 ], [ %5, %2 ]
%12 = phi i32 [ %13, %7 ], [ 0, %2 ]
%13 = add nsw i32 %11, %12
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %13)
br label %18
16: ; preds = %29
%17 = icmp sgt i32 %11, %31
br i1 %17, label %18, label %7
18: ; preds = %10, %16
%19 = phi i32 [ %31, %16 ], [ 0, %10 ]
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [96256 x i8], [96256 x i8]* %3, i64 0, i64 %20
%22 = sub nsw i32 %11, %19
%23 = call i32 @write(i32 %1, i8* %21, i32 %22)
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%26 = icmp slt i32 %23, 0
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %35
29: ; preds = %18
%30 = icmp eq i32 %23, 0
%31 = add nsw i32 %23, %19
br i1 %30, label %32, label %16
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %7, %2, %32, %27
ret void
}