#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 size_t ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
size_t read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
size_t write(int, char *, size_t);
ssize_t rw_file(int fd, char *buf, size_t bytes, bool write_flag) {
  ssize_t rv = 0;
  ssize_t ret;
  while (bytes > rv) {
    if (write_flag)
      ret = write(fd, buf + rv, bytes - rv);
    else
      ret = read(fd, buf + rv, bytes - rv);
    if (ret > 0) {
      rv += ret;
    } else {
      if (ret < 0 && (errno == EAGAIN || errno == EINTR)) {
        continue;
      } else {
        fprintf(stderr, "%s failed:%s\n", write_flag ? "write" : "read",
                strerror(errno));
        return ret;
      }
    }
  }
  return rv;
}


x86 assembly (clang, -O0):
<rw_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1272 <rw_file+0x132>
cmpl   $0x0,-0x24(%rbp)
je     1197 <rw_file+0x57>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
jmp    11b3 <rw_file+0x73>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    11cf <rw_file+0x8f>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    126d <rw_file+0x12d>
cmpq   $0x0,-0x38(%rbp)
jae    1213 <rw_file+0xd3>
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
je     120e <rw_file+0xce>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <rw_file+0xd3>
jmp    115e <rw_file+0x1e>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%edx
lea    0xdeb(%rip),%rax
lea    0xdde(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xda5(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    127a <rw_file+0x13a>
jmp    115e <rw_file+0x1e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rw_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     11d5 <rw_file+0x95>
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebx,%ebx
nop
lea    (%r15,%rbx,1),%rsi
mov    %rbp,%rdx
sub    %rbx,%rdx
mov    %r12d,%edi
test   %r14d,%r14d
je     1180 <rw_file+0x40>
call   1030 <write@plt>
test   %rax,%rax
jne    118a <rw_file+0x4a>
jmp    1194 <rw_file+0x54>
xchg   %ax,%ax
call   1040 <read@plt>
test   %rax,%rax
je     1194 <rw_file+0x54>
add    %rax,%rbx
cmp    %rbp,%rbx
jb     1160 <rw_file+0x20>
jmp    11d7 <rw_file+0x97>
test   %r14d,%r14d
mov    0x2e42(%rip),%rax
mov    (%rax),%ebx
lea    0xe6d(%rip),%rax
lea    0xe60(%rip),%rbp
cmove  %rax,%rbp
mov    0x2e1f(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
lea    0xe38(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [14 x i8] c"%s failed:%s\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
define i64 @rw_file(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64 0, i64* %10
br label %12
12: ; preds = %66, %55, %4
%13 = load i64, i64* %8
%14 = load i64, i64* %10
%15 = icmp ugt i64 %13, %14
br i1 %15, label %16, label %67
16: ; preds = %12
%17 = load i32, i32* %9
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i32, i32* %6
%21 = load i8*, i8** %7
%22 = load i64, i64* %10
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i64, i64* %8
%25 = load i64, i64* %10
%26 = sub i64 %24, %25
%27 = call i64 @write(i32 %20, i8* %23, i64 %26)
store i64 %27, i64* %11
br label %37
28: ; preds = %16
%29 = load i32, i32* %6
%30 = load i8*, i8** %7
%31 = load i64, i64* %10
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i64, i64* %8
%34 = load i64, i64* %10
%35 = sub i64 %33, %34
%36 = call i64 @read(i32 %29, i8* %32, i64 %35)
store i64 %36, i64* %11
br label %37
37: ; preds = %28, %19
%38 = load i64, i64* %11
%39 = icmp ugt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i64, i64* %11
%42 = load i64, i64* %10
%43 = add i64 %42, %41
store i64 %43, i64* %10
br label %66
44: ; preds = %37
%45 = load i64, i64* %11
%46 = icmp ult i64 %45, 0
br i1 %46, label %47, label %56
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @EAGAIN
%50 = icmp eq i64 %48, %49
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = load i64, i64* @errno
%53 = load i64, i64* @EINTR
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %51, %47
br label %12
56: ; preds = %51, %44
%57 = load i32, i32* @stderr
%58 = load i32, i32* %9
%59 = icmp ne i32 %58, 0
%60 = zext i1 %59 to i64
%61 = select i1 %59, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%62 = load i64, i64* @errno
%63 = call i8* @strerror(i64 %62)
%64 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %61, i8* %63)
%65 = load i64, i64* %11
store i64 %65, i64* %5
br label %69
66: ; preds = %40
br label %12
67: ; preds = %12
%68 = load i64, i64* %10
store i64 %68, i64* %5
br label %69
69: ; preds = %67, %56
%70 = load i64, i64* %5
ret i64 %70
}

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