#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 */
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int mutt_copy_bytes(FILE *in, FILE *out, size_t size) {
  char buf[2048];
  size_t chunk;
  while (size > 0) {
    chunk = (size > sizeof(buf)) ? sizeof(buf) : size;
    if ((chunk = fread(buf, 1, chunk, in)) < 1)
      break;
    if (fwrite(buf, 1, chunk, out) != chunk) {
      return (-1);
    }
    size -= chunk;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mutt_copy_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11f8 <mutt_copy_bytes+0xd8>
cmpq   $0x800,-0x20(%rbp)
jbe    1161 <mutt_copy_bytes+0x41>
mov    $0x800,%eax
mov    %rax,-0x830(%rbp)
jmp    116c <mutt_copy_bytes+0x4c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x828(%rbp)
cmp    $0x1,%rax
jae    11ac <mutt_copy_bytes+0x8c>
jmp    11f8 <mutt_copy_bytes+0xd8>
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
cmp    -0x828(%rbp),%rax
je     11e1 <mutt_copy_bytes+0xc1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <mutt_copy_bytes+0xdf>
mov    -0x828(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1137 <mutt_copy_bytes+0x17>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutt_copy_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
xor    %r14d,%r14d
test   %rdx,%rdx
je     119d <mutt_copy_bytes+0x7d>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x800,%rbx
mov    $0x800,%edx
cmovb  %rbx,%rdx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r13,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     119d <mutt_copy_bytes+0x7d>
mov    %rax,%rbp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   1040 <fwrite@plt>
cmp    %rbp,%rax
jne    1197 <mutt_copy_bytes+0x77>
sub    %rbp,%rbx
jne    1150 <mutt_copy_bytes+0x30>
jmp    119d <mutt_copy_bytes+0x7d>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mutt_copy_bytes(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca [2048 x i8]
%9 = alloca i64
store i32* %0, i32** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
br label %10
10: ; preds = %35, %3
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %39
13: ; preds = %10
%14 = load i64, i64* %7
%15 = icmp ugt i64 %14, 2048
br i1 %15, label %16, label %17
16: ; preds = %13
br label %19
17: ; preds = %13
%18 = load i64, i64* %7
br label %19
19: ; preds = %17, %16
%20 = phi i64 [ 2048, %16 ], [ %18, %17 ]
store i64 %20, i64* %9
%21 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%22 = load i64, i64* %9
%23 = load i32*, i32** %5
%24 = call i64 @fread(i8* %21, i32 1, i64 %22, i32* %23)
store i64 %24, i64* %9
%25 = icmp ult i64 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %19
br label %39
27: ; preds = %19
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%29 = load i64, i64* %9
%30 = load i32*, i32** %6
%31 = call i64 @fwrite(i8* %28, i32 1, i64 %29, i32* %30)
%32 = load i64, i64* %9
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 -1, i32* %4
br label %40
35: ; preds = %27
%36 = load i64, i64* %9
%37 = load i64, i64* %7
%38 = sub i64 %37, %36
store i64 %38, i64* %7
br label %10
39: ; preds = %26, %10
store i32 0, i32* %4
br label %40
40: ; preds = %39, %34
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
define i32 @mutt_copy_bytes(i32* nocapture %0, i32* nocapture %1, i64 %2) {
%4 = alloca [2048 x i8]
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %2, 0
br i1 %6, label %19, label %9
7: ; preds = %15
%8 = icmp eq i64 %18, 0
br i1 %8, label %19, label %9
9: ; preds = %3, %7
%10 = phi i64 [ %18, %7 ], [ %2, %3 ]
%11 = icmp ult i64 %10, 2048
%12 = select i1 %11, i64 %10, i64 2048
%13 = call i64 @fread(i8* %5, i32 1, i64 %12, i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = call i64 @fwrite(i8* %5, i32 1, i64 %13, i32* %1)
%17 = icmp eq i64 %16, %13
%18 = sub i64 %10, %13
br i1 %17, label %7, label %19
19: ; preds = %15, %9, %7, %3
%20 = phi i32 [ 0, %3 ], [ 0, %7 ], [ 0, %9 ], [ -1, %15 ]
ret i32 %20
}