#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int EOF;
int errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
int fflush(int /*<<< orphan*/ *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int log_data(unsigned char *, size_t, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int transfer_data(FILE *from, FILE *to, FILE *protocol) {
  unsigned char buffer[BUFSIZ];
  size_t len, written;
  int err;
  int ret;
  err = 0;
  while (1) {
    len = fread(buffer, 1, sizeof(buffer), from);
    if (len == 0)
      break;
    err = log_data(buffer, len, from, to, protocol);
    if (err)
      break;
    written = fwrite(buffer, 1, len, to);
    if (written != len) {
      err = errno;
      break;
    }
    ret = fflush(to);
    if (ret == EOF) {
      err = errno;
      break;
    }
    if (ferror(from))
      break;
  }
  return err;
}


x86 assembly (clang, -O0):
<transfer_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
jmp    11a0 <transfer_data+0x50>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cc <transfer_data+0x7c>
jmp    127c <transfer_data+0x12c>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
call   1060 <log_data@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11f7 <transfer_data+0xa7>
jmp    127c <transfer_data+0x12c>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1230 <transfer_data+0xe0>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    127c <transfer_data+0x12c>
mov    -0x10(%rbp),%rdi
call   1050 <fflush@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%eax
jne    125f <transfer_data+0x10f>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    127c <transfer_data+0x12c>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1277 <transfer_data+0x127>
jmp    127c <transfer_data+0x12c>
jmp    119b <transfer_data+0x4b>
mov    -0x3c(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_data>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x30(%rbp)
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
mov    %rax,-0x38(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    -0x38(%rbp),%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     120e <transfer_data+0xbe>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
mov    -0x30(%rbp),%r8
call   1060 <log_data@plt>
test   %eax,%eax
jne    120b <transfer_data+0xbb>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1070 <fwrite@plt>
cmp    %r12,%rax
jne    11ff <transfer_data+0xaf>
mov    %r15,%rdi
call   1050 <fflush@plt>
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%eax
je     11ff <transfer_data+0xaf>
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1190 <transfer_data+0x40>
jmp    120e <transfer_data+0xbe>
mov    0x2dca(%rip),%rax
mov    (%rax),%r14d
jmp    120e <transfer_data+0xbe>
mov    %eax,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@errno = global i32 0
@EOF = global i32 0
define i32 @transfer_data(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
store i32 0, i32* %11
br label %17
17: ; preds = %56, %3
br label %18
18: ; preds = %17
%19 = trunc i64 %14 to i32
%20 = load i32*, i32** %4
%21 = call i64 @fread(i8* %16, i32 1, i32 %19, i32* %20)
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
br label %57
25: ; preds = %18
%26 = load i64, i64* %9
%27 = load i32*, i32** %4
%28 = load i32*, i32** %5
%29 = load i32*, i32** %6
%30 = call i32 @log_data(i8* %16, i64 %26, i32* %27, i32* %28, i32* %29)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
br label %57
34: ; preds = %25
%35 = load i64, i64* %9
%36 = load i32*, i32** %5
%37 = call i64 @fwrite(i8* %16, i32 1, i64 %35, i32* %36)
store i64 %37, i64* %10
%38 = load i64, i64* %10
%39 = load i64, i64* %9
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = load i32, i32* @errno
store i32 %42, i32* %11
br label %57
43: ; preds = %34
%44 = load i32*, i32** %5
%45 = call i32 @fflush(i32* %44)
store i32 %45, i32* %12
%46 = load i32, i32* %12
%47 = load i32, i32* @EOF
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = load i32, i32* @errno
store i32 %50, i32* %11
br label %57
51: ; preds = %43
%52 = load i32*, i32** %4
%53 = call i64 @ferror(i32* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %51
br label %57
56: ; preds = %51
br label %17
57: ; preds = %55, %49, %41, %33, %24
%58 = load i32, i32* %11
%59 = load i8*, i8** %7
ret i32 %58
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@errno = global i32 0
@EOF = global i32 0
define i32 @transfer_data(i32* %0, i32* %1, i32* %2) {
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
br label %7
7: ; preds = %24, %3
%8 = call i64 @fread(i8* %6, i32 1, i32 %4, i32* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %27, label %10
10: ; preds = %7
%11 = call i32 @log_data(i8* %6, i64 %8, i32* %0, i32* %1, i32* %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = call i64 @fwrite(i8* %6, i32 1, i64 %8, i32* %1)
%15 = icmp eq i64 %14, %8
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = load i32, i32* @errno
br label %27
18: ; preds = %13
%19 = call i32 @fflush(i32* %1)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i32, i32* @errno
br label %27
24: ; preds = %18
%25 = call i64 @ferror(i32* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %7, label %27
27: ; preds = %24, %10, %7, %22, %16
%28 = phi i32 [ %17, %16 ], [ %23, %22 ], [ 0, %24 ], [ %11, %10 ], [ 0, %7 ]
ret i32 %28
}