#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*/ FILE;

/* Variables and functions */
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int single_fread_fwrite(int is_read, FILE *fd, char *buf, int count) {
  int tmp = 0;
  int ret;
  while (tmp < count) {
    if (is_read) {
      ret = fread(buf + tmp, 1, count - tmp, fd);
    } else {
      ret = fwrite(buf + tmp, 1, count - tmp, fd);
    }
    if (ret <= 0)
      return ret;
    tmp += ret;
  }
  return count;
}


x86 assembly (clang, -O0):
<single_fread_fwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11bf <single_fread_fwrite+0x9f>
cmpl   $0x0,-0x8(%rbp)
je     117a <single_fread_fwrite+0x5a>
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
sub    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
jmp    119c <single_fread_fwrite+0x7c>
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
sub    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11b1 <single_fread_fwrite+0x91>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <single_fread_fwrite+0xa5>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    113d <single_fread_fwrite+0x1d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<single_fread_fwrite>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebx
test   %ecx,%ecx
jle    1183 <single_fread_fwrite+0x63>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
nopl   0x0(%rax)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rdi
mov    %ebx,%edx
sub    %eax,%edx
mov    $0x1,%esi
mov    %r15,%rcx
test   %r12d,%r12d
je     1170 <single_fread_fwrite+0x50>
call   1030 <fread@plt>
test   %eax,%eax
jg     1179 <single_fread_fwrite+0x59>
jmp    1181 <single_fread_fwrite+0x61>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
call   1040 <fwrite@plt>
test   %eax,%eax
jle    1181 <single_fread_fwrite+0x61>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1140 <single_fread_fwrite+0x20>
jmp    1183 <single_fread_fwrite+0x63>
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @single_fread_fwrite(i32 %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %44, %4
%13 = load i32, i32* %10
%14 = load i32, i32* %9
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %48
16: ; preds = %12
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i8*, i8** %8
%21 = load i32, i32* %10
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i32, i32* %9
%25 = load i32, i32* %10
%26 = sub nsw i32 %24, %25
%27 = load i32*, i32** %7
%28 = call i32 @fread(i8* %23, i32 1, i32 %26, i32* %27)
store i32 %28, i32* %11
br label %39
29: ; preds = %16
%30 = load i8*, i8** %8
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %9
%35 = load i32, i32* %10
%36 = sub nsw i32 %34, %35
%37 = load i32*, i32** %7
%38 = call i32 @fwrite(i8* %33, i32 1, i32 %36, i32* %37)
store i32 %38, i32* %11
br label %39
39: ; preds = %29, %19
%40 = load i32, i32* %11
%41 = icmp sle i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = load i32, i32* %11
store i32 %43, i32* %5
br label %50
44: ; preds = %39
%45 = load i32, i32* %11
%46 = load i32, i32* %10
%47 = add nsw i32 %46, %45
store i32 %47, i32* %10
br label %12
48: ; preds = %12
%49 = load i32, i32* %9
store i32 %49, i32* %5
br label %50
50: ; preds = %48, %42
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
define i32 @single_fread_fwrite(i32 %0, i32* nocapture %1, i8* nocapture %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %23
6: ; preds = %4
%7 = icmp eq i32 %0, 0
br label %8
8: ; preds = %6, %20
%9 = phi i32 [ 0, %6 ], [ %21, %20 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %2, i64 %10
%12 = sub nsw i32 %3, %9
br i1 %7, label %15, label %13
13: ; preds = %8
%14 = tail call i32 @fread(i8* %11, i32 1, i32 %12, i32* %1)
br label %17
15: ; preds = %8
%16 = tail call i32 @fwrite(i8* %11, i32 1, i32 %12, i32* %1)
br label %17
17: ; preds = %15, %13
%18 = phi i32 [ %14, %13 ], [ %16, %15 ]
%19 = icmp slt i32 %18, 1
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = add nsw i32 %18, %9
%22 = icmp slt i32 %21, %3
br i1 %22, label %8, label %23
23: ; preds = %17, %20, %4
%24 = phi i32 [ %3, %4 ], [ %3, %20 ], [ %18, %17 ]
ret i32 %24
}